更新图像路径

时间:2013-09-25 01:45:38

标签: php mysql sql image

我是PHP编程的新手,我环顾四周但我仍然感到困惑。我正在尝试更新用户表中的图像路径,我不太清楚如何做到这一点。这是我将图像放入数据库的代码,它可以插入它,但我不知道如何在我的数据库中更新图像图片路径以使用新插入的图像而不是用户在他们创建帐户时选择。

// Make sure we didn't have an error uploading the image
($_FILES[$image_fieldname]['error'] == 0)
or handle_error("the server couldn't upload the image you selected.",
$php_errors[$_FILES[$image_fieldname]['error']]);

// Is this file the result of a valid upload?
@is_uploaded_file($_FILES[$image_fieldname]['tmp_name'])
 or handle_error("you were trying to do something naughty.  Shame on you!",
"upload request: file named " .
"'{$_FILES[$image_fieldname]['tmp_name']}'");

// Is this actually an image?
@getimagesize($_FILES[$image_fieldname]['tmp_name'])
 or handle_error("you selected a file for your picture " .
"that isn't an image.",
"{$_FILES[$image_fieldname]['tmp_name']} " .
"isn't a valid image file.");

// Name the file uniquely
$now = time();
while (file_exists($upload_filename = $upload_dir . $now .
'-' .
$_FILES[$image_fieldname]['name'])) {
$now++;
}

// Finally, move the file to its permanent location
@move_uploaded_file($_FILES[$image_fieldname]['tmp_name'], $upload_filename)
 or handle_error("we had a problem saving your image to " .
"its permanent location.",
"permissions or related error moving " .
"file to {$upload_filename}");

$insert_sql = "UPDATE users set user_pic_path WHERE user_id = $user_id =
replace(user_pic_path, '$upload_filename', '$upload_filename' );

//insert the user into the database
mysql_query($insert_sql);
</code>

编辑: 我错过了一个“我修复了,现在没有SQL错误,它将图片放入数据库但不替换数据库中的图像路径。我一直在搞乱$ insert_sql但它仍然没有更新使用新图像路径的数据库,我该怎么办?这是我的新更新代码:

<code>
$insert_sql = "UPDATE users WHERE user_id = $user_id set user_pic_path =
replace(user_pic_path, '$upload_filename', '$upload_filename')";
</code>

1 个答案:

答案 0 :(得分:-1)

在最后一行中,插入SQL测试:

$insert_sql = "UPDATE users WHERE user_id = $user_id set user_pic_path = replace(user_pic_path, '$upload_filename', '$upload_filename')";

// check the query
echo $insert_sql."<br />";

//insert the user into the database
mysql_query($insert_sql);

然后你可以观察你要运行的查询,在PHPMyAdmin中测试它,找出它应该是什么。它也适用于其他关键变量。更好的是,你应该写一个&#34; debug&#34;函数,记录服务器上文件中发生的事情,以便在发生错误时,您可以跟踪它的详细信息,包括每个文件中的关键变量值。