2016-11-12 81 views
1

嘗試使用UPDATE(下面給出的代碼)更新多行時,它會使用最後一行中出現的行更新所有行。請幫忙解決這個問題。更新SQL數據中的多行

我的代碼:

<?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

在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']); 

這將獲取從表單您所提交的數據