查询错误更新字段mysql

时间:2013-02-18 21:09:02

标签: php mysql sql

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

2 个答案:

答案 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());
        }
    }
}