0

I am very frustrated with the function below. It is not updated my flag in the database. I have tried putting the ones and zeros inbetween quotes and not. I have the field set to small integer in database. Do you see what I am doing wrong? The entry is there but the 1 is not updating to 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());
   }
}
1
  • Do not use mysql_* functions. They're deprecated! Use mysqli_* or PDO instead. Commented Feb 18, 2013 at 21:49

2 Answers 2

2

When the while expression exits, there'll be no $row left:

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

So this will not be true:

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

Consider moving the if statements inside the while loop.

Sign up to request clarification or add additional context in comments.

Comments

0

Looks like you're using logic outside of your loop that belongs inside of your loop:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.