【问题标题】:Upload Image To Database Not Working [duplicate]将图像上传到数据库不起作用[重复]
【发布时间】:2017-10-28 11:12:37
【问题描述】:

我在 html 中创建一个表单,它将运行 php 文件并在数据库中上传图像和文本。我的 html 完美地指向 php,但上传过程不起作用。每次按下提交按钮时,php 文件显示不起作用.

我的 HTML 代码:

<form name="f1" method="post" action="php/Savent.php" 
 enctype="application/x-www-form-urlencoded">
    <fieldset>
        <legend name = "addev" align="right"><b>Detail</b></legend>
        <table width="100%">
            <tr align="center">
                <th>Choose Image : </th>
                <td><input type="file" name="image"/></td>
            </tr>
            <tr>
                <td colspan="2"><br/></td>
            </tr>
            <tr align="center">
                <th>Description : </th>
                <td><textarea name="desc" rows="6" cols="30" style="resize: 
                     none"></textarea></td>
            </tr>
            <tr>
                <td colspan="2"><br/></td>
            </tr>
            <tr align="center">
                <td colspan="2" align="center"><input name="submit" 
                  type="submit" value="Submit"/> <input type="reset" 
                  value="Reset"/></td>
            </tr>
            <tr>
                <td colspan="2"><br/></td>
            </tr>
        </table>

PHP 代码:

 <?php

  if(isset($_POST["submit"])){
    mysqli_connect("sql303.unaux.com","unaux_20153623","testin");
    mysqli_select_db("unaux_20153623_dummy");

    $imageName = mysqli_real_escape_string($_FILES["image"]["name"]);
    $imageData = 
    mysqli_real_escape_string(file_get_contents($_FILES["image"]
    ["tmp_name"]));
    $imageType = mysqli_real_escape_string($_FILES["image"]["type"]);
    $desc = mysqli_real_escape_string($_POST["desc"]);

    if (substr($imageType,0,5) == "image"){
        echo "Working";
    mysqli_query("INSERT INTO 'events' 
    VALUES('','$imageName','$imageData','$desc')");
    echo "Saved Succesfully";
    }
    else{
    echo "Not Working";
    }
   }

   ?>

【问题讨论】:

  • 您使用了错误的编码类型。见stackoverflow.com/questions/4526273/…
  • 另外你需要在执行查询时传递你的连接变量。
  • 同样在您的插入语句中,您需要在表名周围使用反引号而不是单引号。确保事件表只有 4 列

标签: php html css database mysqli


【解决方案1】:

在您使用错误 enctype 的表单元素中添加此 enctype="multipart/form-data"

【讨论】:

  • 这只是其中的一部分。你错过了很多东西。
  • 是的,我看到他错过了连接的代码,他的查询也有错误,感谢您指出,因为在您发表评论后我看到了他的整个代码,但幸运的是他使用的是 mysqli 而不是 mysql
猜你喜欢
  • 1970-01-01
  • 2014-05-11
  • 1970-01-01
  • 2019-07-10
  • 1970-01-01
  • 1970-01-01
  • 2019-09-14
  • 2016-08-28
相关资源
最近更新 更多