php - 将个人资料图片上传一次(不是两次)到数据库中,并正确显示它们

标签 php mysql image mysqli

我正在尝试在我的网站上构建一个选项,用户可以在其中上传个人资料图片,到目前为止一切顺利,但是当我刷新页面时,数据会一次又一次保存在我的数据库(表)中

++ 图像没有显示,它只显示一个黑色方 block 。 感谢您的帮助,如您所见,我仍然是一个绝对的初学者,正在热切地学习

$images = (isset ($_FILES['images']['name']));
$paths= [];


if ($images)
{
    $saveto = "uploads/$user.jpg";
    move_uploaded_file($_FILES['images']['tmp_name'], $saveto);
    $typeok = TRUE;
    $paths[] = $saveto;
    $date1=date("Y-m-d h:m:s");

    $link=mysqli_connect("localhost","root","","vlindr");
    mysqli_select_db($link,"imageupload"); //database connectivity

    mysqli_query($link,"insert into images 
                                (images_id, images_path, submission_date) 
                        VALUES (NULL,'$user','$date1')")
        or die(mysqli_error($link));

    switch($_FILES['images']['type'])
    {
        case "image/gif":   $src = imagecreatefromgif($saveto); break;
        case "image/jpeg":  // Both regular and progressive jpegs
        case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break;
        case "image/png":   $src = imagecreatefrompng($saveto); break;
        default:            $typeok = FALSE; break;
    }

    if ($typeok)
    {
        list($w, $h) = getimagesize($saveto);

        $max = 150;
        $tw  = $w;
        $th  = $h;

        if ($w > $h && $max < $w) {
            $th = $max ;
            $tw = $max;
        }
        elseif ($h > $w && $max < $h) {
            $tw = $max / $h * $w;
            $th = $max;
        }
        elseif ($max < $w)           {
            $tw = $th = $max;
        }

        $tmp = imagecreatetruecolor($tw, $th);
        imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
        imageconvolution($tmp,  array(array(-1, -1, -1),
                                array(-1, 16, -1), 
                                array(-1, -1, -1)),
                         8, 0);
        imagejpeg($tmp, $saveto);
        imagedestroy($tmp);
        imagedestroy($src);
    }
}

最佳答案

请告诉我下面的代码是否有效。 我还推荐准备好的语句,尽管下面的代码不是最简洁的,但它可以完成工作!

$images = (isset ($_FILES['images']['name']));
$paths= [];


if ($images)
{
$saveto = "uploads/$user.jpg";
move_uploaded_file($_FILES['images']['tmp_name'], $saveto);
$typeok = TRUE;
$paths[] = $saveto;
$date1=date("Y-m-d h:m:s");

$link=mysqli_connect("localhost","root","","vlindr");
mysqli_select_db($link,"imageupload"); //database connectivity

//Checks if Image already exists in database
$stmt = $link->prepare("SELECT * FROM images WHERE images_path=(?)");
$stmt->bind_param("s",$saveto);
$stmt->execute();
$stmt->store_result();
//If the amount of rows returned is zero, the script will insert the image into the database
if($stmt->num_rows === 0) {

    //Inserts the image into the database
    $stmt2 = $link->prepare("INSERT INTO images (images_id,images_path,submission_date) VALUES (NULL,?,?)");
    $stmt2->bind_param("ss",$saveto,$date1);
    if($stmt2->execute()) {
        echo "Image is unique and uploaded to database";
    }

    switch($_FILES['images']['type'])
    {
        case "image/gif":   $src = imagecreatefromgif($saveto); break;
        case "image/jpeg":  // Both regular and progressive jpegs
        case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break;
        case "image/png":   $src = imagecreatefrompng($saveto); break;
        default:            $typeok = FALSE; break;
    }

    if ($typeok)
    {
        list($w, $h) = getimagesize($saveto);

        $max = 150;
        $tw  = $w;
        $th  = $h;

        if ($w > $h && $max < $w)
        {
            $th = $max ;
            $tw = $max;
        }
        elseif ($h > $w && $max < $h)
        {
            $tw = $max / $h * $w;
            $th = $max;
        }
        elseif ($max < $w)
        {
            $tw = $th = $max;
        }

        $tmp = imagecreatetruecolor($tw, $th);
        imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
        imageconvolution($tmp, array(array(-1, -1, -1),
            array(-1, 16, -1), array(-1, -1, -1)), 8, 0);
        imagejpeg($tmp, $saveto);
        imagedestroy($tmp);
        imagedestroy($src);

    }

}//end if
else {
    echo "Image Already Exists";
}//end else

关于php - 将个人资料图片上传一次(不是两次)到数据库中,并正确显示它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44576950/

相关文章:

java - 通过套接字以字节形式传输图像数据时,我不断收到 UTF 错误

java - 检查文件是否可以作为 BufferedImage 读取

php - PHP 中的多层......正确的方法?

PHP-FPM (7.4.2) 没有正常停止?

php - phonegap 构建 + ajax 调用不适用于 android

php - 如何使用 php 检查 mysql 是否有重复项

PHP 表单输入和 mySQL Null

c# - 在 C# 中从 MySQL 数据库复制粘贴到另一个程序的表单

mysql - 使用 SQLProvider 时出现 "CreateSemaphore"错误

javascript - html && css - 图像突出显示