尝试将图片目录插入MySQL数据库时出错

时间:2014-08-20 12:21:00

标签: php mysql sql

我有一个由用户提交的表单和一个php脚本将输入插入到我的表中,但是它不断返回错误,我能够将除picture目录之外的所有值插入到表中

这些是我的表值:

Field              Type             Null    
Name               Text             No
Picture            varchar(30)      No
Date               date             No
Price              int(11)          No

这是表格:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <link type="text/css" rel="stylesheet" href="stylesheet.css"/>

    </head>

<body>

<form method="post" action="addEvent.php" enctype="multipart/form-data">
    <p>
    Enter the Name of the Event:
      <input type="text" name ="Name"/>
    </p>
    <p>
    Picture:
      <input type="hidden" name="size" value="350000" />
      <input type="file" name="Photo" />
    </p>
    <p>
    Date:
      <input type="date" name="Date"/>
    </p>
    <p>
    Price
      <input type="number" name="Price"/>
    </p>
    <input type="submit" name="upload" title="Add data to the Database" value="Add   Event"/>
<br/>
<br/>
</form>

</body>
</html>

这是我用来将文件插入数据库的脚本,其中包含数据库删除ofc的用户名和密码。

<?php


$target = "public_html/events/Pictures";
$target = $target . basename( $_FILES['photo']['name']);


$name=$_POST['Name'];
$Picture=($_FILES['photo']['name']);
$Date=$_POST['Date'];
$Price=$_POST['Price'];



 mysql_connect("Host", "User", "pass") or      die(mysql_error()) ;
mysql_select_db("") or die(mysql_error()) ;


mysql_query("INSERT INTO Events (Name,Picture,Date,Price)
VALUES ('$name', '$Picture', '$Date', '$Price')") ;


if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{


echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and     your information has been added to the directory";
}
else {


echo "Sorry, there was a problem uploading your file.";
}
?>

该脚本目前返回错误“抱歉,上传文件时出现问题。”

有人能看出问题所在吗? 我要上传的目录是public_html / Events / Pictures

任何帮助将不胜感激, 感谢。

编辑: 更改了脚本,现在我收到了一个新错误。

<?php


$target = "public_html/events/Pictures/";
$target = $target . basename( $_FILES['Photo']['name']);


$name=$_POST['Name'];
$Picture=($_FILES['Photo']['name']);
$Date=$_POST['Date'];
$Price=$_POST['Price'];



 mysql_connect("mysql.hostinger.co.uk", "u219259269_admin", "password") or     die(mysql_error()) ;
mysql_select_db("u219259269_event") or die(mysql_error()) ;


mysql_query("INSERT INTO Events (Name,Picture,Date,Price)
VALUES ('$name', '$Picture', '$Date', '$Price')") ;


if(move_uploaded_file($_FILES['Photo']['tmp_name'], $target))
{


echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
  }
    else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>

警告:move_uploaded_file(public_html / events / Pictures / Test.jpg)[function.move-uploaded-file]:无法打开流:/home/u219259269/public_html/events/addEvent.php中没有此类文件或目录在第31行

警告:move_uploaded_file()[function.move-uploaded-file]:无法将'/ tmp / php79Pc7Q'移动到/ home / u219259269 / public_html / events / addEvent中的'public_html / events / Pictures / Test.jpg' .php在第31行 抱歉,上传文件时出现问题。

2 个答案:

答案 0 :(得分:0)

大多数情况下,问题发生是因为:

1)路径&#34; public_html / Events / Pictures&#34;不存在

2)PHP脚本无权将文件写入目录。

所以你可以先查看你的public_html / Events / Pictures。

答案 1 :(得分:0)

$Picture=($_FILES['photo']['name']);

这是区分大小写的,因此您需要使用&#34; Photo&#34;对于两者或&#34;照片&#34;

<input type="file" name="Photo" />