douzhouhan4618 2017-06-07 13:44
浏览 33
已采纳

插入从HTML中选择到mysql

so i have this table in my 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>

And i have created the javascript so that the user can add as many rows as they want

    $('#add_price').click(function(){
    j++;
    $('#dynamic_admin_add_price').append(
        '<tr id="row2'+j+'">' +
        '<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_remove_event_time" id="'+j+'" class="btn btn-danger btn_remove2" required>Remove</button></td>' +
        '</tr>');
});

$(document).on('click', '.btn_remove2', function(){
    var button_id = $(this).attr("id");
    $('#row2'+button_id+'').remove();
});

The adding rows work fine and the rest of the data can be inputted into the mysql, EXCEPT for the select fields,where it returns incorrectly and doesn't get any input at all from the non-static rows.

What i want to do is that if the user chose early bird, it gave the number 1 in the php, 2 for the middle option, and 3 for the normal ones, as it matches the id_deadline in another table that is used as the foreign key in the table i want to insert the data to. This is the snippet of the php script(i only put the one for the deadline):

if(isset($_POST['admin_add_deadline'])){
$deadline = $_POST['admin_add_deadline'];

$one = 1;
$two = 2;
$three = 3;
if($deadline == 'early_bird'){

        $deadline = $one;
}

if($deadline == 'middle'){
        $deadline = $two;
}

else{
$deadline = $three
}


}

and this is the foreach to insert the data

    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);
    }

}

Again, the problem is that if i have 3 rows generated and i clicked the submit button, the inserted value for the id_deadline column in the table will always be 3 for the first row, and the other 2 rows will return an error as the value is '' for the id_deadline in the insert query

Can you guys help me with this problem?Thank you, i hope my question is clear enough.

EDIT: Fixing the typos

  • 写回答

3条回答 默认 最新

  • dqbr37828 2017-06-07 14:20
    关注

    Got it working now, just moved the ifs and elses from the deadline to the foreach

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等