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

时间:2014-09-22 11:31:43

标签: php mysql database post

我是mysql和php的初学者。而且非常严重地坚持这个问题。不确定问题出在哪里。但是,如果我直接执行插入查询,它会被执行,如果我从用户接受它不会(它显示在代码中)。可能问题在于我用来检索用户提交的值的$ _POST []方法。我已经提交了两个代码,addbooks.php(用户提交值的形式)和add.php(插入到数据库中)。

    //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>

3 个答案:

答案 0 :(得分:0)

您必须将字符值括在引号内,也不需要引用表名(而不是引号,您可以对查询中的表名和列名使用反引号。并且值应仅包含在引号内)。< / p>

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

答案 1 :(得分:0)

从书籍中删除单引号,它应该可以正常工作。

调试此类问题的最佳方法是将sql查询存储在字符串中并使用echo并打印查询。然后查看它正在形成的查询,并首先尝试在mysql shell上直接执行它

答案 2 :(得分:0)

 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}')");