gpt4 book ai didi

php - 图片上传到数据库后,除非刷新,否则网页无法正确加载

转载 作者:太空宇宙 更新时间:2023-11-04 06:25:12 25 4
gpt4 key购买 nike

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

图片上传前的网页: Webpage before an image is uploaded

然后这是上传图片后的网页,屏幕的其余部分就消失了: Webpage after an image is uploaded

我检查了 MAMPS apache 错误日志,这是那里的最后一个日志[Mon Mar 11 20:35:04 2019] [error] [client::1] 文件不存在:/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
}

?>

最佳答案

$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 ("");

您不能在 INSERT 查询中使用 mysqli_stmt_get_result(),如 it only works for SELECT queries .

由于它返回 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");
}

关于php - 图片上传到数据库后,除非刷新,否则网页无法正确加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55149727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com