Reputation: 476
I recently altered some code of mine into a prepared statement to a form which essentially allows a user to simply upload an image and save in the database. Ever since I made some changes to the SQL query, after the upload button is pressed, only half of the webpage reloads again.
Webpage before an image is uploaded:
Then this is the webpage after an image is uploaded, the rest of the screen just dissapears:
I checked MAMPS apache error log and this is the last log that's on there
[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
Which doesn't make any sense to me with it to do with the CSS file as the link to this is at the top of the page:
<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>
The weird thing is that in the error log it doesn't seem to say the correct file path name even which is: /Applications/MAMP/htdocs/PhotoClub/CSS/style.css
It's almost like it's missing out the CSS from the file path?
The html code for the form:
<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>
The php code:
<?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
}
?>
Upvotes: 0
Views: 55
Reputation: 11328
$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 ("");
You cannot use mysqli_stmt_get_result()
on an INSERT
query, as it only works for SELECT
queries.
Since it returns false
, your or die("")
code is fired and your script stops executing there.
Instead, you can check the return value for $stmt->execute()
to see if the query was executed successfully. To avoid confusion the next time, I'd add a more useful error message within the 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");
Or, more transparent (but essentially the same thing):
$result = $stmt->execute();
if (!$result) {
die("Failed to insert image into the database");
}
Upvotes: 1