简体   繁体   中英

How can I loop through a checkbox array and insert into MySQL?

Update: The checkbox array works and fills with the appropriate data. Now I need to insert the data from the checkbox array into MySQL. Something is going awry with this code:

if(!empty($_POST['check1'])) {
foreach($_POST['check1'] as $check) { 
    $exclude_arr[] = $check;
    for ($j = 0; $j < count($exclude_arr); $j++){
        $exclude = $exclude_arr[$j];
        $sql = 'INSERT INTO exclude (name) VALUES ('.$exclude.')';
        $dbhc->query($sql);
    }
  }
}

Previous: I have an HTML table with an array of checkboxes in the last column. I would like to take all of the selected checkboxes and insert their value into a MySQL table. The values of the checkboxes are names being pulled from a different MySQL table. I'm having issues looping through the array of checkboxes.

<?php
$dbhc = new mysqli('localhost','user','password','database') or die(mysql_error());
echo "<form action=\"file.php\" method=\"post\">
<table border='0'>
    <tr>
        <th>strategygame</th>
        <th width='130px'>approachpath</th>
        <th width='130px'>universe</th>
        <th width='130px'>strategylevel</th>
        <th width='130px'>capacity</th>
        <th width='130px'>exclude</th>
    </tr>";
while($row = mysqli_fetch_array($result)){
    $localcounter = $localcounter + 1;
    echo "<tr>";
    echo "<td>" . $row['strategygame'] . "</td>";
    echo "<td align='center'>" . $row['approachpath'] . "</td>";
    echo "<td align='center'>" . $row['universe'] . "</td>";
    echo "<td align='center'>" . number_format($row['strategylevel'], 0, '.', ',') . "</td>";
    echo "<td align='center'>" . number_format($row['capacity'], 0, '.', ',') . "</td>";
    echo "<td align='center'><input type=\"checkbox\" name=\"check1[{$row['id']}]\" value=\"{$row['strategygame']}\"/></td>";
    echo "</tr>";
}
echo "</table>
<input type=\"submit\" value=\"Save Selection\"/> 
</form>";
if(!empty($_POST['check1'])) {
    foreach($_POST['check1'] as $check) { 
        $exclude_arr[] = $check;
        for ($j = 0; $j < count($exclude_arr); $j++){
            $exclude = $exclude_arr[$j];
            $sql = 'INSERT INTO exclude (name) VALUES ('.$exclude.')';
            $dbhc->query($sql);
        }
    }
}
?>

Looking just at the html, a problem you will run into, is that only checked checkboxes are sent to the server, so you never actually know which box represents which row in your database, for two sent in forms, for example $_POST['check1'][3] will represent different things.

The first thing I would change, is to make sure your checkboxes are easily identifyable, using for example something like:

<input type=\"checkbox\" name=\"check1[{$row['id']}]\" value=\"{$row['strategygame']}\"/>
                                                               ^^^^^^^^^^^^^^^^^^^^^^ don't use echo here, you are already echoing
                                       ^^^^^^^^^^^^ add an identifier to the checkbox

As you can see above, I have also removed the echo statement as you are already echoing.

Also, I don't know where $name comes from in your sql statement, but you probably have an sql injection problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM