【问题标题】:The Uploaded Image doesn't Save on the database上传的图像未保存在数据库中
【发布时间】:2017-01-04 02:51:33
【问题描述】:

我有一个上传图片的按钮,每次我上传图片时,图片都不会保存在数据库中,并且没有给出错误,

这是我的代码:

<td>
  <form method="POST" enctype="multipart/form-data">
    <input type="file" name="file" required>
        <button type="submit" name="files" class="btn btn-primary btn-xs">
            Submit
        </button>
        <?php
            if(isset($_POST['files']))
              {
                $userid = $row['stall_id'];
                $a = $_FILES['file']['name'];
                $ab = $_FILES['file']['tmp_name'];
                $location = "".$a;
                move_uploaded_file($ab, "../pictures/".$location);

                $sql2 = $conn->prepare("UPDATE stall SET file = ? WHERE stall_id = ?");
                $sql2->execute(array($location,$userid));

            if($sql){                                                   
                echo '
                <script>                                                        
                    window.location = "stalls.php"
                </script>';                                                 
                    }
                    }
                ?>
</td> 

【问题讨论】:

  • 文件真的上传了吗?
  • @Dagon 它只提交但不保存在数据库中。
  • 是的,但是文件,上传了吗?
  • 你使用了错误的变量,不知道你为什么使用$location = "".$a;
  • 我也没有看到结束 &lt;/form&gt; 标记。加上&lt;form&gt; 不能是&lt;table&gt; 的孩子。您有错误和一些未知数。

标签: php html sql


【解决方案1】:

我自己想通了,

<td>
<form method="POST" enctype="multipart/form-data">
    <input type="file" name="file" required>
    <input type="hidden" name="stall_id" value="<?php echo $value['stall_id']?>">
    <button type="submit" name="files" class="btn btn-primary btn-xs">
    Submit
    </button>
    <?php
    if(isset($_POST['files']))
    {
    $userid = $_POST['stall_id'];
    $a = $_FILES['file']['name'];
    $ab = $_FILES['file']['tmp_name'];
    $location = "".$a;
    move_uploaded_file($ab, "../pictures/".$location);

    $sql2 = $conn->prepare("UPDATE stall SET file = ? WHERE stall_id = ?");
    $sql2->execute(array($location,$userid));

    if($sql2){                                                   
    echo '
    <script>                    
    window.location = "stalls.php"
    </script>';                                                 
    }
    }
    ?>
    </td>

【讨论】:

    【解决方案2】:
    1. 请在 Prepare 语句后绑定参数。检查文档-http://php.net/manual/en/mysqli.prepare.php
    2. 在 if 条件中放入 $update(return value of execution) 而不是 sql
    3. 关闭&lt;/form&gt;标签。
    4. 检查所有有数据的变量。使用var_dump()、print_r() 或echo() 方法检查。

      <td>
      <form method="POST" enctype="multipart/form-data">
      <input type="file" name="file" required>
        <button type="submit" name="files" class="btn btn-primary btn-xs">
          Submit
       </button>
      </form>
      </td>
      <?php
          if(isset($_POST['files']))
            {
              $userid = $row['stall_id'];
              $a = $_FILES['file']['name'];
              $ab = $_FILES['file']['tmp_name'];
              $location = "".$a;
              move_uploaded_file($ab, "../pictures/".$location);
      
              if( $sql2 = $conn->prepare("UPDATE stall SET file = ? WHERE stall_id = ?"))
              {
      
                  $sql2->bindParam(1, $location, PDO::PARAM_STR);
                  $sql2->bindParam(2, $userid, PDO::PARAM_INT);
      
                  $update=$sql2->execute();
                  if($update){                                                   
                       echo '
                       <script>                                                        
                          window.location = "stalls.php"
                       </script>';                                                 
                   }
              }else{
                  echo 'sql prepare failed';
              }
           }
      ?>
      

    【讨论】:

    • 致命错误:在第 65 行调用 C:\xampp\htdocs\VCCDMStry\pages\stalls.php 中未定义的方法 PDOStatement::bind_param()
    • 请检查$conn你有正确的连接吗?
    • 是的,我有一个正确的连接,我认为问题是它无法更新已上传的图像变量 a 尚未被调用?
    • 检查准备方法是否正常工作!我编辑了代码。看看吧。
    • OP 使用的是PDO,而不是mysqli。绑定正确完成。
    【解决方案3】:

    改变

    $sql2 = $conn->prepare("UPDATE stall SET file = ? WHERE stall_id = ?");
    $sql2->execute(array($location,$userid));
    

    到

    $sql2 = $conn->prepare("UPDATE stall SET file = ? WHERE stall_id = ?");
    $sql2->bindParam(1, $location, PDO::PARAM_STR);
    $sql2->bindParam(2, $userid, PDO::PARAM_INT);
    $sql2->execute();
    

    【讨论】:

    • 好的,由于更新删除了mysqli 语法问题,我将删除以前的 cmets。那么,为什么现在将 location 限制为 12 个字符?在您改进之前的 OPs 代码有什么问题?
    • 检查我没有使用 12
    • 实际上我们提供了替代,因为根据他的评论连接已经建立,所以不需要检查它
    猜你喜欢
    • 2013-10-22
    • 2014-08-09
    • 2018-09-05
    • 2015-04-15
    • 2021-03-08
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多