由于您需要自定义字符串格式化,根据您的应用程序需要,我建议您从 mysql 获取字符串,然后手动检查您的字符串,查找搜索到的字符串并根据需要对其进行格式化,然后更新记录在你的 mysql 数据库中。所有这些都可以在您的 php 脚本中完成。
这是一个示例,但可能会根据您的需要有所不同: 注意:此示例假设您从提供 2 个变量的网页调用 php 页面:userId 和要写入的描述。php页面假设写在一个db上,其中字段userId是表的键,唯一要写的字段名为description
添加数据.php
<?php
//first of all define a function to open db and get your data
function searchIdOnDb($id)
{
//connect to your database
mysql_connect("localhost","root","root");
//specify database
mysql_select_db("yourDBname") or die;
//Build SQL Query
$id = mysql_real_escape_string($id);
$query = "select * from yourDBtable where userId ='" . $id . "'";
$queryResult=mysql_query($query);
return $queryResult;
}
//then define a function to update your DB record
function setRecordOnDb($id, $description)
{
//the connection will be still active, then it is not needed to redo it
$id = mysql_real_escape_string($id);
$description = mysql_real_escape_string($description);
if ($description=="") $description=" ";
$query="UPDATE yourDBtable SET description='" . $description . "' WHERE userId='" . $id . "'";
$queryResult=mysql_query($query);
return $queryResult;
}
//ok, we are ready to start. first of all get your variable from the webpage
$me=$_GET["userID"];//get user id
$description=$_GET["description"];//get data to write
//if no data, exit
if ($me==null)
{
die;
}
//if no data, exit
if ($description==null)
{
die;
}
//search in db
$result=searchIdOnDb($me);
$numrows=mysql_num_rows($result);
if ($numrows == 0) { //if user is not in the db exit
die;
} else {
//the user is in the db, let's generate new description and update it
//get description from the record
$row = mysql_fetch_assoc($result);
$originalDescription= $row["description"] ;
//here compare your string description with the string originalString in order
//to obtain the newDescription string
//just for test i will append the two strings, but you can make your search inside the strings and format it according to your needs
$newDescription=$originalDescription . " " . $description;
//update record
$setRecord = setRecordOnDb($me, $newDescription);
}
if ($setRecord==FALSE ) {
//error during write, send me email
$to = "yourEmail@yourProvider.com";
$subject = "Error in file addData.php";
$body = "Variables: \n me=" . $me . " \n newDescription=" . $newDescription . " \n originalDescription=" . $originalDescription . " \n description=" . $description ;
mail($to, $subject, $body);
die;
}else{
//write ok, return ok
die("ok");
}
?>