1

I want to get all of array value with ajax which that is coming from MySQL. I can't get all of result. I can get only 1 result.

My JQuery codes are:

$("input.input-search").keyup(function(){
    var name = $(this).val();
    if(name !='')
    {
        $.ajax({
            type: 'post',
            url: 'ajax.php?bol=search',
            data: {'name':name},
            dataType: 'json',
                success: function(val)
                {    
                     x = val.length;
                     for (i = 1; i<=x; i++){
                        $(".search-result").html(val[i].user+' * '+x);
                    }
                },
                error: function(name){
                $(".search-result").html("Nəticə yoxdur...");
                }
            });
    }
});

PHP Codes are:

case "search":
$name = trim($_POST['name']);
$q = mysql_query("SELECT * FROM `users` WHERE `user` LIKE '%".$name."%' ORDER by id;");
if(mysql_affected_rows() > 0){
    while($arr = mysql_fetch_array($q)){
        $array[] = $arr;
}
    echo json_encode($array);
}
break;
TogrulZade
  • 83
  • 7
  • 1
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Feb 09 '16 at 13:17
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Feb 09 '16 at 13:17
  • You should use `mysql_num_rows()` instead of `mysql_affected_rows()`. What do you see when you look in your console? Do you see JSON? – Jay Blanchard Feb 09 '16 at 13:19
  • I know. But i thing that if i filter the name value with mysql_real_escape string and stripslahs and close mysql connection it will ok. Is there other mistake about injection? – TogrulZade Feb 09 '16 at 13:20

2 Answers2

5

It's simple. You are overwriting your elements HTML content every time your loop runs. With the jQuery

.html("...")

method you set a new content for your selected element every time your loop runs.

So use

.append('...')

instead.

Of course at the very beginning of your success - method empty your element with

.html("");
Torsten Barthel
  • 3,059
  • 1
  • 26
  • 22
  • Ok i got all of result. But when i keyup i get new result under the other. – TogrulZade Feb 09 '16 at 13:44
  • Yes. Thanks. it was ok about getting all result. But now when i keyup then the result is written until count result. Example: i want to search users Like Togrul name. and i have 2 users like togrul. 1. Togrul. 2 Togrul2. When i write "T" it is written under to other Togrul and Togrul2. so i write "To" and it writes the results again under the result. – TogrulZade Feb 09 '16 at 13:59
  • Maybe just place the call to .html('') at the very beginning of your .keyup() - method. – Torsten Barthel Feb 09 '16 at 14:03
  • .html(""); It did not happen. – TogrulZade Feb 09 '16 at 14:14
  • Yes. yes. first i used in after $(".search-result").keyup(... so i used in success: like $(".search-result").html(''); – TogrulZade Feb 09 '16 at 14:20
4

If your query is only returning 2 rows, the problem lies in your Javascript for loop. You're starting at 1 when the array index starts at 0. Try this:

for (i = 0; i <= x; i++) {...}
Jeffwa
  • 1,143
  • 10
  • 12