-1

i am creating a feed back page that allow users to give their feedback and store this feedback in the database using php and mysqli without refreshing the page using jquery and ajax but the problem is that i do not get any inserted data although i get the success message if anyone can help me i will appreciate that

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']);

?>
<!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">
<br />
<br />

  <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" />



<?php require_once('footer.php'); ?>

</body>
</html> 

feedback_process.php

<?php

session_start();
if($_SESSION['login'] != 'true'){
        header("location:index.php");
    }


   $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();
  var_dump($stmt);

  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";
?>

table fields are : feedback_id feedback_text, user_name

user2214618
  • 51
  • 1
  • 1
  • 12
  • you are saving data and returning a string, e.g. "thank you..". What are you meaning by "I do not get inserted data"? Do do not return anything like this, do you? – herrjeh42 Mar 27 '13 at 07:53
  • What is the value of `echo $stmt->error;` after your `$query2` – Hanky Panky Mar 27 '13 at 07:53
  • And: remove the var_dump, it might break the process :-D – herrjeh42 Mar 27 '13 at 07:54
  • @ jamie0726 the var dump is for track what is being send and visualize..... second i do not get inserted data mean in the database no data are inserted – user2214618 Mar 27 '13 at 07:58
  • @ Hanky Panky the value is (You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') VALUES(?,?)' at line 1thank you .we will be in touch soon ) how to solve this ??? – user2214618 Mar 27 '13 at 08:00
  • 1
    @user2214618 remove the , after "user_name" in your query. :-D – herrjeh42 Mar 27 '13 at 08:01
  • Perhaps you're not committing the insert/updates? try adding `$stmt->close();` calls after each `$stmt->execute()`, and try closing the db connection, too – Elias Van Ootegem Mar 27 '13 at 08:01
  • @ jamie0726 i did not quite understand you – user2214618 Mar 27 '13 at 08:03
  • `(feedback_text, username,)` should be `(feedback_text, username)`. The comma before the `)` is wrong. – Barmar Mar 27 '13 at 08:04
  • thank you guys it was a silly error i had to remove a comma thank you jamie0726 – user2214618 Mar 27 '13 at 08:05
  • What's the purpose of the `UPDATE` statement? You just inserted the same data right before it. If a user can submit multiple feedbacks, it will replace all of the old ones with the new text. And why do you set username if it's the same value? – Barmar Mar 27 '13 at 08:06
  • I added the "solution" as an answer for further reference – herrjeh42 Mar 27 '13 at 08:06
  • @user2214618 final comment: consider using some kind of db layer, e.g. zend_db. It's easy to use (without the whole zend framework stack) and will give you many features out of the box like a proper centralized error management, it will help you avoiding sql injection etc. Check out this Stackoverflow post for a code example: http://stackoverflow.com/questions/4840941/zend-db-without-zend-framework – herrjeh42 Mar 27 '13 at 08:11

2 Answers2

0

You have an extra , in your query.

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

//new
$query = "INSERT into feedback (feedback_text, user_name) VALUES(?,?)";
herrjeh42
  • 2,782
  • 4
  • 35
  • 47
  • guys i have a question a notice that if the same user enter another feedback in the database it will insert a new row but what i need is to just update the field of the feedback text without adding any new row for the same user – user2214618 Mar 27 '13 at 08:14
  • check out the replace command in MySQL http://dev.mysql.com/doc/refman/5.1/en/replace.html – herrjeh42 Mar 27 '13 at 08:28
  • @ jamie0726 in the comments section people say that if we use replace statement and we have the foreign key of this desired table this table will be replaced and work well but the referenced table will be empty – user2214618 Mar 27 '13 at 08:42
  • @user2214618 you don't have a referenced table in your use case, don't you? – herrjeh42 Mar 27 '13 at 09:38
  • @user2214618 doing it like pouki06 recommended is also fine, though you have an additional query. – herrjeh42 Mar 27 '13 at 09:40
0

You need to check if a row exists with the same username :

//Check a row with same username exists 
$queryChecking = "Select COUNT(*) FROM feedback WHERE user_name = ?"; 

if the result is equal to one, you need to update row and not insert a new one ;)

Pouki
  • 1,654
  • 12
  • 18
  • @ pouki06 i am using the update statement but it seems that i am using in wrong way .. so you mean that before the insert statement i need to check if username exist and then i insert or i update ?? – user2214618 Mar 27 '13 at 08:45
  • yes. First check if an entry for the given user_name already exists. If not do an insert, otherwise do an update. – herrjeh42 Mar 27 '13 at 09:42
  • Yes exactly, like jamie said, if the row exist, reuse the same id returned by your check query, in this case update check query : $queryChecking = "Select id FROM feedback WHERE user_name = ?" if 1 row is returned, feedback already exists and you have to do update statement instead of an insert, else do the insert as you do now ;) – Pouki Mar 27 '13 at 10:36