我是mysql和php的初学者。在这个问题上陷入了困境。不知道问题出在哪里。但是如果我直接执行insert查询,它就会被执行,而如果我从用户那里接受它,它就不会被执行(代码中显示了这一点)。可能问题出在我用来检索用户提交的值的$\u 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>
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}')");
从书中删除单引号,它应该会起作用。
另外,调试此类问题的最佳方法是将sql查询存储在字符串中,并使用echo打印查询。然后看看它正在形成什么查询,首先尝试在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)");
事情是这样的 我不知道发生了什么,如果有人能帮忙,我会非常感激的。THX!
但是得到 jspException(jspservletwrapper.java:568)org.apache.jasper.servlet.jspservletwrapper.java:455)org.apache.jasper.server.jspserver.servicejjspfile(jspservlet.java:390)org.apache.jasper.server.jspser
问题内容: 我使用Google Maps API制作了一个非常简单的页面,其中有几个字段,用户将在其中放置一些数据。看起来像- http://aiworker2.usask.ca/marker_field_db.html 我要做的是使用javascript / Ajax将数据存储到MySQL数据库中。我发现了几个使用Jquery的示例。我是这个javascript / Ajax / Jquery平
//我试图创建一个注册表单(html),但在我单击按钮后,php文件似乎不能正确执行,web浏览器显示了上面的php代码,我想知道php有什么问题
我尝试从本地磁盘(Windows 10)向我的RDS MySQL数据库插入一个blob。然而,我的查询没有插入blob。新行包含所有其他列,但blob为空。 以下是我尝试过的: 我设置了文件的读/写权限 安全文件设置为/tmp。。。我不知道这是什么意思。这是路径还是变量?如果它是一个变量,我已经尝试从用户/系统变量“TMP”的路径上传blob,但没有成功。我无法更改安全文件,因为RDS允许更改。。