当我执行此代码以加载图像并存储在数据库中时,它不存储。
$image=$_POST['image'];
$img = $_FILES['image']['name'];
if($img)
{
$imgnew = date("YmdHis").".".end(explode('.',$img));
move_uploaded_file($_FILES['image']['tmp_name'],$img_src.$imgnew);
$sql = "UPDATE news SET `image` = '".$imgnew."' WHERE `id` = '".$id."'";
$result = mysql_query($sql);
}
我的表单代码
<input type="file" name="image" value="<?php echo $row['image']; ?>" />
答案 0 :(得分:3)
exit()
答案 1 :(得分:1)
检查为您“做”某些事情的功能的结果非常重要。你想确保它在你向前推进之前确实已经完成了。 move_uploaded_file
和mysql_query
都可能失败。
如果他们这样做,他们会告诉你他们的返回值(通常是布尔值为FALSE)。捕获返回并做出适当反应。然后,如果出现问题,你不必挠头找出原因!
$img_src = '/assuming/some/path/';
$img = false;
$error = false;
if (
isset($_FILES['image']) &&
$_FILES['image']['error'] != 4
) {
$img = $_FILES['image'];
switch ($img['error']) {
case '1':
case '2':
case '3':
$error = 'The uploaded file exceeds the maximum file size.';
break;
case '6':
case '7':
case '8':
$error = 'Server error. Please try again later.';
break;
}
if (!$error) {
$imgnew = date("YmdHis").".".end(explode('.',$img['name']));
$result = move_uploaded_file($img['tmp_name'],$img_src.$imgnew);
if (!$result)
$error = 'File error while processing upload';
}
if (!$error) {
$sql = "UPDATE news SET `image` = '".$imgnew."' WHERE `id` = '".$id."'";
$result = mysql_query($sql);
if (!$result)
$error = mysql_error();
}
}
// do something useful with the error message, preferrably not this
if ($img && $error)
die('There was a problem updating the image: '.$error);
<强>文档强>
PHP的move_uploaded_file
- http://us2.php.net/manual/en/function.move-uploaded-file.php
PHP的mysql_query
- http://us2.php.net/manual/en/function.mysql-query.php