我有一个从员工列表中填充的更新表单。值正在传递但未在数据库中更新。这是我的代码以及我所显示的内容。
<?php
$con = mysql_connect("localhost","root","*******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$query = mysql_query("select * from backup");
if(isset($_POST['update']))
$id = $_POST['id'];
$first = $_POST['first'];
$last = $_POST['last'];
$store = $_POST['store'];
$title = $_POST['title'];
$title2 = $_POST['other'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$dept = $_POST['dept'];
$bio = $_POST['bio'];
$query="UPDATE backup SET first='$first', last='$last', store='$store', title='$title', title2='$title2', phone='$phone', email='$email', bio='$bio' WHERE id='$id'";
mysql_query($query);
echo "Record Updated";
mysql_close();
print_r($_POST)
?>
结果如下
记录UpdatedArray([id] =&gt; 1396 [first] =&gt; Charles [last] =&gt; Adams [store] =&gt; [dept] =&gt;会计[title] =&gt;会计文员[其他] =&gt; [电话] =&gt; 410-555-1212 [电子邮件] =&gt; [email2] =&gt; [生物] =&gt;查理于2009年8月开始。这是一项测试.... [提交] =&gt;提交)
有人可以帮我解决我可能做错的事吗?至于注射,我将在完成测试后修复它。我知道这可能听起来倒退,但我需要找出为什么这不起作用。
感谢您对此的任何帮助
答案 0 :(得分:1)
您还应该选择数据库:
// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
但最重要的是,你应该使用MySQLi或PDO_MySQL。
答案 1 :(得分:1)
这是因为,您在{
之后忘了if (isset(...))
。
此外,未选择任何数据库。
更正后的代码如下:
<?php
$con = mysql_connect("localhost","root","*******");
mysql_select_db('DB_NAME');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$query = mysql_query("select * from backup");
if(isset($_POST['update'])) {
$id = $_POST['id'];
$first = $_POST['first'];
$last = $_POST['last'];
$store = $_POST['store'];
$title = $_POST['title'];
$title2 = $_POST['other'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$dept = $_POST['dept'];
$bio = $_POST['bio'];
$query="UPDATE backup SET first='$first', last='$last', store='$store', title='$title', title2='$title2', phone='$phone', email='$email', bio='$bio' WHERE id='$id'";
mysql_query($query);
echo "Record Updated";
mysql_close();
print_r($_POST)
}
?>
答案 2 :(得分:0)
您尚未在连接中定义数据库。
答案 3 :(得分:0)
您的代码可以更正为:
<?php
$con = mysqli_connect("localhost","root","**","db");
if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
//$query = mysqli_query($con,"select * from backup");
if(isset($_POST['update'])):
$id = $_POST['id'];
$first = $_POST['first'];
//other stuffs
$query="UPDATE backup SET first='$first', last='$last', store='$store', title='$title', title2='$title2', phone='$phone', email='$email', bio='$bio' WHERE id='$id'";
$res = mysqli_query($con,$query);
if(!$res)
die("could not update records. Error = ".mysqli_error($con));
echo "Record Updated";
mysqli_close($con);
print_r($_POST);
endif;
?>