0

I have this code which suppose to fetch data from mysql, there are list of users so if you click one user it will show detail info of the user but whenever i click for example user 3 or 4 it fetch only user 1. i need help here is

fetch.php

<?php
       $connection = mysql_connect('localhost', 'root', '');
       $db = mysql_select_db('temp', $connection);
       $x = $_POST['id'];
       $safex = mysql_real_escape_string($x);
   $query = mysql_query("select * from class where id=$safex",  $connection);

   $result = "";

      $result .= "<div id='display'>";
      $result .="<table border=\"1\">";
      $result .="<tr><th>Name</th><th>Password(encrypted)</th></tr>";
        while($row = mysql_fetch_assoc($query)){
$result .= "<tr><td> {$row['name']}</td>"."<td> {$row['password']}</td></tr></p>";
}
$result .="</table>";

$result .= "</div>";
echo $result;

?>

and here is

index.php

    <?php
$connection = mysql_connect('localhost', 'root', '');
$db = mysql_select_db('temp', $connection);
?>
<html>
<head>
<title>Fetch record using jQuery</title>
<style type="text/css">
#display {
margin : 225px;
}
</style>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
$('document').ready(function(){

$('a').click(function(){
var temp = $('a').attr('myval');
$.post('fetch.php', {id:temp}, function(data){
$('#display').html(data);
});
});
});
</script>
</head>
<body>
<?php
$query = mysql_query("select * from class order by id desc LIMIT 2", $connection);
echo "<ul>";
while($row = mysql_fetch_assoc($query)){
echo "<li><a href=\"javascript:return(0)\" myval=\"{$row['id']}\"><h3>{$row['name']}</h3></a></li>";

}

echo "</ul>";

?>
<div id="display"></div>
</body>
</html>
<?php
mysql_close($connection);
?>
Buzu
  • 11
  • 2
  • 1
    I think the problem is when setting `temp` variable. Should be `var temp = $(this).attr('myval')`. – Honza Haering May 18 '15 at 14:38
  • 2
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 18 '15 at 14:38

3 Answers3

0

use mysql_fetch_array() intesd of mysql_fetch_assoc()

while($row = mysql_fetch_array($query)){
$result .= "<tr><td> {$row['name']}</td>"."<td> {$row['password']}</td></tr></p>";
}
Muhammed Shuhaib
  • 314
  • 6
  • 20
0

The issue is not in database. The issue with your code is the statement

var temp = $('a').attr('myval');

Use the following code :

$('a').click(function(this){
    var temp = $(this).attr('myval');
    $.post('fetch.php', {id:temp}, function(data){
         $('#display').html(data);
    });
 });

The variable 'temp' is always fetching the first id of the anchor tag. Rest of your code is OK.

IBAD GORE
  • 403
  • 2
  • 5
-1

Thanks 'Honza Haering' it is working now, the problem is i used var temp = $('a').attr('myval') but the correct is this var temp = $(this).attr('myval')

Buzu
  • 11
  • 2