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

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

<?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";
    }

?>    

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

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.

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.

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.