JimmysNetwork
Legendary Coder
Hi all,
My goal: I am trying to have a system where users can upload an image, and it will save into
My problem: I can manually add an image to the database, and attach it to the users website in the table. I cannot however update the image when I try to do so in my edit module. I can provide the code below, maybe you can see something I am missing. I am getting the following error:
PHP logic for the edit module:
HTML Code:
Thank you in advance for any assistance.
My goal: I am trying to have a system where users can upload an image, and it will save into
../assets/uploads & my image area in the database (image varchar(255) latin1_swedish_ci Yes NULL ). I have also gave permissions for the files to be uploaded into the correct directory, and it is pulling images from there currently. My problem: I can manually add an image to the database, and attach it to the users website in the table. I cannot however update the image when I try to do so in my edit module. I can provide the code below, maybe you can see something I am missing. I am getting the following error:
[B]Fatal error[/B]: Uncaught Error: Call to a member function execute() on string in public_html/domain.com/dev/user/dashboard.php:108 Stack trace: #0 {main} thrown in [B]public_html/domain.com/dev/user/dashboard.php[/B] on line 108PHP logic for the edit module:
PHP:
if (isset($_POST['edit_website'])) {
$id = $_POST['website_id'];
$name = trim($_POST['name']);
$url = trim($_POST['url']);
$description = trim($_POST['description']);
// Handle image upload
$image = $_FILES['image']['name'];
$image_temp = $_FILES['image']['tmp_name'];
$image_error = $_FILES['image']['error'];
// Check if an image was uploaded
if ($image_error === 0) {
// Define the image upload directory
$upload_dir = '../assets/uploads'; // Ensure this directory exists and is writable
$image_path = $upload_dir . basename($image);
// Move the uploaded image to the specified directory
if (move_uploaded_file($image_temp, $image_path)) {
// Success
$image = basename($image); // Get the image filename (for database storage)
} else {
// Error uploading the image
$error = 'Error uploading the image. Please try again.';
}
} else {
// If no image was uploaded, keep the old image value
$image = null;
}
// Check if required fields are empty
if (empty($name) || empty($url) || empty($description)) {
$error = 'All fields are required to edit the website.';
} else {
// Prepare the SQL query to update the website
$query = "UPDATE websites SET name = :name, url = :url, description = :description";
// Only update the image if a new one is uploaded
if ($image) {
$query .= ", image = :image";
}
$query .= " WHERE id = :id AND user_id = :user_id";
$stmt = $pdo->prepare($query);
// Bind the parameters
$params = [
'name' => $name,
'url' => $url,
'description' => $description,
'id' => $id,
'user_id' => $user_id,
];
// Add the image parameter only if an image is provided
if ($image) {
$params['image'] = $image;
}
// Execute the statement
$stmt->execute($params);
// Success message
$success = 'Website updated successfully!';
}
}
HTML Code:
HTML:
<div class="modal fade" id="editWebsiteModal<?= $website['id'] ?>" tabindex="-1" aria-labelledby="editWebsiteModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST" action="dashboard.php" enctype="multipart/form-data">
<div class="modal-header">
<h5 class="modal-title" id="editWebsiteModalLabel">Edit Website</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<input type="hidden" name="website_id" value="<?= $website['id'] ?>">
<!-- Website Name -->
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" name="name" class="form-control" value="<?= htmlspecialchars($website['name']) ?>" required>
</div>
<!-- Website URL -->
<div class="mb-3">
<label for="url" class="form-label">URL</label>
<input type="url" name="url" class="form-control" value="<?= htmlspecialchars($website['url']) ?>" required>
</div>
<!-- Website Description -->
<div class="mb-3">
<label for="description" class="form-label">Description</label>
<textarea name="description" class="form-control" rows="3" required><?= htmlspecialchars($website['description']) ?></textarea>
</div>
<!-- Image Upload (optional) -->
<div class="mb-3">
<label for="image" class="form-label">Image</label>
<input type="file" name="image" class="form-control">
<small class="text-muted">Leave empty to keep the current image.</small>
</div>
<!-- Current Image Display (optional) -->
<?php if (!empty($website['image'])): ?>
<div class="mb-3">
<label class="form-label">Current Image</label>
<div>
<?php if (!empty($website['image'])): ?>
<img src="assets/uploads/<?= htmlspecialchars($website['image']) ?>" alt="<?= htmlspecialchars($website['name']) ?>" class="img-fluid mb-3">
<?php endif; ?>
</div>
</div>
<?php endif; ?>
</div>
<div class="modal-footer">
<button type="submit" name="edit_website" class="btn btn-primary">Save Changes</button>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
Thank you in advance for any assistance.

