infrabyte Posted May 24, 2011 Share Posted May 24, 2011 Hi Guys, I'm not sure what to do here. I have a variable that is passed on via a url. I have a database table that has a field called 'publish'. This field contains either a Yes or a No. All I want to do is check the field an if it is Yes then make it No, and likewise if it is No then make it Yes. Here is my code. It changes one way but not the other. The value of $publish seems to stay the same...please help...Thank you. <?php // connect to the database include('connect-db.php'); // get id value $id = $_GET['id']; echo "$id"; $publish = $_GET['publish']; echo "$publish"; $sql="SELECT * FROM events WHERE ID='$id'"; $result=mysql_query($sql); if ($publish='Yes') { $publish=='No'; } else { $publish=='Yes'; } // echo "<p>$publish</p>"; $sqltwo = "UPDATE events SET publish='$publish' WHERE ID='$id'"; $resulttwo = mysql_query($sqltwo); // sleep(2); // header("Location: beacon.php"); ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 24, 2011 Share Posted May 24, 2011 Your IF condition is using an assignment (single equal sign) not a comparison (double equal). Since it is successfully able to assign the value of 'Yes' to the $publish variable it is always true. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 24, 2011 Share Posted May 24, 2011 By the way, you can put an If/Else within the query itself. I don't even see what the purpose is of the first query in the code you provided. // connect to the database include('connect-db.php'); // get id value $id = $_GET['id']; echo "$id"; $publish = $_GET['publish']; echo "$publish"; $sqltwo = "UPDATE events SET publish = IF('$publish'='Yes', 'No', 'Yes') WHERE ID='$id'"; $result=mysql_query($sql); Also, I would advise using the values "Yes"/"No". They mean nothing programatically as they are just strings. For boolean values you should be using 1 and 0. Otherwise you are always having to convert the strings too true/false. Quote Link to comment Share on other sites More sharing options...
mikosiko Posted May 24, 2011 Share Posted May 24, 2011 ... Also, I would advise using the values "Yes"/"No". They mean nothing programatically as they are just strings. +1... ^^ .....For boolean values you should be using 1 and 0. . and in that case the UPDATE is simply: $sqltwo = "UPDATE events SET publish = !publish WHERE ID='$id'"; Quote Link to comment Share on other sites More sharing options...
infrabyte Posted May 24, 2011 Author Share Posted May 24, 2011 Hi, I really, really thank you for that...It solved my problem that I have been trying to work around since last night. Thank you Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.