使用图像信息更新表格时,从HTML表单上传带有其他表条目的图像

时间:2014-03-27 15:15:09

标签: php html mysql

我有一个HTML表单,它带有两个条目和一个图像。我试图将图片上传到"目标"文件夹,然后在我的SQL数据库中放置一个超链接到图像以供将来检索,同时还从表单中添加我的其他2个条目。我已经遵循了一些教程,但我似乎仍然无法使用它。目前mySQL正在为日期和条目工作。没有任何内容输入到图片表中,图片也没有上传到目标。

HTML代码

<table border="0" cellpadding="3" cellspacing="1">
    <form enctype="multipart/form-data" action="mob_create.php" method="post">
        <tr>
            <td width="150">Date:</td></tr><tr>
            <td><input type="date" name="inputDate" value="" /> </td>
        </tr>
        <tr>
            <td>Entry:</td></tr><tr>
            <td width="300"><input size="34" type="text" name="inputEntry" value="" /></td>
        </tr>
        <tr>
            <td width="150">Add an image (Optional):</td></tr><tr>
            <td><input type="file" name="inputPic" accept="image/*" /></td></tr>
        <tr>
            <td><input type="submit" name="submit" /></td>
        </tr>
    </form>
</table>

PHP代码

session_start();
include 'includes/Connect.php';

$target = "http://www.aam.prettypottery.ie/upload/"; 
$target = $target . basename( $_FILES['inputPic']['name']);

$date = $_POST['inputDate'];
$entry = $_POST['inputEntry'];
$picture = ($_FILES['inputPic']['name']);
$username=$_SESSION['myusername'];

if(!$_POST['submit']) {
    echo "Please complete all entries of the form";
} else {
    mysql_query("INSERT INTO `$username` (`Date`,`Entry`,`Picture`) VALUES('$date','$entry','$picture')") or die(mysql_error());
    if(move_uploaded_file($_FILES['inputPic']['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."; 
    } 
}
?>

1 个答案:

答案 0 :(得分:0)

目标的路径必须是本地的。

还要确保目标中的文件夹是可写的。

session_start();
include 'includes/Connect.php';

$target = "upload/"; 
$target = $target . basename( $_FILES['inputPic']['name']);

$date = $_POST['inputDate'];
$entry = $_POST['inputEntry'];
$picture = ($_FILES['inputPic']['name']);
$username=$_SESSION['myusername'];

if(!$_POST['submit']) {
echo "Please complete all entries of the form";
} else {
mysql_query("INSERT INTO `$username` (`Date`,`Entry`,`Picture`) VALUES('$date','$entry','$picture')") or die(mysql_error());
if(move_uploaded_file($_FILES['inputPic']['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."; 
} 
}
?>