Home >Backend Development >PHP Tutorial >php insert data into mysql

php insert data into mysql

巴扎黑
巴扎黑Original
2016-11-23 10:52:351045browse

php insert data into mysql

<html>
<head>
<title>Add New Record in MySQL Database</title>
</head>
<body>
<?php
if(isset($_POST[&#39;add&#39;]))
{
$dbhost = &#39;localhost:3306&#39;;
$dbuser = &#39;root&#39;;
$dbpass = &#39;root&#39;;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
 die(&#39;Could not connect to mysql&#39;);
}
if(! get_magic_quotes_gpc() )
{
  $emp_name = addslashes ($_POST[&#39;emp_name&#39;]);
  $emp_address = addslashes ($_POST[&#39;emp_address&#39;]);
}
else
{
  $emp_name = $_POST[&#39;emp_name&#39;];
  $emp_address = $_POST[&#39;emp_address&#39;];
}
$emp_salary = $_POST[&#39;emp_salary&#39;];
$sql = "INSERT INTO employee ".
  "(emp_id, emp_name,emp_address, emp_salary) ".
  "VALUES(1, &#39;$emp_name&#39;,&#39;$emp_address&#39;,$emp_salary)";
mysql_select_db(&#39;test_db&#39;);
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
 die(&#39;Could not enter data: &#39;);
}
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="emp_name" type="text" id="emp_name"></td>
</tr>
<tr>
<td width="100">Employee Address</td>
<td><input name="emp_address" type="text" id="emp_address"></td>
</tr>
<tr>
<td width="100">Employee Salary</td>
<td><input name="emp_salary" type="text" id="emp_salary"></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>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn