0

我正在使用这个 php 照片上传脚本将图像存储到我的数据库中。

我的数据库有三列:'user_id''name''image'

一切正常。图片已上传,但目前它不存储 user_id,我想尝试制作它,以便当用户点击上传时,它使用他们的会话 ID 来存储他们的 user_id。

通过将这两个函数添加到原始 mysql 查询中,我尝试了一下:

(user_id, image, name) '".$_SESSION['user_id']."',

<html>
    <head>
        <title>Upload Image</title>
    </head>
    <body>    
        <form method="post" enctype="multipart/form-data">
            <input type="file" name="image">
            <input type="submit" value="Upload">
        </form>    
<?php        
    include("includes/_config/connection.php");

    // file properties
    $file = $_FILES['image']['tmp_name'];

    if (!isset($file))
        echo "";    
    else {      
        $image = addslashes (file_get_contents ($_FILES['image']['tmp_name']));
        $image_name = addslashes ($_FILES['image']['name']);
        $image_size = getimagesize($_FILES['image']['tmp_name']);

        if ($image_size == FALSE)
            echo "That's not an image.";        
        else {      
            if (!$insert = mysql_query ("INSERT INTO  ptb_img_uploads (user_id, image, name) VALUES ('', '".$_SESSION['user_id']."', '$image_name','$image')"))    
                echo "There was a problem sending the image.";      
            else    
                echo "Your image was successfully uploaded.";    
        }    
    }    
?>    
    </body>
</html>
4

3 回答 3

0
if(!$insert = mysql_query ("INSERT INTO  ptb_img_uploads (user_id, image, name) VALUES ('".$_SESSION['user_id']."', '$image', '$image_name')"))
于 2012-12-16T14:33:19.647 回答
0

您必须先开始会话。会话开始();。之后 echo $insert; 并将查询粘贴到mysql中并检查错误。

于 2012-12-16T14:51:36.867 回答
0

您忘记启动 PHP session_start(),它会启动会话函数。您还必须在某处设置 $_SESSION['user_id'] 。

更改为此代码:

<?php
session_start();
?>
<html>
<head>
    <title>Upload Image</title>
</head>
<body>

<form method="post" enctype="multipart/form-data">
    <input type="file" name="image">
    <input type="submit" value="Upload">
</form>

<?php
include("includes/_config/connection.php");

// file properties
$file = $_FILES ['image'] ['tmp_name'];

if (!isset($file))
{
    echo "";
}
else
{
    $image = addslashes (file_get_contents ($_FILES ['image']['tmp_name']));
    $image_name = addslashes ($_FILES['image']['name']);
    $image_size = getimagesize($_FILES['image']['tmp_name']);

    if($image_size == FALSE)
    {
        echo "That's not an image.";
    }
    else
    {
        if(!$insert = mysql_query ("INSERT INTO  ptb_img_uploads (user_id, image, name) VALUES ('".$_SESSION['user_id']."', '$image_name','$image')"))
        {
            echo "There was a problem sending the image.";
        }
        else
        {
            echo "Your image was successfully uploaded.";
        }
    }

}
?>
</body>
</html>

如果这不起作用,您可以在 session_start 上方添加这个:

session_save_path("tmp");

请记住使用 chmod 777 在您的目录中创建一个名为“tmp”的文件夹。

如果您想测试您的会话是否有效,请执行以下操作:

<?php
session_start();

$_SESSION['test_session']="this is a test session";

var_dump($_SESSION);
?>

希望能帮助到你!

于 2012-12-16T14:19:27.900 回答