PHP development practice: Using PHP and MySQL to implement image upload and display functions
Introduction:
In the era of modern social media and e-commerce, implementing image upload and display functions is very common in website development One of the needs. Implementing such functionality using PHP and MySQL is relatively simple and efficient. This article will introduce how to use PHP and MySQL to implement basic image upload and display functions, and come with corresponding code examples.
First, create a database named "images" and create a data table named "gallery" to store image-related data.
The data table structure is as follows:
CREATE TABLE gallery
(
id
int(11) unsigned NOT NULL AUTO_INCREMENT,
title
varchar(255) NOT NULL,
filename
varchar(255) NOT NULL,
created_at
datetime NOT NULL,
PRIMARY KEY ( id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
e6ee408a49b1a477ad34c0365e38fdd9
The above code will save the pictures uploaded by the user to the "uploads" folder of the server, and insert the relevant information of the pictures into the database table.
4efa0e76ca8196a5078164260a6872d2query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){ echo "<div class='image-box'>"; echo "<img src='uploads/" . $row['filename'] . "' alt='" . $row['title'] . "'>"; echo "<p>" . $row['title'] . "</p>"; echo "</div>"; }
} else{
echo "没有上传的图片。";
}
$connect->close();
?>
The above code will query the image information saved in the database and use the image grid's form is displayed to the user.
Conclusion:
By using PHP and MySQL, we can easily implement image upload and display functions, adding a better user experience to the website. This article provides basic code examples that readers can extend and optimize according to their own needs. I hope this article can help you implement image upload and display functions in PHP development.
The above is the detailed content of PHP development practice: Use PHP and MySQL to implement image upload and display functions. For more information, please follow other related articles on the PHP Chinese website!