ayok Posted November 10, 2008 Share Posted November 10, 2008 Hi, I have a simple and irritating problem with updating a column (boolean) with checkbox. For example: I got this data's on mysql _______________ ID | published | ----------------- |1 | yes | |2 | no | |3 | yes | etc. For example, I want to update ID no. 2 into "yes" with a checkbox: <?php include "db.php"; $id = $_GET[íd]; $select = mysql_query("select * from table where ID = '$id'"); $data = mysql_fetch_array($select); ?> <form method="post" ><input type="checkbox" name="publish" value="<?php echo $data['publish']; ?>" <?php if($data['publish'] == 'yes'){ echo "checked='yes'"; } ?> /><input type="submit" ></form> When it's submitted it sends the variables "publish". So to change no. 2 into yes, i simply check the box. <?php $publish = $POST[publish]; $query = mysql_query("UPDATE table SET publish = '$publish' WHERE ID = '$id'"); ?> However it doesn't work. Not as simple as to insert a new record. Would anybody help me? Thanks, ayok Quote Link to comment https://forums.phpfreaks.com/topic/132145-update-mysql-with-checkbox/ Share on other sites More sharing options...
gaza165 Posted November 10, 2008 Share Posted November 10, 2008 try doing some checks to see what value you are getting from the post.... die($publish) then u will see what value you are getting on post.. let me know Quote Link to comment https://forums.phpfreaks.com/topic/132145-update-mysql-with-checkbox/#findComment-686767 Share on other sites More sharing options...
JonnoTheDev Posted November 10, 2008 Share Posted November 10, 2008 Not the way to do it. Try this: <?php include "db.php"; $id = $_GET['id']; $select = mysql_query("SELECT * FROM table WHERE ID = '$id'"); $data = mysql_fetch_array($select); ?> <form method="post" > <input type="hidden" name="id" value="<?php echo $id; ?>" /> <input type="checkbox" name="publish" value="1"<?php if($data['publish'] == 'yes') echo " checked"; ?>/><input type="submit" ></form> Then your update part: <?php $publish = ($_POST['publish'] == 1) ? "yes" : "no"; $query = mysql_query("UPDATE table SET publish = '$publish' WHERE ID = '".$_POST['id']."'"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/132145-update-mysql-with-checkbox/#findComment-686785 Share on other sites More sharing options...
sasa Posted November 10, 2008 Share Posted November 10, 2008 you dont pass $id thru your form change form to <?php include "db.php"; $id = $_GET[íd]; $select = mysql_query("select * from table where ID = '$id'"); $data = mysql_fetch_array($select); ?> <form method="post" ><input type="checkbox" name="publish" value="yes" <?php if($data['publish'] == 'yes'){ echo "checked='checked'"; } echo "<input type='hidden' name='id' value='$id' />" ?> /><input type="submit" name="submit"></form> and 2nd part to <?php if (isset($_POST['submit'])){ $publish = isset($POST[publish]) ? 'yes' : 'no'; $id = $_POST['id']; $query = mysql_query("UPDATE table SET publish = '$publish' WHERE ID = '$id'"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/132145-update-mysql-with-checkbox/#findComment-686796 Share on other sites More sharing options...
ayok Posted November 11, 2008 Author Share Posted November 11, 2008 Hi thanks, I have tried this, but I keep getting ´no´ value. Could you please explain what this means? isset($POST[publish]) ? 'yes' : 'no'; thanks, ayok Quote Link to comment https://forums.phpfreaks.com/topic/132145-update-mysql-with-checkbox/#findComment-687800 Share on other sites More sharing options...
ayok Posted November 11, 2008 Author Share Posted November 11, 2008 you dont pass $id thru your form Should I pass $id again with <input type="hidden" />? Because the first $id value is the same as the second $id value. Quote Link to comment https://forums.phpfreaks.com/topic/132145-update-mysql-with-checkbox/#findComment-687801 Share on other sites More sharing options...
bobbinsbro Posted November 11, 2008 Share Posted November 11, 2008 @sasa: you bad, bad boy! forgot to quote the array key! @ayok: that's the ternary operator. it evaluates the code on the left of the "?", and then executes the bit in the middle (between the "?" and the ":"), if the evaluation returned true, and execute the bit on the right of the ":" if the evaluation returned false. it's like a one-line if-else statement. Quote Link to comment https://forums.phpfreaks.com/topic/132145-update-mysql-with-checkbox/#findComment-687802 Share on other sites More sharing options...
ayok Posted November 11, 2008 Author Share Posted November 11, 2008 Thanks guys, @bobbinsbro: Thanks for explaining that one. I knew it's like if-else statement, but I had difficulty to read it. It works with neil.johnson's codes; ($_POST['publish'] == 1) ? "yes" : "no"; Quote Link to comment https://forums.phpfreaks.com/topic/132145-update-mysql-with-checkbox/#findComment-687812 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.