-1

我正在创建一个反馈表单,用户可以在其中使用 php mysqli 编写他们的反馈并将其存储在数据库中,而无需刷新整个页面。我收到了成功消息,但没有任何输入的数据,谁能帮助我?我昨天问了同样的问题php mysqli 插入和更新查询

反馈表格.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']);


?>
<!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_process.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 = "feedback_form.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> 

反馈过程.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', '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, user_name,) VALUES(?,?)";

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

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

  }
  $query2 = "UPDATE feedback SET feedback_text = ?, user_name = ? WHERE user_name = ? ";
  $stmt = $conn->stmt_init();
  if($stmt->prepare($query2))
  {
     $stmt->bind_param('sss', $comments, $login_user, $login_user);
     $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";
?>
4

1 回答 1

0

第一:不要问两次。您不会因为您回答两次而得到更好或更快的答案...

成功消息只是告诉您您已经成功访问​​了一个文件(而不是其他任何内容)。基于此,我会尝试使用“虚拟”注释和“虚拟”登录用户单独运行 feedback_process.php(不涉及feedback_form.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', 'root', 'lam_el_chamel_db');

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


//Some dummys for debugging
$comments = 'This is the comments'; 
$login_user = 'FOO';


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

$stmt = $conn->stmt_init();
if($stmt->prepare($query)) {
    $stmt->bind_param('ss', $comments, $login_user);
    $stmt->execute();
}
else {
    echo 'FAILED!';
}

$query2 = "UPDATE feedback SET feedback_text = ?, user_name = ? WHERE user_name = ? ";
$stmt = $conn->stmt_init();
if($stmt->prepare($query2)) {
    $stmt->bind_param('sss', $comments, $login_user, $login_user);
    $stmt->execute();
}
else {
    echo 'FAILED!';
}

if($stmt){
    echo "thank you .we will be in touch soon <br />";
}
else {
    echo "there was an error. try again later.";
}  

?>
于 2013-03-24T23:21:59.163 回答