0

I am trying to add 2 images to one row, but getting errors. Did I do it the correct way, or is there a better way to get this done.

<?php
// Enable error reporting
ini_set('display_errors', 1);
error_reporting(E_ALL);

// Check if the form has been submitted
if (isset($_POST['submit'])) {
    // Connect to the database
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "kripto";
    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    ini_set('display_errors', 1);
    error_reporting(E_ALL);

    // Handle the first image upload
    if ($_FILES['image']['error'] == 0) {
        // Upload the first image
        $name = $_FILES['image']['name'];
        $temp_name = $_FILES['image']['tmp_name'];
        $location = "./assets/images/versekering/";
        if (move_uploaded_file($temp_name, $location.$name)) {
            // Insert the details into the database
            $description = $_POST['description'];
            $value = $_POST['value'];
            $query = "UPDATE versekering_images SET name=?, date_created=NOW(), image_location=?, description=?, value=?";
            $stmt = $conn->prepare($query);
            $value_param = $value;
            $stmt->bind_param("ssssi", $name, $location.$name, $description, $value_param);


            if ($stmt->execute() === true) {
                echo "First image uploaded successfully";
            } else {
                echo "Error: " . $stmt->error;
            }
        } else {
            echo "Error: Failed to move uploaded file.";
        }
    }
    // Handle the second image upload
    elseif ($_FILES['second_image']['error'] == 0) {
        // Upload the second image
        $name = $_FILES['second_image']['name'];
        $temp_name = $_FILES['second_image']['tmp_name'];
        $location = "./assets/images/versekering/";
        if (move_uploaded_file($temp_name, $location.$name)) {
            // Update the row with the second image's details
            $query = "UPDATE versekering_images SET second_image_location=?";
            $stmt = $conn->prepare($query);
            $stmt->bind_param("si", $location.$name);
            if ($stmt->execute() === true) {
                echo "Second image uploaded successfully";
            } else {
                echo "Error: " . $stmt->error;
            }
        } else {
            echo "Error: Failed to move uploaded file.";
        }
    }
    // Close the database connection
    $conn->close();
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Laai Prent</title>
    <title>Upload Files</title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
    <style>

</style>
</head>
<div class="pyramid">
        
</div>

<div class="container">
    <div class="box" style="--i:0;"></div>

    <form method="post" enctype="multipart/form-data" action="Inhoud_Laai.php">
        <label for="image">Kies 'n prent om te laai vir Huis Inhoud:</label>
        <input type="file" name="image">
        <label for="description">Beskrywing:</label>
        <input type="text" name="description">
        <label for="value">Waarde:</label>
    <input type="text" name="value" value="R ">
    <br>
    <label for="second_image">Kies 'n tweede prent om te laai vir Huis Inhoud:</label>
    <input type="file" name="second_image">
    <input type="submit" name="submit" value="Upload">
</form>
</div>

My table in sql have this columns id, name, value, description, date_created, image_location and second_image_location

I have tryed a few changes, but cannot seem to get it to work. his is the error Fatal error: Uncaught Error: Cannot pass parameter 3 by reference in C:\Mylinks\MyKripto\henadmin\Inhoud_Laai.php:34 Stack trace: #0 {main} thrown in C:\Mylinks\MyKripto\henadmin\Inhoud_Laai.php on line 34

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Frik STIK
  • 1
  • 3

0 Answers0