2013-03-23 168 views
0

我正在創建一個反饋頁面,允許用戶編寫他們的評論或投訴,並使用php和mysqli連接到數據庫和jquery ajax以顯示消息而不刷新頁面,但問題是: 系統顯示susccess消息而沒有任何插入的數據到數據庫任何人都可以幫助我? PS 後按提交按鈕,我收到了重複的形式(文字區域和按鈕)php mysqli錯誤

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> 
+0

您的查詢應該是「插入支架(feedback_text)VALUES(?)「,如果您的查詢中有錯誤,請嘗試打印$ conn-> error,然後執行查詢以查看錯誤 – Adidi 2013-03-23 11:27:50

回答

0

這是因爲你正在向你在同一頁面發出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文件,將conatin

<!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> 
+0

此操作解決方案不適合我如何解決它? – user2172837 2013-03-23 06:15:02

+0

@ user2172837立即嘗試,並檢查你sqli查詢 – 2013-03-23 06:16:28

+0

@ Bamba先生它仍然顯示成功消息,而不插入數據....我的表名:feedback字段是feedback_id,feedback_text也許錯誤是在查詢中,你可以幫助我?? – user2172837 2013-03-23 06:25:54

-1

你可以不喜歡這樣。

$('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 
        } 
    }); 

});

+0

並嘗試使用**「插入反饋(feedback_text)VALUES(?)」** – sriyan 2013-03-23 06:46:13

+0

也沒有工作 – user2172837 2013-03-23 07:14:41

+0

你能解釋你正在經歷什麼嗎? – sriyan 2013-03-23 07:37:23

0

沒有提供答案的是有用的,只有通過Adidi寫小評論我不得不把這就是問題所在感謝ANW的所有幫助