Home > Article > Backend Development > How to modify database content in php
php method to modify database content: first select the database; then obtain the data set through the "mysql_query($q)" statement; then read the data in a loop and store it in the array object; finally modify the database field content through the UPDATE method That’s it.
Recommendation: "PHP Video Tutorial"
php front-end passes parameters, traverses the fields of the database table according to the conditions to modify.
<?php header("Content-Type:text/html;charset=utf8"); header("Access-Control-Allow-Origin: *"); //解决跨域 header('Access-Control-Allow-Methods:POST');// 响应类型 header('Access-Control-Allow-Headers:*'); // 响应头设置 $link=mysql_connect("localhost","root","root"); mysql_select_db("business_card", $link); //选择数据库 mysql_query("SET NAMES utf8");//解决中文乱码问题 $wx_id = $_POST['wx_id']; $openid = $_POST['openid']; $q = "SELECT * FROM user"; //SQL查询语句 SELECT * FROM 表名 $rs = mysql_query($q); //获取数据集 if(!$rs){die("数据库没有数据!");} //循环读取数据并存入数组对象 $dlogs;$i=1; while($row=mysql_fetch_array($rs)) { $result = @mysql_query($strsql); if($row["openid"]==$openid){ if($row["collect"]!=""){ $ab =$row["collect"].','.$wx_id; }else{ $ab =$wx_id; } //修改数据库字段 mysql_query("UPDATE user SET collect = '$ab' WHERE openid = '$openid'"); echo urldecode(json_encode($ab)); } } //以json格式返回html页面 ?>
The above is the detailed content of How to modify database content in php. For more information, please follow other related articles on the PHP Chinese website!