将图像上传到文件和信息到mysql数据库

时间:2014-01-09 15:30:35

标签: php mysql

这是我的表格

<h3>Add Image</h3>
<form action="datauploadprocess.php" enctype="multipart/form-data" name="myForm" id="myForm" method="post">
Subject: <input name="subject" type="text" id="subject"  /><br />
Content: <input name="content" type="text" id="content"  /><br />
Image Name: <input name="imageName" type="text" id="imageName" /><br />

Upload Image (jpg only):
<label>
    <input type="file" name="fileField" id="fileField" />
</label>
<label>
    <input type="submit" name="button" id="button" value="Add Photo" />
</label>
</form>

和我的过程...

<?php include "header.php";?>
<?php
//Parser for Add Photo Form
if (isset($_POST["subject"], $_POST["content"], $_POST["imageName"])){
$subject = mysqli_real_escape_string ($con, isset ($_POST["subject"]));
$content = mysqli_real_escape_string ($con, isset ($_POST["content"]));
$imageName = mysqli_real_escape_string ($con, isset ($_POST["imageName"]));

//add photo to db
$sql = mysqli_query($con, "
           INSERT INTO
            Blog (
            subject,
            content,
            imageName
           )VALUES(
            '$subject',
            '$content',
            '$imageName')")
            or die(mysqli_error($con));
$pid = mysqli_insert_id();
$newname = "$pid.jpg";
move_uploaded_file($_FILES['fileField']['tmp_name'],"blogPhotos/$newname");
header("location: datauploadprocess.php");
exit();
}
?>
 <?php include"footer.php"?>

它运行时没有错误,但在数据库中,主题,内容和图像名称的值每次都显示为“1”。此外,图像似乎没有添加到文件夹中。 我的连接包含在      格式正确。

编辑15:45 主题,内容和图像名称问题现已解决,但图像仍然无法上传到指定的文件。 新流程代码;

//Parser for Add Photo Form
if (isset($_POST["subject"], $_POST["content"], $_POST["imageName"])){
$subject = mysqli_real_escape_string ($con, ($_POST["subject"]));
$content = mysqli_real_escape_string ($con, ($_POST["content"]));
$imageName = mysqli_real_escape_string ($con, ($_POST["imageName"]));

...更新 我错过了该行的连接信息

$pid = mysqli_insert_id();

应该是

$pid = mysqli_insert_id($con);

现在全部工作。

2 个答案:

答案 0 :(得分:4)

应该是,删除isset()函数,其结果是布尔值

$subject = mysqli_real_escape_string ($con, $_POST["subject"]);
$content = mysqli_real_escape_string ($con, $_POST["content"]);
$imageName = mysqli_real_escape_string ($con, $_POST["imageName"]);

答案 1 :(得分:1)

$subject = mysqli_real_escape_string ($con, isset ($_POST["subject"]));

此行中的isset()将返回true / false。它返回true,然后被解释为1。

从转义函数中删除isset(),你想要转义值,而不是isset()的结果