-1

In my program, I have 4 checkboxes that correspond to 4 text fields. The PHP script should insert values into my database only where the checkboxes are checked. Unfortunately, it only works properly if all of the checkboxes are checked. Why is this?

Here is the testchk.html

<html>
<head>
<title>testchk</title>
</head>
<body bgcolor="pink">
<h3> choice item</h3>
<form action="testchk.php" method="post">
    <input type="checkbox" name="chk1[]" value="vegetables">vegetables
    <input type="text" name="temperature[]"><br />
    <input type="checkbox" name="chk1[]" value="frozen">frozen products
    <input type="text" name="temperature[]"><br />
    <input type="checkbox" name="chk1[]" value="dried">dried goods
    <input type="text" name="temperature[]"><br />
    <input type="checkbox" name="chk1[]" value="cooling">
    <input type="text" name="temperature[]"><br />
    <br>
    <input type="submit" name="submit" value="submit">
    </form>
</body>
</html>

and here is the testchk.php

include ("db.php");

$checkbox1 = $_POST['chk1'];
$temperature = $_POST['temperature'];
if ($_POST["submit"]=="submit") {
    for ($i=0; $i<sizeof($checkbox1); $i++) {
        $sql="INSERT INTO test (Name, temp) VALUES ('".$checkbox1[$i]."', '".$temperature[$i]."' )";  
        mysql_query($sql) or die(mysql_error());

    }
    echo "insert";
}
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • 4
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 11 '15 at 21:37
  • 1
    I should have something that does that @Cristik but it is just the program in my head that causes me to click on questions. ;-) – Jay Blanchard May 11 '15 at 21:40

1 Answers1

0

This is because of the difference between how checkboxes and text inputs are handled. Only the checkboxes that you have checked will be submitted with your form, but all of the text inputs will be submitted whether you have entered anything into them or not. When you name the inputs using [], PHP will generate numeric indices for those arrays. This means that if you do not check all the checkboxes, the index numbers for the checkboxes will not match up with the index numbers for the text inputs. You can fix this by explicitly defining the index numbers in your HTML:

<input type="checkbox" name="chk1[0]" value="vegetables">vegetables
<input type="text" name="temperature[0]"><br>
<input type="checkbox" name="chk1[1]" value="frozen">frozen products
<input type="text" name="temperature[1]"><br>
<input type="checkbox" name="chk1[2]" value="dried">dried goods
<input type="text" name="temperature[2]"><br>
<input type="checkbox" name="chk1[3]" value="cooling">
<input type="text" name="temperature[3]"><br>

Then you can use foreach instead to only deal with the ones where the checkbox was checked.

foreach($checkbox1 as $index => $column_name) {
    // Do your query using $temperature[$index];
}
Don't Panic
  • 41,125
  • 10
  • 61
  • 80