Uploading an image to a server and storing its filename in a database can be challenging. Additionally, integrating the image upload with the submission of other data from a form presents difficulties.
The updated form includes the following changes:
The PHP code connects to the database and inserts both the uploaded image and the other form data.
Database Connection and Data Insertion:
// Connect to the database $connect = mysqli_connect("yourhost", "username", "password", "dbName"); // Write the information to the database $query = "INSERT INTO tableName (nameMember, bandMember, photo, aboutMember, otherBands) VALUES ('$name', '$bandMember', '$pic', '$about', '$bands')"; mysqli_query($connect, $query);
Image Upload:
// Image upload path $target = "your directory"; $target = $target . basename($_FILES['photo']['name']); if (move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { echo "The file has been uploaded, and your information has been added to the directory"; } else { echo "Sorry, there was a problem uploading your file."; }
The above is the detailed content of How to Efficiently Store Uploaded Image Filenames in a Database Using PHP?. For more information, please follow other related articles on the PHP Chinese website!