0

我对下面的功能感到非常沮丧。它没有更新我在数据库中的标志。我尝试将 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());
   }
}
4

2 回答 2

2

while表达式退出时,就没有$row剩下的了:

while ($row = mysql_fetch_assoc($result)) {

所以这不是真的:

if ($row['flag']=='0'){

考虑在循环if内移动语句。while

于 2013-02-18T21:10:41.010 回答
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());
        }
    }
}
于 2013-02-18T21:10:51.920 回答