Ajax and mysql data interaction to create message board function

亚连
Release: 2018-05-22 17:20:00
Original
1694 people have browsed it

This article mainly introduces Ajax and mysql data interaction in detail to realize the message board function. It has certain reference value. Interested friends can refer to it.

I recently made a small The demo implements the data interaction between Ajax and MySQL. The js part uses jq, the backend uses php, and the database is mysql. A node mongodb version will be released later.

I won’t go into details about the use and installation of mysql. I integrate Baidu xampp, Apache server and mysql database by myself, which is very easy to use.

First open the server and database. I first created an "eleven" database here, and then created a table called microblog (please note: I am using a high version of mysql here, and PHP is linked to the database. All methods use mysqli_ If the version is too low, please use the mysql_ method and modify the code yourself)
The following is the code part:

html page and js part:

    微博留言板  

1 2

Copy after login

This part is the php code part:

Copy after login

Note: I wrote this part as public code because I am learning to do other things is called, so the following code will have

include_once "comment.php";
Copy after login

. This line refers to other codes

         0){ $arr = ["error"=>0,"id"=>$insertId,"time"=>$times]; echo json_encode($arr);//将数组转化为json,方便前端使用 } else{ $arr = ["error"=>1,"msg"=>"留言失败,请重试!"]; echo json_encode($arr);//将数组转化为json,方便前端使用 } break; case 'up': $id = $_GET['id']; $search = "SELECT up FROM microblog WHERE id = $id"; $result = mysqli_query($link, $search); $upNum = mysqli_fetch_row($result)[0]; $upNum++; $query = "UPDATE microblog SET up='{$upNum}' WHERE id = '{$id}'"; mysqli_query($link,$query); if(mysqli_affected_rows($link)){//更新数据成功 echo '{"error":"0"}'; } else{//更新失败 echo '{"error":"1","msg":"点赞失败!"}'; } break; case 'down': $id = $_GET['id']; $search = "SELECT down FROM microblog WHERE id = $id"; $result = mysqli_query($link, $search); $downNum = mysqli_fetch_row($result)[0]; $downNum++; $query = "UPDATE microblog SET down='{$downNum}' WHERE id = '{$id}'"; mysqli_query($link,$query); if(mysqli_affected_rows($link)){//更新数据成功 echo '{"error":"0"}'; } else{//更新失败 echo '{"error":"1","msg":"踩失败!"}'; } break; case 'remove': $id = $_GET['id']; $query ="DELETE FROM microblog WHERE id='{$id}'"; mysqli_query($link,$query); if(mysqli_affected_rows($link)>0){//删除数据成功 echo '{"error":"0"}'; } else{ echo '{"error":"1","msg":"删除失败!"}'; } break; case 'count'://返回总页码 $query = "SELECT count(id) FROM microblog"; $result = mysqli_query($link, $query); $count = mysqli_fetch_row($result)[0];//以索引数组形式返回查询结果 $countPage = ceil($count/5); echo '{"error":"0","countPage":"'.$countPage.'"}'; break; case 'page'://点击分页或者是页面第一次加载 $index = $_GET["num"]*5; $search = "SELECT * FROM microblog ORDER BY id DESC LIMIT {$index},5";//倒叙查询留言 $result = mysqli_query($link, $search); $arr = [];//存查询出来的数据 while($row = mysqli_fetch_assoc($result)){ array_unshift($arr,$row); } // print_r($arr); // {"error":"0","info":[{},{},{},{},{}]} $resultArr = ["error"=>"0","info"=>$arr]; echo json_encode($resultArr); break; } ?>
Copy after login

. The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Instances of ajax responding to json strings and json arrays (graphic tutorial)

Ajax synchronization and asynchronous issues Brief analysis and solutions

Two methods for Ajax to solve redundant refresh

The above is the detailed content of Ajax and mysql data interaction to create message board function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!