【问题标题】:Uploading image and insering data into database上传图像并将数据插入数据库
【发布时间】:2015-07-05 00:16:44
【问题描述】:

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

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代码

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>";
    }
}

}

请帮忙!!!

【问题讨论】:

  • 停止使用 mysql_* 驱动程序不再受支持,它已被弃用,将来将被删除

标签: php mysql


【解决方案1】:

更改表单标签

来自

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

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

你需要

enctype='multipart/form-data

当您处理文件

更多参考

查看此链接Form-data explained

【讨论】:

  • enctype='multipart/form-data 应该有一个 close ' 吗?
  • 是的对不起我的错误我没有关闭它。请关闭它
  • enctype='multipart/form-data" 很有用。我的代码中还有另一个错误,我输入了
猜你喜欢
  • 2013-07-29
  • 1970-01-01
  • 1970-01-01
  • 2013-05-06
  • 1970-01-01
  • 1970-01-01
  • 2019-09-12
  • 2010-09-23
  • 2013-05-10
相关资源
最近更新 更多