尝试使用UPDATE(下面给出的代码)更新多行时,它会使用最后一行中显示的行更新所有行。请帮忙解决这个问题。
我的代码:
<?php
mysql_connect("localhost","root","");
mysql_select_db("my_db");
if( isset($_POST["submit"])){
foreach( $_POST["id"] AS $id) {
$FA1 = mysql_real_escape_string ($_POST ['FA1']);
$FA2 = mysql_real_escape_string ($_POST ['FA2']);
$FA3 = mysql_real_escape_string ($_POST ['FA3']);
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="UPDATE product_tbl SET Agency='$FA1', Material='$FA2', Category='$FA3' WHERE id=$id LIMIT 1 ";
mysql_query($sql) or die(mysql_error());
$result=mysql_query($sql);
//如果成功更新日期。
if($result){
echo "<script>alert('Data upDated Successfully in the DB')</script>";
echo "<BR>";
echo "<script>window.open('edit.php','_self')</script>";
}
}
}
?>
我如何得到结果?
答案 0 :(得分:0)
在foreach循环中,变量$ FA1,$ FA2,$ FA3再次获取相同的值,因为它直接从表单数据获取值
$FA1 = mysql_real_escape_string ($_POST ['FA1']);
$FA2 = mysql_real_escape_string ($_POST ['FA2']);
$FA3 = mysql_real_escape_string ($_POST ['FA3']);
这将从您提交的表单中获取数据