-1

当我执行此代码以加载图像并存储在数据库中时,它不会存储。

$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']; ?>" />
4

2 回答 2

3
  1. exit()您在执行查询之前调用
  2. 有一个额外的括号(这可能只是一个错字或其他问题)
于 2012-06-15T17:44:19.487 回答
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

于 2012-06-15T17:57:47.260 回答