Insert Data Into MySQL Database Using PHP

<html>
<head>
<title>Add New Record in my Created Table</title>
</head>
<body>
<?php
if(isset($_POST['add']))
{
$host = 'localhost';
$user = 'root';
$pass = '';
$conn = mysql_connect($host, $user, $pass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

if(! get_magic_quotes_gpc() )
{
   $ename = addslashes ($_POST['ename']);
   $eaddress = addslashes ($_POST['eaddress']);
}
else
{
   $ename = $_POST['ename'];
   $eaddress = $_POST['eaddress'];
}
$esalary = $_POST['esalary'];

$sql = "INSERT INTO emp ".
       "(ename,eaddress, esalary, jdate) ".
       "VALUES('$ename','$eaddress',$esalary, NOW())";
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Employee Name</td>
<td><input name="ename" type="text" id="ename"></td>
</tr>
<tr>
<td width="100">Employee Address</td>
<td><input name="eaddress" type="text" id="eaddress"></td>
</tr>
<tr>
<td width="100">Employee Salary</td>
<td><input name="esalary" type="text" id="esalary"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="add" type="submit" id="add" value="Add Employee">
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>

No comments: