Search ALL -->
| how to get the current added in the table without getting all the data ( 2024-03-31 ) + ( 854 ) | | https://stackoverflow.com/questions/12576017/how-to-get-the-current-added-in-the-table-without-getting-all-the-data | how to get the current added in the table without getting all the data Asked 11 years, 6 months ago Modified 8 years, 3 months ago Viewed 276 times Part of PHP Collective Report this ad 1 guys im trying to make a simple commenting system, that when i click the submit the fields will automatically save in the database then fetch it using ajax. but im getting all the data repeatedly instead of getting the recently added data in the database here is the code: <div id="wrap-body"> <form action="" method="post"> <input type="text" name="username" id="username"> <input type="text" name="msg" id="msg"> <input type="button" id="submit" value="Send"> </form> <div id="info"> </div> </div> <script> $(document).ready(function (){ $('#submit').click(function (){ var username = $('#username').val(); var msg = $('#msg').val(); if(username != "" && msg != ""){ $.ajax({ type: 'POST', url: 'get.php', dataType: 'json', data:{ 'username' : username , 'msg' : msg}, success: function (data){ var ilan=data[0].counter; var i = 0; for(i=0;i<=ilan;i++){ $('#info').append("<p> you are:"+data[i].username+"</p> <p> your message is:"+data[i].mesg); } } }); } else{ alert("some fields are required"); } }); }); </script> PHP: <?php $host='localhost'; $username='root'; $password='12345'; $db = 'feeds'; $connect = mysql_connect($host,$username,$password) or die("cant connect"); mysql_select_db($db) or die("cant select the".$db); $username = $_POST['username']; $msg = $_POST['msg']; $insert = "INSERT INTO info(user_name,message) VALUES('$username','$msg')"; if(@!mysql_query($insert)){ die('error insertion'.mysql_error()); } $get = "SELECT * FROM info "; $result=mysql_query($get)or die(mysql_error()); $inside_counter = mysql_num_rows($result); $data=array(); while ($row = mysql_fetch_array($result)) { $data[] = array( 'username'=>$row['user_name'], 'mesg'=>$row['message'], 'counter'=>$inside_counter ); } echo json_encode($data); ?> phpjqueryajax Share Improve this question Follow edited Sep 25, 2012 at 6:24 rekire's user avatar rekire 47.6k3131 gold badges172172 silver badges269269 bronze badges asked Sep 25, 2012 at 4:01 Aoi M. Serizawa's user avatar Aoi M. Serizawa 47311 gold badge44 silver badges77 bronze badges Add a comment 2 Answers Sorted by: 2 SELECT * FROM "table_name" ORDER BY "id" desc LIMIT 1 This is a SQL query to get last record from table. It return last inserted according to id. id should be a auto increment field. I think this will be helpful for you. Share Improve this answer Follow edited Dec 6, 2015 at 21:17 marc_s's user avatar marc_s 743k178178 gold badges1.4k1.4k silver badges1.5k1.5k bronze badges answered Sep 25, 2012 at 6:55 Mangala Edirisinghe's user avatar Mangala Edirisinghe 1,11122 gold badges1616 silver badges3232 bronze badges Add a comment Report this ad 0 Its because you are returning all the row in the table again to the ajax call via $get = "SELECT * FROM info "; if you do return all of them again you will have to test if they are not already there before appending them with the jQuery Perhaps only return the newly inserted row. or The jQuery already knows the data it sent to the server, just return success true or false, and then append it if true with the jQuery, no need to return the data back again to the client side EDIT I'm not going to write code for you but perhaps these suggestions may help mysql_query($insert) link here for reference - http://php.net/manual/en/function.mysql-query.php will return true of false depending on if the insert statement was successful you could potentially also check that it acutally inserted a row by calling mysql_affected_rows() link here for reference - http://php.net/manual/en/function.mysql-affected-rows.php then assuming that the insert was successful (i.e. true from mysql_query($insert) or mysql_affected_rows() > 0) simply return successful (i.e. something like echo json_encode(true); then on the client side you could do something like the following: (jQuery Ajax success function only:) success: function (data){ if (data) { $('#info').append("<p> you are:"+username +"</p> <p> your message is:"+msg); } else { alert('There has been an error saving your message'); } } using the variables that you just used to send the request (Please note code samples provided here are only for example, not intended to be used verbatim) Couple of points though. Is your intention to post the data via ajax only? (i.e. no page refresh) as if that is the case, you will need to return false from you form submit event handler to stop the page full posting. Also you do not appear to have any logic, or are not worried about complete duplicates being entered (i.e. same username, same message) - not sure if you want to worry about this. I would also potentially look at tracking all the messages via ids (although I don't know your DB structure), perhaps using mysql_insert_id() link here FYI - http://php.net/manual/en/function.mysql-insert-id.php That way each message can have a unique id, may be easier to establish duplicates, even if the username and message are identical There are numerous ways to deal with this. Anyway hope that helps PS - using the mysql_ - group of commands is generally discouraged these days, use the mysqli_ range of function instead
|
|