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 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab