插入所选复选框的隐藏字段

时间:2022-04-03 09:25:02

i am having a table with check boxes i want to insert specific hidden values of a row in database which is selected by the user using a checbox. But when ever i used to save the record in database. It insert only one record that is even not selected and is the top one in the table on the site. Here is my code

我有一个带复选框的表我想在数据库中插入一行的特定隐藏值,由用户使用checbox选择。但是,当我曾经用来保存数据库中的记录。它只插入一条甚至未选中的记录,并且是该站点表格中的*记录。这是我的代码

<?php 
    $i = 0;                         
    <td><?php echo $record['a'];?></td>
    <td><?php echo $record['name']; ?>
    <input type="hidden" name="name[]" value="<?php echo $record['name']; ?>"</td>
    <td><?php echo $record['contact'];?>
        <input type="hidden" name="contact[]" value="<?php echo $record['contact']; ?>"
    </td>
    <td><?php echo $record['district'];?>
        <input type="hidden" name="district[]" value="<?php echo $record['district']; ?>"
    </td>
    <td>
        <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $i++;?>" />
    </td>
<?php   }?>
    <input type="submit" value="Save" name="save" />
<?php     
    if ($_POST['save'])
    {
        foreach ($_POST['checkbox'] as $i)
        {   
            $save = "insert into tablename(id,name,contact,district) values (NULL, '{$_POST['name']'[$i]}'{$_POST['contractorname'][$i]}','{$_POST['district'][$i]}')";
            mysql_query($save) or die("Mistake in Query");
        }  
    echo "Record saved Successfully";
    }

?>    

3 个解决方案

#1


0  

You set $i = 0; inside that loop So each time the loop is run through, $i = 0; resets your counter to 0. This results in all your checkboxes having the same value 0. To resolve this, set $i = 0; before the loop starts

你设置$ i = 0;在那个循环中所以每次循环运行时,$ i = 0;将您的计数器重置为0.这会导致所有复选框具有相同的值0.要解决此问题,请设置$ i = 0;在循环开始之前

#2


0  

There is no foreach-loop around your input elements, or at lease it's not in the part of the code that you've posted. But the } before the submit-button lets me assume that there's a loop starting earlier in the code.

您的输入元素周围没有foreach循环,或者至少它不在您发布的代码部分中。但是在提交按钮之前的}让我假设在代码中有一个循环开始。

You set $i = 0; inside that loop, and later use echo $i++ to increment the value of the checkbox. But each time the (assumed) loop is run through, $i = 0; resets your counter to 0. This results in all your checkboxes having the same value 0. To resolve this, set $i = 0; before the loop starts.

你设置$ i = 0;在该循环内,稍后使用echo $ i ++增加复选框的值。但每次(假设)循环运行时,$ i = 0;将您的计数器重置为0.这会导致所有复选框具有相同的值0.要解决此问题,请设置$ i = 0;在循环开始之前。

#3


0  

First off always clean input prior to inserting it into the database. Otherwise you're vulnerable to SQL-injection attacks. Use functions like mysql_real_escape_string(). In addition, the mysql library in PHP is deprecated and you should consider using MySQLi or PDO:MySQL.

首先,在将输入插入数据库之前始终清理输入。否则你很容易受到SQL注入攻击。使用mysql_real_escape_string()之类的函数。另外,不推荐使用PHP中的mysql库,你应该考虑使用MySQLi或PDO:MySQL。

#1


0  

You set $i = 0; inside that loop So each time the loop is run through, $i = 0; resets your counter to 0. This results in all your checkboxes having the same value 0. To resolve this, set $i = 0; before the loop starts

你设置$ i = 0;在那个循环中所以每次循环运行时,$ i = 0;将您的计数器重置为0.这会导致所有复选框具有相同的值0.要解决此问题,请设置$ i = 0;在循环开始之前

#2


0  

There is no foreach-loop around your input elements, or at lease it's not in the part of the code that you've posted. But the } before the submit-button lets me assume that there's a loop starting earlier in the code.

您的输入元素周围没有foreach循环,或者至少它不在您发布的代码部分中。但是在提交按钮之前的}让我假设在代码中有一个循环开始。

You set $i = 0; inside that loop, and later use echo $i++ to increment the value of the checkbox. But each time the (assumed) loop is run through, $i = 0; resets your counter to 0. This results in all your checkboxes having the same value 0. To resolve this, set $i = 0; before the loop starts.

你设置$ i = 0;在该循环内,稍后使用echo $ i ++增加复选框的值。但每次(假设)循环运行时,$ i = 0;将您的计数器重置为0.这会导致所有复选框具有相同的值0.要解决此问题,请设置$ i = 0;在循环开始之前。

#3


0  

First off always clean input prior to inserting it into the database. Otherwise you're vulnerable to SQL-injection attacks. Use functions like mysql_real_escape_string(). In addition, the mysql library in PHP is deprecated and you should consider using MySQLi or PDO:MySQL.

首先,在将输入插入数据库之前始终清理输入。否则你很容易受到SQL注入攻击。使用mysql_real_escape_string()之类的函数。另外,不推荐使用PHP中的mysql库,你应该考虑使用MySQLi或PDO:MySQL。