错误上传到Mysql数据库

时间:2012-12-03 08:20:33

标签: php mysql

我创建上传图片并将图片移动到上传文件夹。当有人上传图像 mysql_insert_id()将插入唯一ID(1,2,3,4,5等......)和图片扩展名(jpg,gif,png)时,我需要结果看起来像这样(1.jpg,2.gif,3.png,4.jpg,5.gif,6.png等......)

1 个答案:

答案 0 :(得分:1)

我认为问题在于这段代码:

mysql_query("INSERT INTO users VALUES('', '$image_file')") or die (mysql_error());
$image_id = mysql_insert_id();
$image_file = $image_id.'.'.$extension;

看起来好像你在创建之前在sql中使用$image_file。试试这个:

$image_file = $image_id.'.'.$extension;
mysql_query("INSERT INTO users VALUES('', '$image_file')") or die (mysql_error());
$image_id = mysql_insert_id();

嗯嗯

看了你的代码之后,我根本无法看到你在sql中设置$image_file的位置,我注意到你在发布我的答案后使用了返回的mysql_insert_id,对不起那里的混乱

我认为你需要在sql中使用它之前生成$image_file,并且在你希望使用返回的mysql_insert_id之后再次生成。{/ p>

$image_file = $_FILES['image']['name'];
mysql_query("INSERT INTO users VALUES('', '$image_file')") or die (mysql_error());
$image_id = mysql_insert_id();

同时

在重命名文件时,您还需要在创建记录后更新数据库中存储的图像名称:

$image_file = $_FILES['image']['name'];
mysql_query("INSERT INTO users VALUES('', '$image_file')") or die (mysql_error());
$image_id = mysql_insert_id();
$image_file = $image_id . '.' . $extension;
move_uploaded_file($_FILES["image"]["tmp_name"], "upload/".$image_file);
mysql_query("UPDATE users SET image_column = '{$image_file}' WHERE id_column = {$image_id}") or die(mysql_error());

因为你没有在sql语句中使用列名,所以我使用了通用术语,你需要插入自己的列。