dswg47377 2016-09-28 06:36
浏览 15
已采纳

插入数据不工作php mysql

good day guys,

can you help to solve this problem this is a sample script for my assignment. I am having a problem regarding on insertion of data in mysql.

<?php 


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

        $name = mysql_real_escape_string($_POST['name']);
        $email = mysql_real_escape_string($_POST['email']);
        $contact = mysql_real_escape_string($_POST['contact']);
        $room = $_POST['room'];
        $adult = $_POST['adult'];
        $children = $_POST['children'];
        $arrival = $_POST['arrival'];
        $departure = $_POST['departure'];
        $date = date("Y/m/d H:i:s");
        $status = "Confirmed";

        $query = "INSERT INTO booking VALUES('".$name."','".$email."','".$contact."','".$room."','".$adult."','".$children."','".$arrival."','".$departure."','".$date."','".$status."')";
        if(mysql_query($query)){
            ?>
                <div class="alert alert-success" role="alert"  style="text-align:center; margin-top: 200px; background-color: #fff;" >
                    <p>New reservation has successfully sent!</p>
                </div> 
            <?php 
            header("refresh:2;url=/index.php");
        }
        else{
            echo "ERROR: Could not able to execute $sql. ";
        }
    }
?>
<form class="form-horizontal" method="post" action="reserve.php">
    <div class="form-group">
        <label for="focusedinput" class="col-sm-2 control-label">Name</label>
        <div class="col-sm-8">
            <input type="text" class="form-control1" id="focusedinput" name="name" placeholder="Enter Name" autofocus="">
        </div>
    </div>
    <div class="form-group">
        <label for="focusedinput" class="col-sm-2 control-label">Email</label>
        <div class="col-sm-8">
            <input type="text" class="form-control1" id="focusedinput" name="email" placeholder="Enter Email Address">
        </div>
    </div>
    <div class="form-group">
        <label for="focusedinput" class="col-sm-2 control-label">Contact</label>
        <div class="col-sm-8">
            <input type="text" class="form-control1" id="focusedinput" name="contact" placeholder="Enter Contact Number">
        </div>
    </div>

    <div class="form-group">
        <label for="selector1" class="col-sm-2 control-label">Select Room</label>
        <div class="col-sm-8">
        <select name="room" id="selector1" class="form-control1">
            <option value="null">Select Type of Room</option>
            <optgroup label="Dorm Type">
                <option value="aguila">Aguila Room</option>
                <option value="rizal">Rizal Room</option>
                <option value="kalabaw">Kalabaw Room</option>
            </optgroup>
            <optgroup label="Private Room">
                <option value="sampaguita">Sampaguita Room</option>
                <option value="bahay_kubo">Bahay Kubo Room</option>
                <option value="tinikling">Tinikling Room</option>
                <option value="kalesa">Kalesa Room</option>
            </optgroup>
        </select>
        </div>
    </div>
    <div class="form-group">
        <label for="focusedinput" class="col-sm-2 control-label">Adult</label>
        <div class="col-sm-3">
            <select name="adult" id="selector1" class="form-control1">
                <option value="1">1</option>
                <option value="2">2</option>         
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
            </select>
        </div>
        <label for="focusedinput" class="col-sm-2 control-label">Children</label>
        <div class="col-sm-3">
            <select name="children" id="selector1" class="form-control1">
                <option value="0">0</option>
                <option value="1">1</option>
                <option value="2">2</option>         
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label for="focusedinput" class="col-sm-2 control-label">Arrival Date</label>
        <div class="col-sm-3">
            <input type="text" class="form-control1 date" id="datepicker1" type="text" name="arrival" value="DD/MM/YY" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'DD/MM/YY';}">

        </div>
        <label for="focusedinput" class="col-sm-2 control-label">Departure Date</label>
        <div class="col-sm-3">
            <input type="text" class="form-control1 date" id="datepicker1" type="text" name="departure" value="DD/MM/YY" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'DD/MM/YY';}">
        </div>
    </div>
    <div class="clearfix"> </div><br><br>
    <div class="panel-footer">
        <div class="row"><br>
            <div class="col-sm-8 col-sm-offset-2">
                <input type="submit" name="submit" class="btn-success btn" value="Submit">
            </div>
        </div>
    </div>
</form>

when I clicked the submit button there's nothings happen. TY

  • 写回答

4条回答 默认 最新

  • dsaf415212 2016-09-28 06:41
    关注

    Note: Usage of mysql extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

    You have made some few mistakes so that the query might not be inserting datas into the phpmyadmin database. The basic error you have made is in the insert query by not concatenating the values that you want in the VALUES section and the insert statement syntax will be like this.

    Insert Syntax:

    INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)
    

    Note: If a column is AUTO_INCREMENT (like the "id" column) or TIMESTAMP (like the "reg_date" column), it is no need to be specified in the SQL query; MySQL will automatically add the value.

    Replace your Insert Code with the following

    $query = "INSERT INTO booking(`name`,`email`,`contact`,`room`,`adult`,`children`,`arrival`,`departure`,`date`,`status`) VALUES('".$name."','".$email."','".$contact."','".$room."','".$adult."','".$children."','".$arrival."','".$departure."','".$date."','".$status."')";
    

    Ensure all the values that i give in the table(column1,column2) are correct as per the table structure that you have.

    Mysql Connectivity

    You are missing mysql connectivity code in the code that you have provided. Ensure that you have placed the connectivity code to your file.

    <?php
       $dbhost = 'localhost';
       $dbuser = 'root';
       $dbpass = '';
       $conn = mysql_connect($dbhost, $dbuser, $dbpass);
       if(! $conn )
       {
         die('Could not connect: ' . mysql_error());
       }
       echo 'Connected successfully';
       mysql_select_db( 'TUTORIALS' );
       mysql_close($conn);
    ?>
    

    Note: You first put echo to the Insert Statement and then break the execution by putting the exit; and you copy the statement that is echoed and place it in SQL of the DB and then check whether any error occurs in insertion. If no error occurs remove the echo and delete the exit;

    And after all the check i am suggesting you to check the Note that i have mentioned just above to this line since it will be delivering you with the perfect error what you have made.

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

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)