繁体   English   中英

PHP MySQLi错误

[英]php mysqli errors

我正在创建一个反馈页面,该页面允许用户写评论或投诉,并使用php和mysqli连接到数据库和jquery ajax,以显示消息而无需刷新漏洞页面,但是问题是:系统显示的成功消息没有有任何插入的数据到数据库中的任何人都可以帮助我? ps按下提交按钮后,我得到一个重复的表格(textarea和按钮)

feedback.php

<?php

session_start();

 $login = ($_SESSION['login']);
   $userid = ($_SESSION['user_id']);
   $login_user = ($_SESSION['username']);
   $fname = ($_SESSION['first_name']);
   $lname = ($_SESSION['last_name']);
   $sessionaddres =($_SESSION['address']);


$conn = new mysqli('localhost', 'root', '', 'lam_el_chamel_db');

  echo"<pre>";
  print_r($_POST);
  echo"</pre>";

  if(isset($_POST['comments'])){

  $comments = $_POST['comments'];



  $query = "INSERT into feedback feedback_text VALUES(?)";

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

     $stmt->bind_param('s', $comments);
     $stmt->execute();

  }

  if($stmt){

  echo "thank you .we will be in touch soon <br />";

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

}

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



<!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 href="style/stylesheet.css"rel="stylesheet" type="text/css"/>

    <script type = "text/javascript">

    $(function(){

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


             var comments = $('#comments').val();


             $.ajax({

                url: 'feedback.php',
                type: 'POST',
                data: '&comments=' + comments,

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

                }

             });         

            return false;

       });


    });

    </script>




    </head>
<!--<?php require_once('header.php'); ?>-->

<body>
<form action = "submit_to_db.php" method = "post">
  <div id = "container">
            <h2><?php echo $login_user ?></h2>



          <label for = "comments">Comments</label>
          <textarea rows = "5"cols = "35" name = "comments" id = "comments"></textarea>
          <br />
  </div>
   </form>
       <input type = "submit" name = "submit" id = "submit" value = "send feedBack" />




</body>
</html> 

改变这个

data: '&comments=' + comments,

data: {'comments' : comments},

http://api.jquery.com/jQuery.post/

因为正在向与您所在页面相同的页面发出ajax请求! 制作一个名为Submit.php的文件,其中将包含php代码

<?php

session_start();

 $login = ($_SESSION['login']);
   $userid = ($_SESSION['user_id']);
   $login_user = ($_SESSION['username']);
   $fname = ($_SESSION['first_name']);
   $lname = ($_SESSION['last_name']);
   $sessionaddres =($_SESSION['address']);


$conn = new mysqli('localhost', 'root', '', 'lam_el_chamel_db');

  echo"<pre>";
  print_r($_POST);
  echo"</pre>";

  if(isset($_POST['comments'])){

  $comments = $_POST['comments'];



  $query = "INSERT into feedback feedback_text VALUES(?)";

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

     $stmt->bind_param('s', $comments);
     $stmt->execute();

  }

  if($stmt){

  echo "thank you .we will be in touch soon <br />";

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

}

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

然后制作另一个将继续的HTML文件

<!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 href="style/stylesheet.css"rel="stylesheet" type="text/css"/>

    <script type = "text/javascript">

    $(function(){

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


             var comments = $('#comments').val();


             $.ajax({

                url: 'submit.php',
                type: 'POST',
                data: {"comments": comments},

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

                }

             });         

            return false;

       });


    });

    </script>




    </head>
<!--<?php require_once('header.php'); ?>-->

<body>
<form action = "submit_to_db.php" method = "post">
  <div id = "container">
            <h2><?php echo $login_user ?></h2>



          <label for = "comments">Comments</label>
          <textarea rows = "5"cols = "35" name = "comments" id = "comments"></textarea>
          <br />
  </div>
   </form>
       <input type = "submit" name = "submit" id = "submit" value = "send feedBack" />




</body>
</html> 

提供的答案都没有用,只有阿迪迪(Adidi)写的一点评论,我不得不把问题放在括号里,谢谢大家的帮助。

您可以这样做。

$('form').on('submit',function(e){

  e.preventDefault(); $.ajax({ type : "POST", url : $(this).attr('action'), data : $(this).serialize(), success : function(data){ alert(data); // You can do whatever you like } }); 

});

暂无
暂无

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

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