插入数据不工作php mysql

插入数据不工作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

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.

It is not looking any error in code , Please check your database table structure about each fields , If you are not sure then please add column name also in like INSERT INTO table_name (column1, column2) VALUES (value1, value2 ), You might have some more/additional columns in you database Table , Please double check that .

First you need to check whether condition is true mean check the control going inside your condition if(isset($_POST['submit'])){ //code} put die('inside condition') for the check.

Then you should insert with INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)

after mysql_query you should write the echo mysql_errno() . ": " . mysql_error() . " "; for the get the error and find out what is the error occur during the execution.

mysql_* is Deprecated try to use mysqli_* or PDO

Warning mysql_query, mysql_fetch_array,mysql_connect etc.. extensions were 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.

            //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. ";
                        }
                }
            ?>