Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在MySQL数据库中插入符号?_Php_Jquery_Mysql_Ajax_Database - Fatal编程技术网

Php 如何在MySQL数据库中插入符号?

Php 如何在MySQL数据库中插入符号?,php,jquery,mysql,ajax,database,Php,Jquery,Mysql,Ajax,Database,我创建了一个页面,使用PHP和jQuery将一些数据插入MySQL数据库。它工作得很好,但问题是当我尝试插入符号时,例如: :) :( :P =D :o ;) :v >:( :/ :'( ^_^ 8) B| <3 3:) O:) -_- o.O >:o :3 (y) 我得到这个错误: 您的SQL语法有错误 将数据插入数据库的代码 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/l

我创建了一个页面,使用PHP和jQuery将一些数据插入MySQL数据库。它工作得很好,但问题是当我尝试插入符号时,例如:

 :) :( :P =D :o ;) :v >:( :/ :'( ^_^ 8) B| <3 3:) O:) -_- o.O >:o :3 (y) 
我得到这个错误:

您的SQL语法有错误

将数据插入数据库的代码

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function(){
    $("#insert").click(function(){
      var meesg=$("#reply").val();
      clear();
      $.post('full.php', {messagge: meesg, from: 'cevin', to: 'calvin'},
          function(data){
              $("#message").html(data);
              $("#message").hide();
              $("#message").fadeIn(200);
          });
      return false;
    });
    function clear() {
      $("#myre:input").each(function() {
          $(this).val('');
      });
    }
});
</script>
<a id="insert" title="Insert Data" href="#">Push into mysql</a>
PHP:

如何解决向数据库中插入符号的问题?

您需要转义参数,而不是整个查询,尤其是在执行后进行的查询,这根本没有意义,因为太晚了。所以这是错误的:

# this is wrong!
$query=mysql_query("INSERT INTO `thetable`(`to`,`from`,`message`) VALUES ('$to','$from','$msg')");
mysql_real_escape_string($query);
这更好,但还是切换到PDO或至少使用mysqli_389;:


除了您应该真正使用PDO或MySQLi之外,您不能对整个查询进行转义,您必须转义参数:

$to = mysql_real_escape_string($to);
$from = mysql_real_escape_string($from);
$msg = mysql_real_esacpe_string($msg);
$query=mysql_query("INSERT INTO `thetable`(`to`,`from`,`message`) VALUES ('$to','$from','$msg')");

你必须逃避大多数安全原因。。。当你这样做的时候,你可以为你的SQL输入使用某种卫生设施。我的建议是学习准备陈述等。更多信息可以找到

如果您不保护sql的输入,那么当有人决定对您恶作剧或一起破坏您的数据库时,您会遇到一些麻烦


PS:准备语句比转义字符串好。

>推荐。我很高兴mysql从PHP7中删除。你是说像这样的mysql\u real\u escape\u字符串$msg;例如,是的。请参阅编辑后的答案太棒了!!!!!!成功了!!!更好的答案是建议OP不要再使用mysql_*函数,因为它们不再受支持,非常过时,在所有受支持的PHP5版本中都被弃用,在PHP7i中被完全删除。我相信,但是,还是切换到PDO,或者至少使用mysqli_*很好地解决您的投诉。太好了,我也在搜索它!非常感谢。
# this is right
$q = sprintf("INSERT INTO `thetable`(`to`,`from`,`message`) VALUES ('%s','%s','%s')",
      mysql_real_escape_string($to),
      mysql_real_escape_string($from),
      mysql_real_escape_string($msg));
$query=mysql_query($q);
$to = mysql_real_escape_string($to);
$from = mysql_real_escape_string($from);
$msg = mysql_real_esacpe_string($msg);
$query=mysql_query("INSERT INTO `thetable`(`to`,`from`,`message`) VALUES ('$to','$from','$msg')");