【发布时间】:2015-05-20 11:49:17
【问题描述】:
我有一个 PHP 表单,它应该将一本书添加到 SQLite 数据库中的书籍表中。表单提交了,但是一本书没有添加到我的数据库中。
<?php
session_start();
require("books.php");
require("layout.php");
$db=sqlite_open ("products.db", 0666, $error);
echo $header;
echo "<p>
<a href='./index.php'>Bookshop</a></p>";
echo "<h1> Add Books </h1>
<p>
<form action='' method='get' id='AddBook'>
Author: <input type='text' name='Author'><br>
Title: <input type='text' name='Title'><br>
Brief_Synopsis: <input type='text' name='Synopsis'><br>
ISBN_Number: <input type='text' name='ISBN'><br>
Publisher: <input type='text' name='Publisher'><br>
imgNumber (save img with this name under /img/): <input type='text' name='imgNum'><br>
Price: <input type='text' name='Price'><br>
Category 1: <input type='text' name='Cat1'><br>
Category 2: <input type='text' name='Cat2'><br>
<input type='submit' value='Submit' name='Submit'>
</form>
</p>";
if(isset($_POST['Submit'])){
$author = $_POST['Author'];
$title = $_POST['Title'];
$Synopsis = $_POST['Synopsis'];
$ISBN = $_POST['ISBN'];
$Publisher = $_POST['Publisher'];
$imgNum = $_POST['imgNum'];
$Price = $_POST['Price'];
$Cat1 = $_POST['Cat1'];
$Cat2 = $_POST['Cat2'];
sqlite_query($db,"INSERT INTO Books (Author, Title, Brief_Synopsis, ISBN_Number, Publisher, imgNumber, price, cat1, cat2) VALUES ('$_POST[Author]', '$_POST[Title]', '$_POST[Synopsis]', '$_POST[ISBN]', '$_POST[Publisher]', '$_POST[imgNum]', '$_POST[Price]', '$_POST[Cat1]', '$_POST[Cat2]')");
echo("Book Added!");
$dbh = null;
}
?>
为什么这段代码没有正确更新我的数据库?在我添加 if 语句之前,它每次加载页面时都会向数据库添加一本空书,但是现在它提交并重置表单,我的 URL 看起来正确但数据库没有添加项目。
【问题讨论】:
-
尝试在
sqlite_query之前打印 INSERT 查询 - 它显示什么吗? -
您的代码静默失败,因为您在表单中使用了 GET 方法,而您使用的是 POST 数组。我应该
post作为答案吗? 请原谅双关语 ;-) -
我不太关心sql注入,因为这是一个永远不会连接到互联网的项目,只托管在本地机器上。
-
不客气。我在下面发布了一个答案,您可以接受关闭。