无法使用php将数据插入数据库(mysql)

无法使用php将数据插入数据库(mysql)

问题描述:

I am a beginner at both mysql and php. And very badly stuck at this problem. Not sure where the problem is. but if i execute the insert query directly, it gets executed while if i accept it from user it dont(It is shown in the code). Probably the problem is with the $_POST[] method that i am using to retrieve the values submitted by user. I have submitted both the codes, addbooks.php(form from which user submits values) and add.php (to insert into the database).

    //add.php
<?php
$con=mysqli_connect("localhost","root","","a_database");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

//Using the following statements i am able to insert data.
//mysqli_query($con,"INSERT INTO books (book_name, book_author, book_price)
//VALUES ('Peter', 'Griffin',35)");


//But when i accept it from user(for which the following script is written), it is not working
if (isset($_POST['name']) && isset($_POST['author']) && isset($_POST['publication']) && isset($_POST['price']) && isset($_POST['stock'])) 
    {
    $book_name = $_POST['name'];   //post method to retrieve the value submited by user
    $book_author = $_POST['author'];  //post method to retrieve the value submited 
    $book_publication = $_POST['publication'];  //post method to retrieve the value submited by user
    $book_price = $_POST['price'];  //post method to retrieve the value submited by user
    $book_stock = $_POST['stock']; //post method to retrieve the value submited by user
    mysqli_query($con, "INSERT INTO 'books' (book_name, book_author, publication, book_price, book_stock) VALUES ($book_name, $book_author, $book_publication, $book_price, $book_stock)");
mysqli_close($con);

}
?>

//the form from which the values are being accepted(addbooks.php)is given bellow.
/*addbooks.php*/
<?php
//require 'connect.php';
//require 'newEmptyPHP.php';
//require 'filename.php';

?>
<html>
    <body><form name="form1" method="post" action="add.php">  //call to addphp
            <label>
                    Name of Book
                    <input type="text" name="name"/>  //Accepting book details
                    <br>
                    Author 
                    <input type="text" name="author"/>   //Accepting book details
                    <br>
                    Publication 
                    <input type="text" name="publication"/>  //Accepting book details
                    <br>
                    Price 
                    <input type="text" name="price"/>   //Accepting book details
                    <br>
                    Stock 
                    <input type="text" name="stock"/>   //Accepting book details

                    <br>
                    submit   //submitting th datails
                    <input type="submit" name="Submit" value="Submit"/>

            </label>
        </form>
    </body>
</html>

You have to enclose the character values within quotes also no need of quotes for table name (Instead of quotes you can use backticks ` for tablename and column names in a query. And the values should be enclosed within quotes only).

 mysqli_query($con, "INSERT INTO `books` (book_name, book_author, publication, book_price, 
 book_stock) VALUES ('$book_name', '$book_author', '$book_publication', $book_price, 
 $book_stock)");

Remove the single quotes from books and it should work.

Also the best way to debug this kind of problem is store the sql query in the string and using echo and print the query. And then look what query it is forming and first try to directly execute it on mysql shell

 mysqli_query($con, "INSERT INTO books (book_name, book_author, publication, book_price, book_stock) VALUES ('{$book_name}', '{$book_author}', '{$book_publication}',$book_price, '{$book_stock}')");