Based on jQuery, this article implements a rating function through PHP and mysql, which is a simple and very good ajax application instance, you can refer to this article to introduce a jQuery + Ajax + PHP code for implementing the "like" rating function. When you click the red-hearted button on your favorite image on the page, the front-end page sends an ajax request to the backend. After receiving the request, the PHP program in the backend queries whether the user's click record exists in the IP address library. If not, the corresponding value is + 1, at the same time, the user's IP information is written to the IP database, and vice versa, the user is "liked ".
Database Design
Prepare two tables. The pic table stores the image information, including the image name, path, and total number of "liked" images. pic_ip records the IP address data after the user clicks the favorite image.
CREATE TABLE IF NOT EXISTS `pic` ( `id` int(11) NOT NULL AUTO_INCREMENT, `pic_name` varchar(60) NOT NULL, `pic_url` varchar(60) NOT NULL, `love` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `pic_ip` ( `id` int(11) NOT NULL AUTO_INCREMENT, `pic_id` int(11) NOT NULL, `ip` varchar(40) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8
Index. php
In index. php, we use PHP to read and display the image information in the pic table, and use CSS to improve the page display effect.
The Code is as follows:
JQuery + Ajax + PHP achieves "like" rating