【发布时间】:2013-11-27 22:26:17
【问题描述】:
我的问题是我有一个表单应该将有关漫画书的信息保存到数据库中,它保存的信息是标题、描述等,而且它正在将漫画的图像上传到我的服务器。
现在它确实将图像上传到服务器,但它没有将任何信息放入我的表中,简单不知道为什么?
我是相当新的 php 和 mysql,所以也许这是一个简单的问题,但我无法弄清楚,而且我无法在网上找到答案。
我的表结构:
- id - int(11)
- 标题 - varchar(50)
- 说明 - 文字
- publicer - varchar(50)
- 图像 - varchar(30)
- 价格 - int(10)
- 状态 - tinyint(1)
我的表单在我的 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.";
}
?>
希望任何人都可以帮助我:)
【问题讨论】:
-
你可以尝试输出这样的mysql错误:
mysql_query(...) or die(mysql_error());。我注意到的一件事是您正试图将一个字符串插入id,该字符串设置为 int(11)。如果id是一个自动递增的字段,请将其排除在您的INSERT语句之外。 -
如果您的
id列设置为AUTO_INCREMENT,请删除查询中的第一个条目。 (编辑)似乎我们同时输入了相同的评论哈哈@showdev -
所以我现在感觉有点傻,我按照你们说的做了,删除了id,还是不行,尝试了mqsql_error,结果是我尝试的图像名称上传更长的 30 个字符,它是 31 -.- 但无论如何。感谢大家的帮助。
标签: php mysql database forms insert