将图像和内容数据上传到数据库

时间:2015-07-04 15:57:59

标签: php mysql

我正在尝试将数据插入数据库并上传图像。图像名称将保存在数据库中,图像将保存在图像文件夹中,在其中创建随机文件夹。但是数据库中没有保存任何内容。

HTML代码

 <form action="news.php" method="post">
                    <label>Title</label>
                    <input type="text"  placeholder="Title" class="form-control" name="title" required><br/><br/>

                    <label>Description-1</label>
                    <textarea class="form-control" rows="4" cols="50" name="desc1" required></textarea><br/><br/>

                    <label>Description-2</label>
                    <textarea class="form-control" rows="4" cols="50" name="desc2"></textarea><br/><br/>

                    <label>Description-3</label>
                    <textarea class="form-control" rows="4" cols="50" name="desc3"></textarea><br/><br/>

                    <label>Description-4</label>
                    <textarea class="form-control" rows="4" cols="50" name="desc4"></textarea><br/><br/>

                    <label>Image</label>
                    <input type="file" name="Image" id="ImageFile" class="form-control">

                    <br/><br/>
                    <button type="button" name="submit" class="btn btn-success">Submit</button>
                </form>

php code

if (isset($_REQUEST['submit'])) {

$title = $_POST['title'];
$desc1 = $_POST['desc1'];
$desc2 = $_POST['desc2'];
$desc3 = $_POST['desc3'];
$desc4 = $_POST['desc4'];
$Image = $_POST['Image'];

$folder_path = 'Images/';

$imagename = basename($_FILES['Image']['name']);
$newname2 = $folder_path . $imagename;

if ((@move_uploaded_file($_FILES['Image']['tmp_name'], $newname2))) {
    $query = "INSERT INTO tbl_news(title,desc1,desc2,desc3,dessc4,image) VALUES('{$title}','{$desc1}','{$desc2}','{$desc3}','{$desc4}','{$imagename}')";
    $result = mysql_query($query,$con);
    if ($result) {
        echo "<script type='text/javascript'>alert('Submiited Successfully')</script>";
    }
}

}

请帮助!!!

1 个答案:

答案 0 :(得分:1)

更改表单标记

来自

 <form action="news.php" method="post">

<form action="news.php" method="post" enctype='multipart/form-data' >

你需要

  

enctype =&#39; multipart / form-data

当您处理文件

以获取更多参考资料

  

点击此链接 Form-data explained