时间:2021-07-01 10:21:17 帮助过:43人阅读
function insert(){
global $m;
if (!isset($_REQUEST['strs']) || !isset($_REQUEST['type'])
|| !isset($_REQUEST['hash'])){
echo 'param error';
return;
}
//strs为所有字符串
$poststr = $_REQUEST['strs'];
$xstrs = json_decode(stripslashes($poststr), true);
$type = $_REQUEST['type'];
$hash = $_REQUEST['hash'];
if (count($xstrs) <= 0){
$msg = 'str error';
DsLog::errLog($msg.$poststr);
echo $msg;
return;
}
if ($type == '0'){
$table = 'white';
}
else if($type == '1'){
$table = 'black';
}
else{
$msg = 'type error';
DsLog::errLog($msg);
echo $msg;
return;
}
$strs = array();
for($i = 0; $i < count($xstrs); $i++) {
$strtmp = $xstrs[$i];
$strtmp = trim($strtmp);
$strtmp = strtolower($strtmp);
$strtmp = addslashes($strtmp);
if (strlen($strtmp) > 256){
$strtmp = substr($strtmp, 0, 256);
}
if (strlen($strtmp) >= 7)
{
array_push($strs, $strtmp);
}
}
//拼接所有字符串
$tmp = '("'.implode('","', $strs).'")';
//获取已存在的字符串
$sql = "select * from $table where str in $tmp";
$ret = mysql_query($sql, $m);
if (!$ret){
$msg = 'exec error:'.mysql_error($m).','.$sql;
DsLog::errLog($msg);
echo $msg;
return;
}
$exists = array();
$notexists = array();
$count = mysql_num_rows($ret);
for ($i = 0; $i < $count; $i++)
{
$item = mysql_fetch_assoc($ret);
if (!$item){
break;
}
array_push($exists, $item['str']);
}
for ($i = 0; $i < count($strs); $i++){
if (in_array($strs[$i], $exists)){
continue;
}
array_push($notexists, $strs[$i]);
}
for($i = 0; $i < count($exists); $i++) {
$exists[$i] = addslashes($exists[$i]);
}
for($i = 0; $i < count($notexists); $i++) {
$notexists[$i] = addslashes($notexists[$i]);
}
if (count($exists) > 0){
//更新已存在字符串的count字段
$tmp = '("'.implode('","', $exists).'")';
$time = date('YmdHi');
$sql = "update $table set count=count+1 where str in $tmp";
$ret = mysql_query($sql, $m);
if (!$ret){
$msg = 'exec error:'.mysql_error($m).','.$sql;
DsLog::errLog($msg);
echo $msg;
return;
}
//更新已存在字符串的upd字段
$sql = "update $table set upd='$time' where str in $tmp";
$ret = mysql_query($sql, $m);
if (!$ret){
$msg = 'exec error:'.mysql_error($m).','.$sql;
DsLog::errLog($msg);
echo $msg;
return;
}
}
//插入新信息
if (count($notexists) > 0){
$time = date('YmdHi');
$sql = "insert ignore into $table (str,hash,count, upd) values";
for ($i = 0; $i < count($notexists); $i++){
$str = $notexists[$i];
$crc = sprintf("%u", crc32($str));
$sql .= "('$str','$crc','1', '$time'),";
}
$sql = substr($sql, 0, strlen($sql) - 1);
$ret = mysql_query($sql, $m);
if (!$ret){
$msg = 'insert error:'.mysql_error($m).','.$sql;
DsLog::errLog($msg);
echo $msg;
return;
}
}
echo !!$ret;
}