简体   繁体   English

如何通过 PHP 将文本和 html 格式的数据插入 MySQL 数据库?

[英]How to insert text and html format data into a MySQL database via PHP?

I have a problem inserting data (text and HTML format) into a MySQL field LONGTXT.我在将数据(文本和 HTML 格式)插入 MySQL 字段 LONGTXT 时遇到问题。 Here is the error这是错误

  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)

error and almost clear.错误,几乎清楚。 I used all the functions of protection (encoding format, quote, disabling html...) I thought of another way, I created two functions dealing with commas, semicolons and slash.我使用了所有的保护功能(编码格式、引用、禁用html...)我想到了另一种方法,我创建了两个处理逗号、分号和斜杠的函数。 Here is the code of the function addslashes:这是函数addlashes的代码:

FUNCTION addText($txt)
    {
        IF(get_magic_quotes_gpc()==false)
                {
                RETURN utf8_encode(addslashes($txt));
                }else{
                RETURN utf8_encode($txt);
                }

    }

protect commas function :保护逗号功能:

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';
    }

insert php function :插入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;
}

insert SQL proc code:插入 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

Syntax error always grabs me by the throat.语法错误总是扼住我的喉咙。 Could you help me please?请问你能帮帮我吗?

Don't utf8_encode , unless you want to convert strings from Latin-1 to UTF-8.不要utf8_encode ,除非您想将字符串从 Latin-1 转换为 UTF-8。
Don't use addslashes or depend on magic quotes;不要使用addslashes或依赖魔术引号; turn magic quotes off or use stripslashes to reverse their effects should you not be able to turn them off.如果您无法关闭魔术引号,请关闭魔术引号或使用stripslashes来反转它们的效果。
Don't manually replace and escape single characters, unless you have a very specific reason to.不要手动替换和转义单个字符,除非您有非常具体的原因。

Do escape once using the appropriate escaping mechanism for your database.使用适合的数据库的转义机制进行一次转义。 If you're using mysql_* (don't use that anymore), use mysql_real_escape_string .如果您正在使用mysql_* (不再使用它),请使用mysql_real_escape_string If you're using mysqli_* , use mysqli_real_escape_string or better yet prepared statements.如果您使用mysqli_* ,请使用 mysqli_real_escape_string或更好 的准备好的语句。 If you're using PDO, use prepared statements.如果您使用 PDO,请使用准备好的语句。

See The Great Escapism (What you need to know to work with text within text) for a longer, more detailed discussion of the topic.有关该主题的更长、更详细的讨论,请参阅The Great Escapism(使用文本中的文本需要知道的内容)

Your current "prepared statement" is useless, since it does not separate the query from the values at all.您当前的“准备好的语句”是无用的,因为它根本没有将查询与值分开。 You're just concatenating all values as usual, then force them through a prepared statement in one go.您只是像往常一样连接所有值,然后一次性强制它们通过准备好的语句。 There's also no need for a stored procedure, this can all be better done using the client-side API.也不需要存储过程,这都可以使用客户端 API 更好地完成。

So:所以:

  1. Disable magic quotesas explained in the manual . 按照手册中的说明禁用魔术引号。
  2. Use prepared statements as explained in the manual .按照手册中的说明使用准备好的语句。
  3. Use mysqli::insert_id to get the last insert id as explained in the manual .如手册中所述,使用mysqli::insert_id获取最后一个插入 ID。
  4. There is no 4.没有4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM