duandang6352 2014-09-22 11:31
浏览 20
已采纳

无法使用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>
  • 写回答

3条回答 默认 最新

  • dongya4335 2014-09-22 11:33
    关注

    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)");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 nslt的可用模型,或者其他可以进行推理的现有模型
  • ¥15 arduino上连sim900a实现连接mqtt服务器
  • ¥15 vncviewer7.0安装后如何正确注册License许可证,激活使用
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并2
  • ¥66 关于人体营养与饮食规划的线性规划模型
  • ¥15 基于深度学习的快递面单识别系统
  • ¥15 Multisim仿真设计地铁到站提醒电路
  • ¥15 怎么用一个500W电源给5台60W的电脑供电
  • ¥15 请推荐一个轻量级规则引擎,配合流程引擎使用,规则引擎负责判断出符合规则的流程引擎模板id
  • ¥15 Excel表只有年月怎么计算年龄