我是初学,照着书上写的一段代码,但是无法插入记录到数据表当中。
哪位能帮我看一下什么地方出了问题? 非常感谢。Filename: newbook.html
<html>
<head>
<title>Book-O-Rama - New Book Entry</title>
</head><body>
<h1>Book-O-Rama - New Book Entry</h1> <form action="insert_book.php" method="post">
<table border="0">
<tr>
<td>ISBN</td>
<td><input type="text" name="isbn" maxlength="13" size="13"></td>
</tr>
<tr>
<td>Author</td>
<td> <input type="text" name="author" maxlength="30" size="30"></td>
</tr>
<tr>
<td>Title</td>
<td> <input type="text" name="title" maxlength="60" size="30"></td>
</tr>
<tr>
<td>Price $</td>
<td><input type="text" name="price" maxlength="7" size="7"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Register"></td>
</tr>
</table>
</form>
</body>
</html>Filename: insert_book.php
<html>
<head>
<title>book-o-rama - new book entry</title>
</head><body>
<h1>book-o-rama - new book entry results</h1>
<?php
// create short variable names
$isbn=$_POST['isbn'];
$author=$_POST['author'];
$title=$_POST['title'];
$price=$_POST['price'];if(!$isbn||!$author||!$title||!$price){
echo 'You have not entered all the required details. <br/>'.
'Please go back and try again.';
exit;
}if(!get_magic_quotes_gpc()){
$isbn=addslashes($isbn);
$author=addslashes($author);
$title=addslashes($title);
$price=doubleval($price);}$db=new mysqli('localhost','root','liang ye','books');if(mysqli_connect_errno()){
echo 'Error: could not connect to database. Please try again later. ';
exit;
}$query="insert into books values('". $isbn."', '".$author."','".$title."','".$price."')";
print "<br/>$query<br/>";$result=$db->query($query);if($result){
echo $db->affected_rows. ' book inserted into database.';
}else{
echo 'No record inserted';
}
$db->close();
?></body>
</html>Table: books
+--------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------+------+-----+---------+-------+
| isbn | char(13) | NO | PRI | NULL | |
| author | char(50) | YES | | NULL | |
| title | char(100) | YES | | NULL | |
| price | float(4,2) | YES | | NULL | |
+--------+------------+------+-----+---------+-------+
哪位能帮我看一下什么地方出了问题? 非常感谢。Filename: newbook.html
<html>
<head>
<title>Book-O-Rama - New Book Entry</title>
</head><body>
<h1>Book-O-Rama - New Book Entry</h1> <form action="insert_book.php" method="post">
<table border="0">
<tr>
<td>ISBN</td>
<td><input type="text" name="isbn" maxlength="13" size="13"></td>
</tr>
<tr>
<td>Author</td>
<td> <input type="text" name="author" maxlength="30" size="30"></td>
</tr>
<tr>
<td>Title</td>
<td> <input type="text" name="title" maxlength="60" size="30"></td>
</tr>
<tr>
<td>Price $</td>
<td><input type="text" name="price" maxlength="7" size="7"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Register"></td>
</tr>
</table>
</form>
</body>
</html>Filename: insert_book.php
<html>
<head>
<title>book-o-rama - new book entry</title>
</head><body>
<h1>book-o-rama - new book entry results</h1>
<?php
// create short variable names
$isbn=$_POST['isbn'];
$author=$_POST['author'];
$title=$_POST['title'];
$price=$_POST['price'];if(!$isbn||!$author||!$title||!$price){
echo 'You have not entered all the required details. <br/>'.
'Please go back and try again.';
exit;
}if(!get_magic_quotes_gpc()){
$isbn=addslashes($isbn);
$author=addslashes($author);
$title=addslashes($title);
$price=doubleval($price);}$db=new mysqli('localhost','root','liang ye','books');if(mysqli_connect_errno()){
echo 'Error: could not connect to database. Please try again later. ';
exit;
}$query="insert into books values('". $isbn."', '".$author."','".$title."','".$price."')";
print "<br/>$query<br/>";$result=$db->query($query);if($result){
echo $db->affected_rows. ' book inserted into database.';
}else{
echo 'No record inserted';
}
$db->close();
?></body>
</html>Table: books
+--------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------+------+-----+---------+-------+
| isbn | char(13) | NO | PRI | NULL | |
| author | char(50) | YES | | NULL | |
| title | char(100) | YES | | NULL | |
| price | float(4,2) | YES | | NULL | |
+--------+------------+------+-----+---------+-------+
我可以确认数据库连接正常、成功。
sql语句没有问题,因为我可以在MySQL的提示符下运行插入数据成功。
唯一的问题是通过页面无法插入数据记录,而且没有报错的信息。
if($result){
echo $db->affected_rows. ' book inserted into database.';
}else{
echo 'No record inserted';
}
我运行可以插入记录的。检查你输入的ISBN是不是已经存在。ISBN是主键,不能有重复,如果有重复肯定无法插入。
$db=new mysqli('localhost','root','liang ye','books');
问题应该是出在数据库连接字段上,楼主说没有出错提示,那说明账号,密码没错,那是不是数据库名选错了?
还是说本来你的表名和数据库名都是books?
book-o-rama - new book entry resultsinsert into books values ('111', '222','333',444)
No record inserted 注:我稍微修改了我的代码,最后的字段因为是float型,所以我去掉了引号,但还是不行。
另外,我跟你用的确实是同一个教程,是PHP与MySQL Web设计第三版
insert into books values('978-7-115-176', 'Me','PHP','5.5')
1 book inserted into database.
谢谢回复。
我的想法是这样,
[1]. 如果连接数据库没有问题,能不能说我的环境就不应该有问题?
[2]. 我在mysql的提示符下用页面代码中生成的sql可以成功地插入记录, 不知到这个能否表明我的数据库设置是没有问题的
[3]. 我能够确认我要插入的数据绝对没有跟现有数据冲突的地方,因为即使我把现有的全部记录都删除,仍然无法用我的代码插入新的记录。
$query = "INSERT INTO books VALUES ('111', '222', '333', '444');";
PS:记得把23行的exit去掉 = =