I'm trying to upload an image path along with some text to a MySQL database in PHP, how should I use the move_uploaded_file along with an SQL statement?
I've tried to move the move_uploaded file next to the statement. Though whatever I try, only the text will be saved in the database and not the image path. The code I have is this:
<?php
if (isset($_POST['listpost-submit'])) {
$target = "site_images/";
$target2 = $target. basename($_FILES['image_file']['name']);
$title = $_POST['title'];
$description = $_POST['description'];
$price = $_POST['price'];
$cat = $_POST['categories'];
$vendor = $_POST['vendor'];
elseif (move_uploaded_file($_FILES['image_file']['name'], $target2)) {
//Database connection
require 'dbh2.inc.php';
$sql = "INSERT INTO listings
(`image`,`title`,`description`,`price`,`category`,`vendor`)
VALUES (?, ?, ?, ?, ?, ?)";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../listing1.php?error=sqlerror");
exit();
}
else {
mysqli_stmt_bind_param($stmt, "ssssss", $image, $title,
$description, $price, $cat, $vendor);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
header("Location: ../index.php?listing=posted");
exit();
}
}
else {
header("Location: ../listing1.php?file=notuploaded");
exit();
}
}
else {
header("Location: ../listing1.php");
exit();
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
I expect all of my database columns to be filled with the given information but, the only thing missing is, of course, the image column and I want to know why that is.