在MySQL数据库中更新上传的图像URL

时间:2015-05-17 11:23:33

标签: php mysql

我想更新MySQL数据库WHERE UserName='$username'中的上传图片网址,但没有任何反应。

我已仔细检查过我的代码,但没有任何错误但仍然无效。

提示

  1. $user_name是会话。 $user_name = mysqli_real_escape_string($con, $_SESSION['UserName']);
  2. $con是连接
  3. Php代码

    function uploadImageFile($user_name) { // Note: GD library is required for this function
    
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $iJpgQuality = 100;
    
        if ($_FILES) {
            // if no errors and size less than 250kb
            if (! $_FILES['image_file']['error'] && $_FILES['image_file']['size'] < 250 * 1024) {
                if (is_uploaded_file($_FILES['image_file']['tmp_name'])) {
                    if (!is_dir('avatar')) {
                        mkdir('avatar');
                    }
                    // new unique filename
                    $sTempFileName = 'avatar/' . md5(time().rand());
                    // move uploaded file into cache folder
                    move_uploaded_file($_FILES['image_file']['tmp_name'], $sTempFileName);
    
                    // change file permission to 644
                    @chmod($sTempFileName, 0644);
    
                    $sResultFileName = copyImageFile('avatar/'.$user_name.'', $sTempFileName, 200, 200, $iJpgQuality);
                    if ($sResultFileName) {
    
                    $con = mysqli_connect("localhost","root","","ara") or die("Error " . mysqli_error($link));
    
                    $avatar1=copyImageFile('avatar1/'.$user_name.'', $sTempFileName, 500, 500);
                    $avatar2=copyImageFile('avatar2/'.$user_name.'', $sTempFileName, 700, 700);
    
                    mysqli_query($con,"UPDATE profiles SET (AvatarImage,AvatarImageBig,AvatarImageSmall) = ('".$sResultFileName."','".$avatar1."','".$avatar2."') WHERE UserName = '$user_name'");   
    
    
                        @unlink($sTempFileName);
    
                        return $sResultFileName;
                    }
                }
            }
        }
    }
    return false;
    }
    
    function copyImageFile($dirName, $originImageName, $iWidth, $iHeight, $iJpgQuality = 90) {
    if (file_exists($originImageName) && filesize($originImageName) > 0) {        
        $aSize = getimagesize($originImageName); // try to obtain image info
        if (!$aSize) {
            @unlink($originImageName);
            return;
        }
    
        // check for image type
        switch($aSize[2]) {
            case IMAGETYPE_JPEG:
                $sExt = '.jpg';
                $vImg = @imagecreatefromjpeg($originImageName);
                break;
            /*case IMAGETYPE_GIF:
                $sExt = '.gif';
    
                // create a new image from file 
                $vImg = @imagecreatefromgif($sTempFileName);
                break;*/
            case IMAGETYPE_PNG:
                $sExt = '.png';
                $vImg = @imagecreatefrompng($originImageName);
                break;
            default:
                @unlink($originImageName);
                return;
        }
    
        // create a new true color image
        $vDstImg = @imagecreatetruecolor( $iWidth, $iHeight );
    
        // copy and resize part of an image with resampling
        imagecopyresampled($vDstImg, $vImg, 0, 0, (int)$_POST['x1'], (int)$_POST['y1'], $iWidth, $iHeight, (int)$_POST['w'], (int)$_POST['h']);
    
        // define a result image filename        
        if (!is_dir($dirName)) {
            mkdir($dirName);
        }
        $newImageName = $dirName . DIRECTORY_SEPARATOR . md5(time().rand()) . $sExt;
    
        // output image to file
        imagejpeg($vDstImg, $newImageName, $iJpgQuality);
        //@unlink($sTempFileName);
    
        return $newImageName;
    }
    
    return false;
    }
    
    $user_name = $_SESSION['UserName'];
    
    $sImage = uploadImageFile($user_name);
    echo '<img src="'.$sImage.'" />';
    

1 个答案:

答案 0 :(得分:0)

您的数据库连接和查询脚本中存在错误:

$con = mysqli_connect("localhost","root","","ara") or die("Error " . mysqli_error($link));

这里有一个未声明的$ link变量。它应该是$ con。无论如何,如果您没有成功连接,则无法调用mysqli_error($ con)。您应该更好地测试您的连接,如:

$con = mysqli_connect("localhost","root","","ara");
// Check connection
if (mysqli_connect_errno()){
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
}

请参阅http://www.w3schools.com/php/func_mysqli_error.asp

您的更新查询应为:

"UPDATE profiles SET AvatarImage = '".$sResultFileName."', AvatarImageBig = '".$avatar1."',AvatarImageSmall = '".$avatar2."' WHERE UserName = '$user_name'"

而不是

,"UPDATE profiles SET (AvatarImage,AvatarImageBig,AvatarImageSmall) = ('".$sResultFileName."','".$avatar1."','".$avatar2."') WHERE UserName = '$user_name'"

当然,您必须检查mysqli中已经注释的错误。