Home >Backend Development >PHP Tutorial >php update database information

php update database information

巴扎黑
巴扎黑Original
2016-11-23 10:48:061417browse

php updates database information

<html>
<head>
<title>Update a Record in MySQL Database</title>
</head>
<body>
<?php
if(isset($_POST[&#39;update&#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 &#39;);
}
$emp_id = $_POST[&#39;emp_id&#39;];
$emp_salary = $_POST[&#39;emp_salary&#39;];
$sql = "UPDATE employee ".
  "SET emp_salary = $emp_salary ".
  "WHERE emp_id = $emp_id" ;
mysql_select_db(&#39;test_db&#39;);
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
 die(&#39;Could not update data&#39;);
}
echo "Updated 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 ID</td>
<td><input name="emp_id" type="text" id="emp_id"></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="update" type="submit" id="update" value="Update">
</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
Previous article:php name resolution rulesNext article:php name resolution rules