我的问题是我有一个表格应该将漫画书的信息保存到数据库中,它保存的信息是标题,描述等,它也是将漫画的图像上传到我的服务器。
现在它确实将图像上传到服务器,但是它没有将任何信息放到我的表中,而且简单的不知道为什么?
我相当新的PHP和mysql所以也许这是一个容易的问题,但我无法弄明白,我无法在网上找到答案。
我的表结构:
我的表单在我的index.php上,看起来像这样:
<form method="post" action="newcomic.php" enctype="multipart/form-data">
<p>Comic name:
<br><input type="text" name="title"/></p>
<p>Description of the comic:
<br><textarea name="description"></textarea></p>
<p>Publicer:
<br><input type="text" name="publicer" /></p>
<p>Image:
<br><input type="file" name="image" /></p>
<p>Price:
<br><input type="text" name="price" /></p>
<p><input type="submit" name="add" title="Add new comic to database" value="Add Comic"/></p>
</form>
我的newcomic.php文件看起来像这样
<?php
//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['image']['name']);
//This gets all the other information from the form
$title = $_POST['title'];
$description = $_POST['description'];
$publicer = $_POST['publicer'];
$image = ($_FILES['image']['name']);
$price = $_POST['price'];
// Connects to your Database
mysql_connect("localhost", "root", "root") or die(mysql_error()) ;
mysql_select_db("comic_express") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO products (id, title, description, publicer, image, price, status)
VALUES ('', '$title', '$description', '$publicer', '$image', '$price', '1')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
希望有人能帮助我:)。
答案 0 :(得分:1)
错误的sql语法。您正在尝试将空字符串放入id。
答案 1 :(得分:0)
您应该在查询执行中添加一些错误处理,以帮助查找正在发生的事情。
基本的mysql错误处理php会是这样的:
<?php
$link = mysql_connet(CREDS HERE);
$query = "YOUR QUERY HERE";
$result = mysql_query($query, $link);
if(!$result)
echo mysql_error();
else
//Query was successful, do whatever here
?>
您始终希望确保查询成功,即使您确信它会成功。
我相信Guarana也是正确的,只需取出id(如果正确设置表,id将自动生成)或者你需要实际插入id而不是空字符串。
希望这有帮助!
答案 2 :(得分:0)
你必须使用mysql_error()函数和你的查询。这样你就可以在sql查询字符串中找到弹出问题。
使用给定的编辑代码并尝试。
<?php
//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['image']['name']);
//This gets all the other information from the form
$title = $_POST['title'];
$description = $_POST['description'];
$publicer = $_POST['publicer'];
$image = ($_FILES['image']['name']);
$price = $_POST['price'];
// Connects to your Database
mysql_connect("localhost", "root", "root") or die(mysql_error()) ;
mysql_select_db("comic_express") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO products (title, description, publicer, image, price, status)
VALUES ('$title', '$description', '$publicer', '$image', '$price', '1')") or die(mysql_error()) ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
哦,是的,你在整数类型字段中插入空白值。检查它是否为自动增量的主键。如果是,请离开此栏表示您无需插入。