无法使用php从动态创建的添加删除字段中插入数据

我正在尝试使用 php 从动态创建的数据库中添加删除字段插入数据,但无法执行。我只能从原始表单插入数据。

我的表格看起来像这样。请帮助我。

我的数据看起来像这样 

http://img1.mukewang.com/61a9f80f0001386513550088.jpg

<?php

require_once "conn.php"; 

$ch_direction= $ch_direction_through==$reg_id="";


if($_SERVER["REQUEST_METHOD"] == "POST"){  

    for ($i=0; $i < count($_POST['ch_direction']); $i++ )

{

        $ch_direction = trim($_POST["ch_direction"][$i]);

        $ch_direction_through = trim($_POST["ch_direction_through"][$i]);

        $reg_id= $_POST['reg_id'][$i];


        $sql = "INSERT INTO bps_registration_charkilla (ch_direction, ch_direction_through,reg_id) VALUES (?, ?, ?)";   


        if($stmt = mysqli_prepare($conn, $sql)){

            mysqli_stmt_bind_param($stmt, "sss",$ch_direction, $ch_direction_through, $reg_id);

            $reg_id=$reg_id;

            $ch_direction=$ch_direction; 

            $ch_direction_through=$ch_direction_through; 


            if(mysqli_stmt_execute($stmt)){

                if(!empty($reg_id)){

                    $success = "Submitted form successfully sent";     

                    header("location: registration_detail.php?success=$success&id=".$reg_id);

                    exit();

                } else {

                    header("location: registration_detail.php");

                    exit();

                }

            } else {

                echo "Something went wrong. Please try again later.";

            }

        }

    }

    mysqli_close($conn);

}

?>


$(document).ready(function() {

    var max_fields = 15; //maximum input boxes allowed

    var wrapper = $(".input_fields_wrap"); //Fields wrapper

    var add_button = $(".add_field_button"); //Add button ID

    var x = 1; //initlal text box count

    $(add_button).click(function(e){ //on add input button clicf k

        e.preventDefault();


        if(x < max_fields) { //max input box allowed

            x++; //text box increment

        }

    });


    $(wrapper).on("click",".remove_field", function(e) { //user click on remove text

        e.preventDefault();

        $(this).parent('div').remove(); x--;

    });

});


偶然的你
浏览 193回答 2
2回答

达令说

因为你有一个标题,代码将插入单行并对请求的文件做标题。因此,删除两个标题。

宝慕林4294392

您在循环内分配变量,但在循环后仅插入一次数据库。将您的数据库插入放入循环中,如下所示(为了更好地理解这个想法,我保持简短):for ($i=0; $i < count($_POST['ch_direction']); $i++ ) { // Here you start each loop&nbsp; &nbsp; $ch_direction = trim($_POST["ch_direction"][$i]);&nbsp; &nbsp; $ch_direction_through = trim($_POST["ch_direction_through"][$i]);&nbsp; &nbsp; $reg_id= $_POST['reg_id'][$i];&nbsp; &nbsp; $sql = "INSERT INTO bps_registration_charkilla (ch_direction, ch_direction_through,reg_id) VALUES (?, ?, ?)";&nbsp; &nbsp;&nbsp; &nbsp; ...&nbsp; &nbsp; if($stmt = mysqli_prepare($conn, $sql)) ... // Here you prepare the statement for the current element in the loop&nbsp; &nbsp; ...&nbsp; &nbsp; if(mysqli_stmt_execute($stmt)) ... // Here you actually insert current loop element into the database&nbsp; &nbsp; ...} // End of the loop
打开App,查看更多内容
随时随地看视频慕课网APP