Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/132145-update-mysql-with-checkbox/
Share on other sites

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']."'");
?>

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'");
}
?>

@sasa: you bad, bad boy! forgot to quote the array key!  :P

 

@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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.