简体   繁体   English

如何遍历复选框数组并插入MySQL?

[英]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. 现在,我需要将复选框数组中的数据插入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. 上一篇:我在最后一列中有一个HTML表,其中包含多个复选框。 I would like to take all of the selected checkboxes and insert their value into a MySQL table. 我想选择所有选中的复选框并将其值插入MySQL表中。 The values of the checkboxes are names being pulled from a different MySQL table. 复选框的值是从其他MySQL表提取的名称。 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. 仅查看html,您将遇到的一个问题是,只有选中的复选框被发送到服务器,所以对于两个以表格形式发送的表单,例如$_POST['check1'][3] ,您实际上不知道哪个框代表数据库中的哪一行。 $_POST['check1'][3]将代表不同的事物。

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. 如您在上面看到的,由于您已经在echo ,所以我也删除了echo语句。

Also, I don't know where $name comes from in your sql statement, but you probably have an sql injection problem. 另外,我不知道$name在您的sql语句中来自何处,但是您可能遇到了sql注入问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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