首页 > 解决方案 > 图像上传到数据库后,除非刷新,否则网页无法正常加载

问题描述

我最近将我的一些代码更改为准备好的语句到一个表单,该表单基本上允许用户简单地上传图像并保存在数据库中。自从我对 SQL 查询进行了一些更改,按下上传按钮后,只有一半的网页重新加载。

图片上传前的网页: 图片上传前的网页

然后这是上传图片后的网页,屏幕的其余部分消失了: 图片上传后的网页

我检查了 MAMPS apache 错误日志,这是那里的最后一个日志 [Mon Mar 11 20:35:04 2019] [error] [client ::1] File does not exist: /Applications/MAMP/htdocs/PhotoClub/style.css, referer: http://localhost:8888/PhotoClub/after-login.php?login=success

这对我来说与 CSS 文件无关,因为指向此文件的链接位于页面顶部:

    <head> 
    <meta charset="utf-8" />
    <title>Dashboard</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta name="keywords" content="blog, tech, girl, techblog, gallery, coder, techblog, trends, fashion, beauty"/> 
    <link rel="stylesheet" type="text/css" href="CSS/style.css"/>
    <link href="https://fonts.googleapis.com/css?family=Lora" rel="stylesheet"/>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.1/css/all.css" integrity="sha384-gfdkjb5BdAXd+lj+gudLWI+BXq4IuLW5IT+brZEZsLFm++aCMlF1V92rMkPaX4PP" crossorigin="anonymous">
    <link href="CSS/lightbox.css" rel="stylesheet">
</head> 

奇怪的是,在错误日志中它似乎没有说出正确的文件路径名,即使它是:/Applications/MAMP/htdocs/PhotoClub/CSS/style.css它几乎就像它从文件路径中遗漏了 CSS?

表单的html代码:

<div class="grid-2">
                    <p><b>Upload photo entries here!</b></p>
                    <form action = "" method = "POST" enctype="multipart/form-data">
                        <label>Select Competition</label>
                        <select name="catID">
                          <option value="">Default</option>
                          <option value="1">Winter Warmer</option>
                          <option value="2">Fresh New Year</option>
                          <option value="3">Month of Love</option>
                          <option value="4">Seaside Scenery</option>
                        </select>
                    </fieldset>

                    <label>Enter Member ID</label>
                        <input type ="text" name ="member-id" placeholder="Enter Your Member ID...">
                        <label>Enter Title</label> 
                        <input type ="text" name ="img-title" placeholder="Enter Title...">
                      <table width="300" border="0" cellpadding="1" cellspacing="1" class="box">
                        <tr> 
                          <td width="246">
                            <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <!-- 2 Megabytes -->
                            <input name="userfile" type="file" id="userfile"> 
                          </td>
                          <td width="80">
                            <input name="upload" type="submit" id="upload" value="Upload "> <!-- A button -->
                          </td>
                        </tr>
                      </table>
                    </form>

php代码:

<?php
                        $uploadDir = 'images/';


                        if(isset($_POST['upload']))
                        {
                          $fileName = $_FILES['userfile']['name'];
                          $tmpName = $_FILES['userfile']['tmp_name'];
                          $fileSize = $_FILES['userfile']['size'];
                          $fileType = $_FILES['userfile']['type'];
                          $memberID = $_POST['member-id'];
                          $imgTitle = $_POST['img-title'];
                          $catID = $_POST['catID'];

                          $filePath = $uploadDir . $fileName;

                          $result = move_uploaded_file($tmpName, $filePath);

                          if (!$result) {
                            echo "Error uploading file";
                    exit;
                  }
                else{
                  echo "<br>Files uploaded<br>";
                }

                if(mysqli_connect_errno())
                {
                  printf("Connect failed: %s\n", mysqli_connect_error());
                    exit();
                }

                if(!get_magic_quotes_gpc())
                {
                $fileName = addslashes($fileName);
                $filePath = addslashes($filePath);
                } 


                $stmt = $conn->prepare ("INSERT INTO tblImage (fldImageID, fldMemberID, fldCatID, fldFilePath, fldName) VALUES (NULL, ?, ?, ?, ?)");

                $stmt->bind_param("iiss", $memberID, $catID, $filePath, $imgTitle); 
                $stmt->execute();
                $result = mysqli_stmt_get_result($stmt) or die ("");

                //2. $query = "SELECT `fldImageID` FROM `tblImage` ORDER BY `fldImageID` DESC LIMIT 1";
                //3. Then I need a query to update the membEntComp or comp Table
                }

                  ?>

标签: phphtmlcss

解决方案


$stmt = $conn->prepare ("INSERT INTO tblImage (fldImageID, fldMemberID, fldCatID, fldFilePath, fldName) VALUES (NULL, ?, ?, ?, ?)");

$stmt->bind_param("iiss", $memberID, $catID, $filePath, $imgTitle); 
$stmt->execute();
$result = mysqli_stmt_get_result($stmt) or die ("");

您不能mysqli_stmt_get_result()INSERT查询上使用,因为它仅适用于SELECT查询

因为它返回false,你的or die("")代码被触发并且你的脚本停止在那里执行。

相反,您可以检查返回值$stmt->execute()以查看查询是否成功执行。为避免下次混淆,我将在以下内容中添加更有用的错误消息die("")

$stmt = $conn->prepare ("INSERT INTO tblImage (fldImageID, fldMemberID, fldCatID, fldFilePath, fldName) VALUES (NULL, ?, ?, ?, ?)");

$stmt->bind_param("iiss", $memberID, $catID, $filePath, $imgTitle); 
$stmt->execute() or die("Failed to insert image into the database");

或者,更透明(但本质上是一样的):

$result = $stmt->execute();

if (!$result) {
    die("Failed to insert image into the database");
}

推荐阅读