0

I want add a checkbox in every row of a table in php. How can I fill a database cell with Yes or No by CheckBox in PHP? When I change checkbox name for example "name", I have an error.

    <?php
$con=mysql_connect("localhost","biterium_login","login");
mysql_select_db("biterium_login");

// Check connection
if (!$con){
     die("به دلیل مشکل زیر، اتصال برقرار نشد : <br />" . mysql_error());
}

$result = mysql_query("SELECT * FROM member",$con);



echo "<table border='1'>
<tr>
<th>id</th>
<th>name</th>
<th>phone</th>
<th>email</th>
<th>action</th>
<th>action</th>
<th>active</th>
</tr>";

while($row = mysql_fetch_assoc($result)) 
{
    echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['phone']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td><a href='delete.php?id=".$row['id']."'>Delete</a></td>";
echo "<td><a href='FormUpdate.php?id=".$row['id']."'>Edit</a></td>";
echo "<td><input type='checkbox' value='".$row['id']."'  name='".$row['id']."'></td>";
echo "</tr>";
}
echo "</table>";
?>

<a href='add.html'>ADD</a></td>
Reza Mirzaei
  • 159
  • 2
  • 4
  • 2
    FYI, [you shouldn't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](https://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which one is best for you. – John Conde Jan 09 '18 at 13:18
  • set name="checkArray[]" and when you submit your form that time you will get checked checkbox value using $_REQUEST – Sachin Sarola Jan 09 '18 at 13:20
  • `$_Get` is incorrect syntax. Look up "php superglobal" to get the correct syntax. – Funk Forty Niner Jan 09 '18 at 13:22

1 Answers1

0

There are a couple of ways to do it. You can either use a hidden form element like this:

echo "<td><input type='hidden' name='status' value='N'><input type='checkbox' name='status' value='Y'></td>";

(I renamed the element to be 'status' rather than the row's id to make it more generic.)

This method takes advantage of the fact that the unchecked element will not be sent when submitting the form but the hidden element will be. If the checkbox is selected, the value from the checkbox will override the value of the hidden element because the checkbox element comes after the hidden element in the form.

Or you can check if the status field exists in the submitted form data and set 'Y' or 'N' accordingly.

This is all moot anyway because your code doesn't submit forms or process data anywhere that you've shown us.

Jeffwa
  • 1,143
  • 10
  • 12