Php 将Select从HTML插入mysql

Php 将Select从HTML插入mysql,php,mysql,Php,Mysql,所以我的html中有这个表 <form method = "POST" action="admin_event.php"> <table class = "table" id="dynamic_admin_add_price"> <thead>

所以我的html中有这个表

<form method = "POST" action="admin_event.php">
<table class = "table" id="dynamic_admin_add_price">
                                                        <thead>
                                                        <th>Deadline </th>
                                                        <th>Register Date</th>
                                                        <th>Price</th>
                                                        <th></th>
                                                        <th></th>
                                                        </thead>

                                                        <tbody>
                                                        <tr>
                                                            <td>
                                                                <select name="admin_add_deadline[]" class="form-control">
                                                                    <option value="early_bird">Early Bird</option>
                                                                    <option value="middle">Middle</option>
                                                                    <option value="normal">Normal</option>
                                                                </select>
                                                            </td>
                                                            <td><input type="text" name="admin_add_register_date[]" class="form-control registerDate" required></td>
                                                            <td><label>Rp.</label></td>
                                                            <td><input type="number" name="admin_add_price[]" class="form-control" value=""  required></td>
                                                            <td><button type="submit" name="button_add_price" id="price" class="btn btn-success">Add more deadlines</button></td>
                                                            <!--<td><button type="button" name=button_remove_event_time id="remove_event-time" class="btn btn-danger" disabled>Remove</button></td>-->
                                                        </tr>
                                                        </tbody>
                                                    </table>

 <button type="submit" class="btn btn-primary" name="admin_add_event_datas">Save</button>
</form>
这是插入数据的foreach

    foreach ($register_date as $key => $value) {

    $post_register_date = $db->real_escape_string($value);

    $post_insert_price = $db->real_escape_string($register_price[$key]);

    $post_deadline = $deadline[$key];

    $query = "INSERT INTO register_time(id_event, id_deadline, register_date, price, created_at, status)
                          VALUES('$row_id_event','$post_deadline', '$post_register_date', '$post_insert_price', '$time', '$status')";

    if(mysqli_query($db, $query)){
        $succ = "New record created successfully";
    }

    else{
        echo "Error: " . $query . "<br>" . mysqli_error($db);
    }

}
foreach($register\u date as$key=>$value){
$post\u register\u date=$db->real\u escape\u字符串($value);
$post_insert_price=$db->real_escape_字符串($register_price[$key]);
$post_deadline=$deadline[$key];
$query=“插入注册时间(id\u事件、id\u截止日期、注册日期、价格、创建时间、状态)
值(“$row\u id\u event”、“$post\u deadline”、“$post\u register\u date”、“$post\u insert\u price”、“$time”、“$status”);
if(mysqli_查询($db,$query)){
$succ=“新记录创建成功”;
}
否则{
echo“Error:”.$query.
“.mysqli_Error($db); } }
同样,问题是,如果生成了3行并单击了“提交”按钮,则表中id\u deadline列的插入值对于第一行始终为3,而对于插入查询中的id\u deadline,其他2行将返回一个错误,因为该值为“”

你们能帮我解决这个问题吗?谢谢,我希望我的问题足够清楚


编辑:修复打字错误

您的代码中有许多错误

  • HTML操作中缺少双引号

    <form method = "POST" action="admin_event.php">
    
    HTML中不存在,请对此进行更改:

     <td><a  id="add_price" id="price" class="btn btn-success">Add more deadlines</a></td>
    
  • 在PHP中,将其放入以查看数据:

    if(isset($_POST)){
        echo "<pre>";
        var_dump($_POST);
        die;
    }
    

    就我目前所知,在第三个代码段中,您用数字覆盖了
    $deadline
    变量,然后在第四个代码段中使用它作为数组


    你的代码片段太稀疏了,不能再说什么了,抱歉…

    现在开始工作了,只需使用名为“snytax”的表单字段将ifs和ELSE从截止日期移动到foreach

    $\u POST['admin\u add\u detaild']
    将是一个数组。为什么要将数组与字符串值进行比较?这没有任何意义。我遵循这里的方法:在那里,字段名不包含
    []
    ,因此这是一种完全不同的情况。您有一个数组,因此需要在其上循环并查看其中的各个项目。如果您不清楚从表单中获得的数据结构,请使用
    var\u dump($\u POST)来找出答案。啊,那是因为可以通过点击按钮生成另一行,并且生成的行还包含select字段。现在开始工作了,只需将ifs和ELSE从截止日期移动到ForEach中。这应该是一个注释,而不是aswers。啊,不,这是我在将代码复制到stackoverflow时的一个输入错误,因为我必须将一些变量名更改为英语,因为你们可能更熟悉它。它到达控制器是的,数据可以插入,但仅限于静态行,id\u截止日期的值始终为3,动态行没有任何结果。好的,如果我的英语使我的解释混淆,我很抱歉谢谢你的帮助,但我现在已经开始工作了,只是在foreach内的最后期限内移动了ifs。请将
    var\u dump($\u POST)的输出放在这里没关系,我把ifs和ELSE放错地方了,现在可以用了。抱歉浪费你的时间。别担心:)保重!
    
    $('#add_price').click(function(){
    
     <td><a  id="add_price" id="price" class="btn btn-success">Add more deadlines</a></td>
    
    var j = 0;
    
    if(isset($_POST)){
        echo "<pre>";
        var_dump($_POST);
        die;
    }
    
    array(4) {
      ["admin_add_deadline"]=>
      array(4) {
        [0]=>
        string(10) "early_bird"
        [1]=>
        string(6) "middle"
        [2]=>
        string(6) "normal"
        [3]=>
        string(6) "middle"
      }
      ["admin_add_register_date"]=>
      array(4) {
        [0]=>
        string(3) "one"
        [1]=>
        string(3) "two"
        [2]=>
        string(5) "three"
        [3]=>
        string(4) "four"
      }
      ["admin_add_price"]=>
      array(4) {
        [0]=>
        string(1) "1"
        [1]=>
        string(1) "2"
        [2]=>
        string(1) "3"
        [3]=>
        string(1) "4"
      }
      ["admin_add_event_datas"]=>
      string(0) ""
    }