我对以下功能感到非常沮丧。它没有在数据库中更新我的标志。我已经尝试将1和0放在引号之间而不是。我在数据库中将字段设置为小整数。你看到我做错了吗?该条目存在,但1未更新为0.
function postValue(){
global $customerID;
$query="SELECT flag FROM welcomecall WHERE customerID= '$customerID'";
echo $query;
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo "The customer flag is ". $row["flag"];
}
if ($row['flag']=='0'){
$query="UPDATE welcomecall SET flag='1' WHERE customerID='$customerID'";
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
}
else if($row['flag']=='1'){
$query="UPDATE welcomecall SET flag='0' WHERE customerID='$customerID'";
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
}
}
答案 0 :(得分:2)
当while
表达式退出时,将不会留下$row
:
while ($row = mysql_fetch_assoc($result)) {
所以这不是真的:
if ($row['flag']=='0'){
考虑在if
循环中移动while
语句。
答案 1 :(得分:0)
看起来你正在循环中使用属于循环内部的逻辑:
function postValue(){
global $customerID;
$query="SELECT flag FROM welcomecall WHERE customerID= '$customerID'";
echo $query;
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo "The customer flag is ". $row["flag"];
// Moved
if ($row['flag']=='0'){
$query="UPDATE welcomecall SET flag='1' WHERE customerID='$customerID'";
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
}
else if($row['flag']=='1'){
$query="UPDATE welcomecall SET flag='0' WHERE customerID='$customerID'";
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
}
}
}