插入具有不同ID的多个行

时间:2016-11-22 05:30:44

标签: php html mysql

我正在创建一个系统,其中每行都有动态行和动态字段。我通过一个ADD按钮将动态行插入数据库时​​遇到问题。

当我点击该添加按钮时,它会保存数据,但数据保存在一个ID下。它也没有得到这个名字。它正在为所有人保存名字。我从数据库中获取这些名称。所以请帮我解决这个问题。我在添加具有不同ID的行时遇到问题。

我的表单代码

<form name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  <table>
  <tr>
    <td>Select</td>
    <td width="103">Date</td>
    <td width="94">Backup_Name</td>
    <td width="94">No Of Tapes</td>
    <td width="53">Tape Code</td>    
    <td width="71">Operator Approval</td>
    <td width="144">Operator Remark</td>
    <td width="144">Submit</td>
  </tr>
  <?php
   if ($que->num_rows > 0) {
    while ($row = mysqli_fetch_array($que)) {
   ?>
<tr>
    <td></td>
    <td><?php echo $Date;?></td>
    <td><?php echo $row['Backup_Name'];  ?></td>
    <td class="No_Of_Backup[]" value="" /><a href="#" class="addNewTape">Add</a></td>
    <td class="Tape_Code"></td>
    <td class="Operator_Verify_Status"></td>
    <td class="Operator_Remark"></td>

</tr>
<?php
    }
}
?>
</table>
<td><input type= "submit" value="add" name="add" onclick="submitdetails();"/></td>
</form>

我的PHP代码

<?php
include_once 'dpconnect.php';

//set validation error flag as false
$error = false;
$que=mysqli_query($MySQLiconn ,"select Backup_Name from admin_backup_list ");
$ftch=mysqli_fetch_array($que);
$Backup_Name=$ftch['Backup_Name'];


if (isset($_POST['add'])) {

$query1 = "insert into backup_list (Backup_Name) values ('". $Backup_Name ."')";
mysqli_query($MySQLiconn, $query1);


$last_id = mysqli_insert_id($MySQLiconn);
$Date=date('y/m/d');
    foreach($_POST['Tape_Code'] as $key => $value){

        $Tape_Code = $_POST['Tape_Code'][$key];
        $Operator_Verify_Status = $_POST['Operator_Verify_Status'][$key];
        $Operator_Remark = $_POST['Operator_Remark'][$key];

        $query2= "insert into tapes (Backup_List_ID, Tape_Code,Operator_Verify_Status, Operator_Remark, Operator_Date) values ('". $last_id ."','". $Tape_Code ."','" .$Operator_Verify_Status ."','". $Operator_Remark ."','". $Date ."');";
        mysqli_query($MySQLiconn, $query2);
    }
}
?>

动态文字字段

$(document).ready(function() {
    var i = 0;
    $('.addNewTape').on( "click", function() {
        $(this).parent().siblings(".Tape_Code").append('<input type="text" name="Tape_Code[]" value="'+ i +'"/><br>');
        $(this).parent().siblings(".Operator_Verify_Status").append('<SELECT name="Operator_Verify_Status[]"><OPTION value="verify">Verify</OPTION><OPTION value="NotVerify">Not Verify</OPTION><OPTION value="Failed">Failed</OPTION></SELECT><br>');
        $(this).parent().siblings(".Operator_Remark").append('<input type="text" name="Operator_Remark[]" value=""/><br>');
        i++;
    });

});


|backup list

id       name
-----------------

0        qwer
1        33344
2


tapes

id    code    approval
-----------------------
0      5959    yes
1      565     yes----->dynamic field
1      5656    yes----->dynamic field
2      8888    yes

我无法在数据库中获得这些1,1。现在我所有人都得到0

2 个答案:

答案 0 :(得分:0)

您在没有while循环的情况下获取数据,因此在while循环中获取数据,如下所示:

    while ($ftch=mysqli_fetch_array($que)) {
        if (isset($_POST['add'])) {

            $query1 = "insert into backup_list (Backup_Name) values ('". $Backup_Name ."')";
            mysqli_query($MySQLiconn, $query1);


            $last_id = mysqli_insert_id($MySQLiconn);
            $Date=date('y/m/d');

            foreach($_POST['Tape_Code'] as $key => $value){

                $Tape_Code = $_POST['Tape_Code'][$key];
                $Operator_Verify_Status = $_POST['Operator_Verify_Status'][$key];
                $Operator_Remark = $_POST['Operator_Remark'][$key];

                $query2= "insert into tapes (Backup_List_ID,  Tape_Code,Operator_Verify_Status, Operator_Remark, Operator_Date) values ('". $last_id ."','". $Tape_Code ."','" .$Operator_Verify_Status ."','". $Operator_Remark ."','". $Date ."');";
                mysqli_query($MySQLiconn, $query2);
            }
        }
    }

答案 1 :(得分:0)

您应该在AUTO INCREMENT上设置数据库的ID列,以便自动为新记录生成ID。大多数数据库支持自动增量既然你正在使用MySQL: http://dev.mysql.com/doc/refman/5.7/en/example-auto-increment.html