1

I'm new in PHP so I faced a problem, I got these rows in MySQL

+---+--------------+
|ID |  published   |
+---+--------------+
| 1 |    TRUE      |
| 2 |    FALSE     |
| 3 |    TRUE      |
+---+--------------+

For example, I want to update ID no. 2 into "TRUE" with a checkbox:

<?php 

$host="localhost";  
$username="root";  
$password="password"; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="chek_box_test"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
?>

<?php 

$id = $_GET['id'];
$select = mysql_query("SELECT * FROM $tbl_name WHERE id = $id");
while($row = mysql_fetch_array($select)){
?>
<form method="post" action="checkbox_test.php?id=<?php echo $id; ?>">
<input type="checkbox" name="publish" <?php
if($row['publish'] == TRUE){
     echo "checked='checked'";
}
?>
/>
<input type='hidden' name='id' value="<?php echo $row['id']; ?>" />
<input type="submit" name="submit" value="Update"></form><?php } ?>

<?php 
if(isset($_POST['submit'])){
    echo $publish = ($_POST['publish'] == 1) ? TRUE : FALSE;
    $id=$_POST['id'];
    $query = mysql_query("UPDATE $tbl_name SET publish = '$publish' WHERE id =$id");
}
?>

My problem is if I check that PHP it only give me 'FALSE' value. I want that if I check and click submit it will give me "TRUE" or "FALSE".

Please help me to do this. Any useful advice will be appreciated.

6
  • What does your table field published actually hold? Tinyint (0|1), enum('yes','no') or enum('TRUE','FALSE'). You are mixing up true and yes. Commented Jun 23, 2014 at 11:33
  • oh sorry its a mistake` it was boolian number instaed of yes or no Commented Jun 23, 2014 at 14:03
  • There is no such thing as a boolean number in mysql. What is the datatype of this column? Commented Jun 23, 2014 at 14:05
  • Ok its tiny or enum. Pick one of them. Better if it is enum('TRUE','FALSE') Commented Jun 23, 2014 at 14:10
  • If it is tinyint 'yes' and 'no' are all mapped to 1, which is not what you want. Commented Jun 23, 2014 at 14:11

2 Answers 2

1

The value of your input field is missing:

<input type="checkbox" name="publish" value="1" <?php
if($row['publish'] == 'yes'){
     echo "checked='checked'";
}
?>
/>

To solve the error 'Undefined index publish' you could change

echo $publish = ($_POST['publish'] == 1) ? TRUE : FALSE;

to:

$publish = FALSE;
if(isset($_POST['publish']) and $_POST['publish']==1) {
  $publish = TRUE;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Its working. Its give me true value by check and uncheck. But 1 more problem have. Next time when i uncheck and click update button it show a error. And the error is 'Undefined index publish in my file name on line 34
0

Mention the value="TRUE"

   <input type="checkbox" name="publish"  <?php
   if($row['publish'] == 'yes'){
    echo "checked='checked' ";
    echo "value='TRUE'";
   }
   ?>
   />

1 Comment

plz see my question code one more time and First ans also

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.