0

I have multiple checkboxes where the user should check the series he watches. In my database i want to update the column "watch" with the value "yes" in every series he checked (i have already created a column with all the series in it).

The HTML code:

<p>Please check the series you watch:</p>
<form action="2.php" method="post">
Game Of Thrones <input type="checkbox" value="Game Of Thrones" name="series"/><br>
The Big Bang Theory <input type="checkbox" value="The Big Bang Theory" name="series"/><br>
Arrow <input type="checkbox" value="Arrow" name="series"/><br>
<input type="submit" value="Submit">
</form>

The send.php:

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$series = $_POST['series'];
$sql = "UPDATE data SET watch='yes' WHERE series='$series'";

if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}

$conn->close();
?>

It works as long as the user check only one series, but when he check two or more it updates only one of the series he checked.

Help?

lipe0010
  • 41
  • 5

4 Answers4

1

You have to POST it as array, simple example:

<form method="POST">
<input type="checkbox" name="fields[]" value="1" />
<input type="checkbox" name="fields[]" value="2" />
</form>

After it was POSTed, you have an array $_POST['fields'] with all checked checkbox values. Process it like this:

foreach($_POST['fields'] as $field)
    $db->query("UPDATE table SET value = 1 WHERE id = '" . $db->real_escape_string($field) . "'");
Richard
  • 2,840
  • 3
  • 25
  • 37
0

Change your HTML code like:

<p>Please check the series you watch:</p>
<form action="2.php" method="post">
  <!-- make the name as an array -->
    Game Of Thrones <input type="checkbox" value="Game Of Thrones" name="series[]"/><br>
    The Big Bang Theory <input type="checkbox" value="The Big Bang Theory" name="series[]"/><br>
    Arrow <input type="checkbox" value="Arrow" name="series[]"/><br>
    <input type="submit" value="Submit">
</form>

And your send.php :

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$series = $_POST['series'];
for( $i = 0; $i < count($series); $i ++) {

  $sql = "UPDATE data SET watch='yes' WHERE series='$series[$i]'";

  if ($conn->query($sql) === TRUE) {
     echo "Record updated successfully";
  }  
  else {
     echo "Error updating record: " . $conn->error;
  }
}
$conn->close();
?>
Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
0

Set the input names as:

<input type="checkbox" value="Game Of Thrones" name="series[]"/>

That way $_POST['series'] will be an array.

If you are updating multiple rows in database build the query string like this:

$series = array_map( function( $item ) use ( $conn ) {
    return $conn->real_escape_string( $item );
}, $_POST['series'] );

$query = "UPDATE data SET watch='yes' WHERE series IN ( '" . implode( "', '", $series ) . "' )";
Danijel
  • 12,408
  • 5
  • 38
  • 54
0

You have to define the checkboxes as array in your form:

HTML

<input type="checkbox" value="Game Of Thrones" name="series[]"/>
<input type="checkbox" value="The Big Bang Theory" name="series[]"/>
<input type="checkbox" value="Arrow" name="series[]"/>

PHP

var_dump($_POST['series']);

See the PHP reference on this topic.

Be aware that your code contains an SQL injection vulnerability! See how to protect your code properly. Best is to use PHP's PDO (PHP Data Objects) extension.

Community
  • 1
  • 1
Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116