检查为您“做”某事的函数的结果很重要。你想确保它确实完成了,然后再假设这样做。两者都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