插入数据不工作php mysql

时间:2016-09-28 06:36:13

标签: php mysql

好日子,

你可以帮助解决这个问题这是我的作业的示例脚本。我在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>

当我点击提交按钮时,就会发生这样的事情。 TY

4 个答案:

答案 0 :(得分:1)

  

注意:在PHP 5.5.0中不推荐使用mysql扩展,并在PHP 7.0.0中删除了它。相反,应该使用MySQLi或PDO_MySQL扩展。

您犯了一些错误,因此查询可能无法将数据插入phpmyadmin数据库。你所做的基本错误是在插入查询中没有连接你想要在VALUES部分中的值,而insert语句的语法就是这样。

插入语法:

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

注意:如果列是AUTO_INCREMENT(如“id”列)或TIMESTAMP(如“reg_date”列),则不需要在SQL查询中指定; MySQL会自动添加值。

使用以下

替换插入代码
$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."')";

确保我在表格中提供的所有值(column1column2)都符合您的表结构。

Mysql连接

您在所提供的代码中缺少mysql连接代码。 确保已将连接代码放入文件中。

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

注意:您首先将echo放入Insert语句,然后通过放置退出来中断执行;然后复制回显的语句并将其放在DB的SQL中,然后检查插入中是否发生错误。如果没有错误发生,请删除echo并删除退出;

在完成所有检查之后,我建议你查看我在上面提到的注意事项,因为它会为你提供完美的错误。

答案 1 :(得分:1)

它没有在代码中查找任何错误,请检查您的数据库表结构有关每个字段,如果您不确定那么请添加列名也像INSERT INTO table_name(column1,column2)VALUES(value1,value2), 您可能在数据库表中有更多/更多列,请仔细检查。

答案 2 :(得分:0)

首先,您需要检查条件是否为真意味着检查控件是否在您的条件if(isset($_POST['submit'])){ //code}中放置die('inside condition')进行检查。

然后你应该插入INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)

在mysql_query之后,您应该为获取错误编写echo mysql_errno() . ": " . mysql_error() . "\n";并找出执行期间发生的错误。

答案 3 :(得分:0)

mysql_ *已弃用尝试使用mysqli_ *或PDO

  

警告mysql_query,mysql_fetch_array,mysql_connect等..扩展在PHP 5.5.0中已弃用,并且已在PHP 7.0.0中删除。   相反,应该使用MySQLi或PDO_MySQL扩展。

            //db connection

         global $conn;

            $servername = "localhost";  //host name

            $username = "username"; //username

            $password = "password"; //password

            $mysql_database = "dbname"; //database name

        //mysqli prepared statement 

            $conn = mysqli_connect($servername, $username, $password) or die("Connection failed: " . mysqli_connect_error());

           mysqli_select_db($conn,$mysql_database) or die("Opps some thing went wrong");





                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";

                     $stmt = $conn->prepare("INSERT INTO booking (`name`,`email`,`contact`,`room`,`adult`,`children`,`arrival`,`departure`,`date`,`status`) VALUES(?,?,?,?,?,?,?,?,?,?)");

                    $stmt->bind_param('ssssssssss',$name,$email,$contact,$room,$adult,$children,$arrival,$departure,$date,$status);

                    //The argument may be one of four types:

                    //i - integer
                    //d - double
                    //s - string
                    //b - BLOB
                    //change it by respectively 

                    $stmt->execute();

                    $row_count= $stmt->affected_rows;
                    $stmt->close();
                     $conn->close();



                        if($row_count>0){
                            ?>
                                <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. ";
                        }
                }
            ?>