This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
                            
                        
                    
                
                                7年前关闭。
            
                    
我在将字符串插入MySQL表时遇到问题。我的代码是:

mysql_query( "INSERT INTO jos_content (
    title, alias, introtext, metakey, state, created_by, metadesc)
VALUES ('$izv','$izv','$forprint','$mk',1,62,'$izv')" );


当变量$forprint不在查询中时,一切都会按预期进行,但是当我添加它时,查询数据库不会更新。 $forprint是大于4000个字符的字符串。

编辑:

输入mysql_real_escape_string传递给db,但缺少一些。

缺少的部分是:

<p><br></p><table style="text-align: left;" mce_style="text-align: left;"
class="mceItemTable" border="0" width="100%"><tbody><tr><td colspan="2"
rowspan="1"><h1 style="text-align: center;">HP</h1></br></br><h1 style="text-
align: center;" mce_style="text-align: center;"><br><p><br></p><p></h1></p><p><a
href="#" mce_href="#" onclick="jwplayer().load('/templates/BV/list
/HP.xml')"><p style="text-align: center;" mce_style="text-align:
center;">SDS</p></a><br mce_bogus="1"></p><br mce_bogus="1"></td></tr>


所以在我的$ forprint生成的exit.txt中,所有数据都存在,但是在DB中,这部分和表的关闭部分丢失了。

编辑2:
PDO完成了这项工作,谢谢您的建议。

最佳答案

$forprint的内容是什么?它是否包含'?如果是这样,那会引起问题。 $forprint的内容是否来自用户?如果是这样,您有严重的问题。

您并没有通过允许SQL injection attacks来逃避可能极度危险的变量

尝试使用mysql_real_escape_string

例如:

$con = mysql_connect("localhost","root","");
if (!$con)
  die('Could not connect: ' . mysql_error());


mysql_select_db("balkan", $con);

$izv_safe = mysql_real_escape_string($izv,$con);
$mk_safe = mysql_real_escape_string($mk,$con);
$forprint_safe = mysql_real_escape_string($forprint,$con);

$query = <<<END_OF_SQL
  INSERT INTO jos_content (title, alias, introtext, metakey, state, created_by, metadesc)
  VALUES ('$izv_safe','$izv_safe','$forprint_safe','$mk_safe',1,62,'$izv_safe')"
END_OF_SQL;

mysql_query($query,$con);


更好的是,使用prepared statements via mysqliPDO

关于php - PHP MySQL插入语句不会插入数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13388262/

10-12 12:32