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 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解