Jump to content

Update mysql with checkbox


ayok

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.