【问题标题】:Insert special chars in my database with Ajax使用 Ajax 在我的数据库中插入特殊字符
【发布时间】:2013-03-15 06:45:58
【问题描述】:

我正在使用 php 和 mysql 创建一个简单的评论表单 t 连接到数据库和 jquery ajax 以不刷新表单包含的页面: 姓名 电子邮件 评论 问题是数据库中的名称字段(名称)只是插入 [1] 的值 但是当我发布字段 name 的值时,它会回显正确的值,任何人都可以帮助我吗??

submit_to_db.php

<?php
  $conn = new mysqli('localhost', 'root', '', 'my_db');
  echo"<pre>";
  print_r($_POST);
  echo"</pre>";

  if(isset($_POST['name_'])){
  $name =isset ($_POST['name_']);
  $email = $_POST['email'];
  $comments = $_POST['comments'];



  $query = "INSERT into comments(name, email, comments) VALUES(?, ?, ?)";

  $stmt = $conn->stmt_init();
  if($stmt->prepare($query)){

     $stmt->bind_param('sss', $name, $email, $comments);
     $stmt->execute();

  }

  if($stmt){

  echo "thank you .we will be in touch soon <br />";
 // echo $_POST['name'];
  //echo $_POST['email'];
  //echo $_POST['comments'];

  }
  else{
   echo "there was an error. try again later.";
   }  

}

else
   echo"it is a big error";
?>

这是表单注释

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>feedback page</title>
<script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link rel ="stylesheet" href = "css/default.css" />

<script type = "text/javascript">

$(function(){

   $('#submit').click(function(){
     $('#container').append('<img src = "img/loading.gif" alt="Currently loading" id = "loading" />');

         var name = $('#name_').val();
         var email = $('#email').val();
         var comments = $('#comments').val();


         $.ajax({

            url: 'submit_to_db.php',
            type: 'POST',
            data: 'name_=' + name + '&email=' + email + '&comments=' + comments,

            success: function(result){
                 $('#response').remove();
                 $('#container').append('<p id = "response">' + result + '</p>');
                 $('#loading').fadeOut(500, function(){
                     $(this).remove();
                 });

            }

         });         

        return false;

   });


});

</script>




</head>

<body>
   <form action = "submit_to_db.php" method = "post">
   <div id = "container">
      <label for = "name">Name</label>
      <input type = "text" name = "name_" id = "name_" />

      <label for = "email">Email address</label>
      <input type = "text" name = "email" id = "email" />

      <label for = "comments">Comments</label>
      <textarea rows = "5"cols = "35" name = "comments" id = "comments"></textarea>
      <br />

      <input type = "submit" name = "submit" id = "submit" value = "send feedBack" />
    </div>
   </form>




</body>
</html>

【问题讨论】:

  • 啊!终于有人不使用mysql_*函数了。

标签: php jquery mysqli


【解决方案1】:

问题来了

$name =isset ($_POST['name_']);

应该是

$name =$_POST['name_'];

您使用了isset,它给出了值 1 并存储在 $name 中,因此它在数据库中插入了 1。

【讨论】:

    猜你喜欢
    • 2016-02-18
    • 1970-01-01
    • 2012-03-24
    • 2020-08-30
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 2011-02-04
    相关资源
    最近更新 更多