How to Store File Name in Database While Uploading Image to Server Using PHP
Storing file names in a database is necessary when you want to link images to other data in your application. This allows you to easily retrieve and display the images associated with specific records. To achieve this, you'll need to use a few techniques to handle the file upload and database insertion.
Let's begin with the form that collects the image and additional information:
<br><form method="post" action="addMember.php" enctype="multipart/form-data"></p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><input type="text" name="nameMember" placeholder="Band Member Name"/> <input type="text" name="bandMember" placeholder="Band Member Position"/> <input type="file" name="photo" accept="image/*" /> <textarea name="aboutMember" placeholder="Additional Member Information"></textarea> <input type="text" name="otherBands" placeholder="Other Bands Member Has Been In"/> <input type="submit" name="upload" value="Add Member"/>
Next, you'll write the PHP script to process the form data and handle the image upload:
<br><?php</p><p>$targetDirectory = "images/";<br>$targetFilePath = $targetDirectory . basename($_FILES'photo');</p><p>$nameMember = $_POST['nameMember'];<br>$bandMember = $_POST['bandMember'];<br>$aboutMember = $_POST['aboutMember'];<br>$otherBands = $_POST['otherBands'];</p><p>try {</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">// Connect to the database $conn = new PDO('mysql:host=localhost;dbname=yourdbname', 'username', 'password'); // Prepare the SQL statement to insert data into the database $stmt = $conn->prepare("INSERT INTO members (name, band_position, photo, about, other_bands) VALUES (:name, :band_position, :photo, :about, :other_bands)"); // Bind the parameters to the SQL statement $stmt->bindParam(':name', $nameMember); $stmt->bindParam(':band_position', $bandMember); $stmt->bindParam(':photo', $photoName); $stmt->bindParam(':about', $aboutMember); $stmt->bindParam(':other_bands', $otherBands); // Execute the SQL statement $stmt->execute(); // Upload the image to the server if (move_uploaded_file($_FILES['photo']['tmp_name'], $targetFilePath)) { // Image uploaded successfully echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the database."; } else { // Image upload failed echo "Sorry, there was a problem uploading your file."; }</p> <p>} catch (PDOException $e) {</p> <pre class="brush:php;toolbar:false">echo "Error: " . $e->getMessage();
}
$conn = null;
?>
In this code, we establish a database connection using PDO and prepare an SQL statement to insert the data into the database. We also bind the form data to the SQL parameters and execute the statement. Finally, we upload the image to the specified directory.
By following these steps, you can successfully store both the uploaded file name and other information in your database.
The above is the detailed content of How to Store File Names in a Database When Uploading Images in PHP?. For more information, please follow other related articles on the PHP Chinese website!