dsuikgi9199 2014-06-13 23:48
浏览 19
已采纳

在PHP中将图像位置保存到MySQL DB

Been exhausting myself again trying to see what I'm doing wrong. I'm trying to upload a photo to my page and save its location to the mysql using prepared statements. My code is as follows

<?php

// First we execute our common code to connection to the database and start the session
require("common.php");

// At the top of the page we check to see whether the email is logged in or not
if(empty($_SESSION['email']))
{
    // If they are not, we redirect them to the login page.
    header("Location: ../index.html");
    die("Redirecting to FrontPage");
}

$email = $_SESSION['email']['email'];

//Profile Image upload script
if(isset($_FILES['profilepic']))
{
    if((($_FILES["profilepic"]["type"]=="image/jpeg") || ($_FILES["profilepic"]["type"]=="image/png") || ($_FILES["profilepic"]["type"]=="image/gif")) && ($_FILES["profilepic"]["size"] < 1048576)) 
    {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $rand_dir_name = substr(str_shuffle($chars), 0, 15);
        mkdir("userdata/profile_pics/$rand_dir_name");

        if(file_exists("userdata/profile_pics/$rand_dir_name/".$_FILES["profilepic"]["name"]))
        {
            echo $_FILES["profilepic"]["name"]."Already exists";
        }
        else
        {
            move_uploaded_file($_FILES["profilepic"]["tmp_name"], "userdata/profile_pics/$rand_dir_name/".$_FILES["profilepic"]["name"]);

            $profile_pic_name = $_FILES["profilepic"]["name"];

            $queryProfilePic = "UPDATE db SET profilePic=:profile_Pic WHERE email = :email";

            $query_params = array(':profile_Pic' => $rand_dir_name/$profile_pic_name,':email' => $email);

            $stmt = $db->prepare($queryProfilePic);
            $result = $stmt->execute($query_params);

            //header("Location: profile.php");
        }
    }
    else
    {

    }   
}

?>

The image is uploaded to the randomly generated folder on the server however when I try to save the location of the file to the db I get a big goose egg.

  • 写回答

1条回答 默认 最新

  • drhib62644 2014-06-13 23:53
    关注

    You cannot divide these values:

    $rand_dir_name/$profile_pic_name
    

    I assume you want to concatenate them:

    $rand_dir_name . '/' . $profile_pic_name
    

    or you can use

    "$rand_dir_name/$profile_pic_name"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据