저는 mysql과 php 모두 초보자입니다. 그리고이 문제에 아주 심하게 붙어 있습니다. 문제가있는 곳을 잘 모릅니다. 하지만 만약 내가 삽입 쿼리를 직접 실행, 나는 그것을 사용자로부터 받아 들일 때 (그것은 코드에 표시됩니다) 실행됩니다. 아마도 문제는 $ _POST [] 메서드를 사용하여 사용자가 제출 한 값을 검색하는 것입니다. addbooks.php (사용자가 값을 제출하는 양식) 및 add.php (데이터베이스에 삽입) 코드를 모두 제출했습니다.PHP를 사용하여 데이터베이스 (mysql)에 데이터를 삽입 할 수 없습니다
//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>
변화'books' INTO 테이블 이름 COL 이름에 대한 작은 따옴표를 넣지'에 INSERT INTO 'books'' 그것을 실행하려고 어떤 쿼리 본다. –