简体   繁体   English

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

[英]Unable to insert data into database(mysql) using php

I am a beginner at both mysql and php. 我是mysql和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. 可能是我正在使用$ _POST []方法来检索用户提交的值。 I have submitted both the codes, addbooks.php(form from which user submits values) and add.php (to insert into the database). 我已经提交了这两个代码,即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>

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. 调试此类问题的最佳方法是将sql查询存储在字符串中,并使用echo和打印查询。 And then look what query it is forming and first try to directly execute it on mysql shell 然后查看它正在形成什么查询,然后首先尝试在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}')");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM