将变量发送到MySQL数据库

时间:2014-03-02 21:30:19

标签: php mysql

我一直在努力解决这个问题一段时间,我似乎无法理解如何找到问题的解决方案。我正在尝试发送图像文件路径以及当前用户会话ID。出于某种原因,我不断收到错误消息,说明两个POST变量是未识别的索引。这是代码:

if (move_uploaded_file($_FILES["image"]["tmp_name"], "Images Upload/" . $_FILES["image"]["name"])) {
    $target= "Images Upload/".$_FILES["image"]["name"];
    $target1= $_POST[$target];
    $sessionid= $_SESSION['user_id'];
    $sessionid1= $_POST[$sessionid];

    $query= "insert into images (UserID, Image) values('{$target1}','{$sessionid}')";

    if (mysql_query($query)) {
         echo"<script>alert('Upload Successful!')</script>";
    } else {
         echo "this isn't working";
    }           
} else {
    echo "this isn't working";
}

2 个答案:

答案 0 :(得分:0)

我认为你对$ _POST的目的感到困惑。 $ _POST变量填写了提交html表单。所以这一行:

$target1= $_POST[$target];

正在尝试通过名称“Images Upload /".$_ FILES [”image“] [”name“]查找提交的表单输入。像这样:

<input type='text' name='Images Upload/example.png' value = 'someValue'>

因为它不存在会给出错误。并将target1设置为null。

只需删除这些行,它就可以正常工作;

$target1= $_POST[$target];
$sessionid1= $_POST[$sessionid];

并将此行更改为:

$query= "insert into images (UserID, Image) values('$target','$sessionid')";

答案 1 :(得分:0)

你想做什么:

$target1= $_POST[$target];

sessionid1= $_POST[$sessionid];

请在此处阅读{_SESSION和$ _POST http://www.php.net/manual/en/index.php

试试这个:

if(move_uploaded_file($_FILES["image"]["tmp_name"], "Images Upload/" . $_FILES["image"]["name"])){
$target= "Images Upload/".$_FILES["image"]["name"];
$sessionid= $_SESSION['user_id'];

$query= "insert into images (UserID, Image) values('$target','$sessionid')";

if(mysql_query($query)){
    echo"<script>alert('Upload Successful!')</script>";
    }
    else{echo "this isn't working";}

} else{echo "this isn't working";}

此外,您在目录名称“图片上传”中有“空格”,请尽量避免使用这些空格。

我希望你有个主意......