gpt4 book ai didi

php - 上传到Mysql数据库时出错

转载 作者:行者123 更新时间:2023-11-29 14:09:53 24 4
gpt4 key购买 nike

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

最佳答案

我认为问题出在这段代码中:

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();

嗯,不

再次查看您的代码后,我根本看不到您在哪里设置 $image_file 在 sql 中使用,我注意到您在发布我的答案后使用了返回的 mysql_insert_id ,抱歉因为那里的困惑。

我认为您需要先生成$image_file,然后再在sql中使用它,并在您希望使用返回的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 = $_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 语句中使用列名称,所以我使用了通用术语,因此您需要插入自己的列。

关于php - 上传到Mysql数据库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13679472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com