0

i have an ajax script and a php file on the server that inserts new post to the database, the ajax script can post to the target php file and get a response as json data (which is decoded and echo as HTML data which is my posts) which I use to append to a div. however, my solution returns the entire result from my database, i.e all my posts that my query returns via mysql_query. how do i get only results that are recent or at least how do i get only my last post;

$(function() {
 $("#PostItemz").on('submit', function(event){
event.preventDefault()
var formData = new FormData(this);

    $.ajax({
        url: 'inc/modules/post_data.php',
        type: 'POST',
        data: formData,
        success: function (data) {
             //here the data return is my post as html object
            //data is return via echo on another php page
           console.log(data);
           $('#postarea').append(data);
           $("#PostItemz")[0].reset();
           $('.inner-addon, .left-addon, .create-text').hide();
        },
        cache: false,
        contentType: false,
        processData: false
    });

});


  });

AND this is my php code: on server that returns the post data:

        <?php
         function getFollowedGroupPosts($user_id){

        $sql_query = mysql_query("SELECT gid FROM members WHERE uid = '$user_id'");
   //iterate through record set, push records into user defined record_set[] array and return array as json encoded data
                        .
                        .
                        .
                        .
                        .
        $record_set[] = array('gid' => $rows_b['gid'],
                    'pid' => $rows_b['pid'],
                    'post' => $rows_b['post'],
                    'images' => getPostImages($rows_b['pid']),
                    'jImages' => $url_string,
                    'videos' => getPostVideos($rows_b['pid']),      
                    'audios' => getPostAudios($rows_b['pid']),
                    'date' => timeAgo($rows_b['date']),
                    'username' => userIdToName($rows_b['uid']),
                    'filemap_id' => $rows_b['filemap_id'],
                    'group_name' => groupIdToName($rows_b['gid']),
                    'comments' => getComments($rows_b['pid']),
                    'likes' => checkLikeCount($rows_b['pid']),
                    'dislikes' => checkDislikeCount($rows_b['pid']),
                    'post_count' => $row_count_b);


    }

        }
        return $record_set;

how do i make async ajax calls to php and what query do i use on server side to return recent posts other than the ones on my page already

Maxwell
  • 147
  • 2
  • 12
  • Go read up on how ORDER BY and LIMIT work. Go do that after you have read up on what you need to do in regard to SQL injection. – CBroe Sep 11 '17 at 11:40
  • Possible duplicate of [SQL Server SELECT LAST N Rows](https://stackoverflow.com/questions/4193705/sql-server-select-last-n-rows) – Haseeb Hassy Sep 11 '17 at 11:45
  • Order by is what u need – Masivuye Cokile Sep 11 '17 at 11:49
  • Don't use the `mysql_*` functions. They have been deprecated since v5.5 (Jun 2013) and removed since v7.0 (Dec 2015). Instead use the [**mysqli_***](https://secure.php.net/manual/en/book.mysqli.php) or [**PDO**](https://secure.php.net/manual/en/book.pdo.php) functions with [**prepared statements**](https://secure.php.net/manual/en/pdo.prepare.php) and [**bound parameters**](https://secure.php.net/manual/en/pdostatement.bindparam.php). – Alex Howansky Sep 11 '17 at 14:01
  • Your code is vulnerable to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use prepared statements with bound parameters, via either the [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) drivers. [**This post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has some good examples. – Alex Howansky Sep 11 '17 at 14:02
  • am working on a legacy system, which explains the mySQL syntax use – Maxwell Sep 11 '17 at 14:03

2 Answers2

1

For any last inserted record will be get through mysql_insert_id If your table contain any AUTO_INCREMENT column it will return that Value.

<?php
$last_id = mysql_insert_id ( mysqli $link )
SELECT * FROM tbl WHERE pid(your primary key) = '$last_id'
?>

Also you can do that by using limit and order

SELECT * FROM `table_name` 
ORDER BY `table_name`.`column_name` DESC
LIMIT 1 

Note: Do not use mysql It deprecated from php 5.5.x

Ahmed Ginani
  • 6,522
  • 2
  • 15
  • 33
0

Use mysqli_insert_id() method to get the primary key of the last inserted post and then use that id to retrieve that particular post and return from the php script.

Kushagra Saxena
  • 671
  • 5
  • 17