我在将数据(文本和 HTML 格式)插入 mysql 字段 LONGTXT 时遇到问题。这是错误
public 'error' => string 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''<p>
Haec subinde Constantius audiens et quaedam referente Thalassio doc' at line 1' (length=226)
错误,几乎清楚。我使用了所有的保护功能(编码格式,引用,禁用html......)我想到了另一种方法,我创建了两个处理逗号,分号和斜杠的函数下面是函数addslashes的代码:
FUNCTION addText($txt)
{
IF(get_magic_quotes_gpc()==false)
{
RETURN utf8_encode(addslashes($txt));
}else{
RETURN utf8_encode($txt);
}
}
保护逗号功能:
FUNCTION protect_virgules($txt)
{
IF($txt!='')
{
$x = strlen($txt);
$newTxt = '';
FOR($i=0;$i<=$x;$i++)
{
IF($txt[$i]==',' || $txt[$i] == ';')
{
$newTxt.= '\\'.$txt[$i];
}
else
{
$newTxt.=$txt[$i];
}
}
RETURN htmlentities($newTxt);
}
else
RETURN '0';
}
插入php函数:
public function insert($table,$data){
$this->last_insert_id = NULL;
$fields = "";
$values = "";
foreach($data as $fld => $val){
$values .= trim($this -> escape($val)).",";
$fields .= $fld.",";
}
$values = '"'.substr($values,0,strlen($values)-1).'"';
$fields = '"'.substr($fields,0,strlen($fields)-1).'"';
$tab=array($this->escape($table),$fields,$values,"@last_id");
$this->CALL_SP("sp_insert",$tab);
if ( $result = mysqli_query( $this->linkId, "SELECT @last_id AS last_inserted_id" ) ) {
while ($row = mysqli_fetch_assoc($result))
{
$this->last_insert_id = $row['last_inserted_id'];
}
}
return $this->queryId;
}
插入 SQL 过程代码:
BEGIN
SET @stmt_sql=CONCAT("INSERT INTO ", tableName, " (",fields,")VALUES(", param ,")");
PREPARE stmt FROM @stmt_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT LAST_INSERT_ID() into last_id;
END
语法错误总是扼住我的喉咙。请问你能帮帮我吗 ?