[英]Is there a problem with this mysql update in a php for loop?
我正在使用此php代碼更新表,但是沒有更新嗎?
if(isset($Submit))
{
include('connect_mysql.php');
for($i=0;$i<$count;$i++)
{
$sql = "UPDATE $table SET cost='{$cost[$i]}', net=('{$bypiece[$i]}' + '{$cost[$i]}') WHERE serial='{$serial[$i]}'";
$result = mysql_query($dbcon, $sql);
}
}
if($result)
{
header("location:admin-index.php#office.php");
mysql_close();
}
似乎應該可以,而且我沒有理由不這樣做。 我的變量已定義,當我用else語句回顯它們時,一切似乎都很好。 但是數據沒有被更新,並且頭沒有被重定向。 誰能幫我找到可行的解決方案?
調試時的一個好主意是打開錯誤報告。
error_reporting(E_ALL);
ini_set('display_errors', '1');
通過更改還可以查看sql語句中是否有任何錯誤
$result=mysql_query($dbcon, $sql);
至
$result=mysql_query($dbcon, $sql) or die(mysql_error());
更改:
net=('{$bypiece[$i]}' + '{$cost[$i]}')
至:
net=({$bypiece[$i]} + {$cost[$i]})
您還應該對查詢進行一些錯誤檢查。
if(isset($Submit)){
include('connect_mysql.php');
$count=20;
for($i=0;$i<$count;$i++){
$sql="UPDATE $table SET cost='{$cost[$i]}', net=({$bypiece[$i]} + {$cost[$i]}) WHERE serial='{$serial[$i]}'";
$result=mysql_query($dbcon, $sql) or die(mylsql_error());
}
if($result){
header("location:admin-index.php#office.php");
mysql_close();
}
}
$ count設置為什么? 由於$ count沒有設置為任何值,因此您的代碼無法進入現在的狀態。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.