繁体   English   中英

选中复选框,然后输入到新行

[英]checking checkbox and then input to a new row

如何根据复选框结果插入数据库。

我有5个房间,用户可以预订1个以上的房间。

如何仅将数据插入到我的预订表中以选择房间。

这是我正在尝试的方法,但仅适用于第一个房间,没有尝试过elseif,但是这也不起作用。

    if(isset($_POST['room1'])) {
        $query = "insert into bookings (bookingNo,roomNo) 
        values ('$bookingNo','1')";                 
        $result = mysql_query($query);
    } 

基本上,这只是输入roomNo和该房间的详细信息,例如预订日期和时间。

谢谢

形成

            <fieldset>

            <legend>Rooms</legend>
                <ol>
                    <li>
                        <label for =youthCafe>Youth Cafe</label>
                        <input type="checkbox"name="roomid[]" value="1" ><br>
                        <label for =inkwellMain>Inkwell Main</label>
                        <input type="checkbox" name="roomid[]" value="2"><br>
                        <label for =inkwellSmall>Inkwell Small</label>
                        <input type="checkbox"name="roomid[]" value="3"><br>
                        <label for =kitchen>Kitchen</label>
                        <input type="checkbox"name="roomid[]" value="4"><br>
                        <label for =outsideCatering>Outside Catering</label>
                        <input type="checkbox" name="roomid[]" value="5"><br>
                    </li>
                </ol>
            </fieldset>

您应该首先创建表格。 正如您没有提到的,我将举一个例子:

重要提示:您应该使用MYSQLi或PDO函数来避免sql注入。 除了这就是您所需要的。 (只是基础知识)

<?php
if ( isset($_POST['roomid']) ){
    foreach( $_POST['roomid'] as $value ){ //Receive the checkboxes as an array

         //you did not specify where $bookingNo came from, so
         //I'm assuming that you already have it from somewhere.

         $query = "insert into bookings (bookingNo,roomNo) 
                    values ('$bookingNo','$value')"; 

         $result = mysql_query($query);
    }
}
?>

<html>

<body>
    <form name="frm" method="post">
        <input type="checkbox" name="roomid[]" value="1"/>Room 1<br/>
        <input type="checkbox" name="roomid[]" value="2"/>Room 2<br/>
        <input type="checkbox" name="roomid[]" value="3"/>Room 3<br/>
        <input type="submit" name="submit" value="Send"/><br/>
    </form>
</body>

</html>

如果room1是复选框。 $ _POST ['room1']可能设置为“ on”,而不是“ true”或“ 1”。 首先调试$ _POST ['room1']

var_dump($_POST['room1']);

大多数情况下,这有助于了解有关类型和设置值的更多信息。

您可以使用javascript在复选框上设置事件,以在服务器上调用ajax函数。 服务器上的功能将更新数据库。 这样,您就可以将数据库工作与界面工作分开。 使用JQuery,它看起来像:

$('#myformelement :checkbox').click(function() {
   $.ajax({
        "url":      (call function using http request)...,
        "type":     "post",
        "data":     { elementid },
        "success":  function(data) {
            //everything ok
        },
        "error": function(data) {
           //error
        }
    });

试试这个代码

<form action="index.php" method="post">
<input type="checkbox" name="room[]" value="1" />PHP<br />
<input type="checkbox" name="room[]" value="2" />HTML<br />
<input type="checkbox" name="room[]" value="3" />Java<br />
<input type="checkbox" name="room[]" value="4" />C++<br />
<input type="checkbox" name="room[]" value="5" />C++<br />
<input type="submit" value="submit" />
</form>
if ( isset($_POST['room']) ){
    foreach($_POST['room'] as $value)
    {
        $query = "insert into bookings (bookingNo,roomNo) 
            values ('$bookingNo','$value')";                 
            $result = mysql_query($query);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM