上传的图像不会保存在数据库中

时间:2017-01-04 02:51:33

标签: php html sql

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

这是我的代码:

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

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

  1. 准备好声明后请绑定参数。检查docs- http://php.net/manual/en/mysqli.prepare.php
  2. in if condition put $ update(返回执行值)而不是sql
  3. 关闭</form>代码。
  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';
            }
         }
    ?>
    

答案 2 :(得分:-1)

改变
$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();
相关问题