2013-09-25 68 views
-1

我对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> 
+0

所以,你没有任何计划或编码技巧,对吧?我敢打赌,有一些文件,你从哪里得到这些代码。 – djot

+1

你觉得'user_pic_path'代表什么? – djot

+0

我确实有一些编程技巧,我只是比较新的PHP和坚持这一点。我不确定要在$ insert_sql中放入什么来更新数据库中的映像路径。它可以很好地将图像放入数据库,但它不会替换路径,这正是我期望做的 – nikito2003

回答

-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中测试它,确定它应该是什么。对其他关键变量也是值得的。更好的是,您应该编写一个“调试”功能,记录服务器上文件中发生的情况,以便发生错误时可以跟踪其详细信息,包括每个文件中的关键变量值。

+0

我仍然得到一个SQL /内部服务器错误。我认为我的问题是更新代码,但我不知道如何去改变它以使其起作用 – nikito2003

+0

感谢您的帮助,我找到了答案。 – nikito2003