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 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码