Topic: {help needed} - using msql and php to display records

Hi i would like to know why my code isn't storing information into the stated variables after ive clicked the next record button

<?php 
$sql=mysql_query("SELECT *FROM clients WHERE id > 0 ORDER BY id ASC LIMIT 1");

$row=mysql_fetch_array($sql);

$id=$row['id'];
$FirstName=$row['FirstName'];
$Surname=$row['Surname'];
$DOB=$row['DOB'];
$Nationality=$row['Nationality'];


?>

And when i press the "Next record"  button, the data inside the previous variables should change but it doesn't


<?php 

if($_REQUEST['chngRecord'] == 'Next Record')
{

    $sql=mysql_query("SELECT *FROM clients WHERE id > {$id} LIMIT 1");
    $row=mysql_fetch_array($sql);
    
    $id=$row['id'];
    $FirstName=$row['FirstName'];
    $Surname=$row['Surname'];
    $DOB=$row['DOB'];
    $Nationality=$row['Nationality'];


echo $id;

}

Last edited by Hoax (November 21, 2011 01:07)

Thumbs up

Re: {help needed} - using msql and php to display records

I know this is a tad late, but i just joined.

In that first code box, since you're using fetch_array, you need to use a loop to view the records. i always use a while loop, but a for loop would work too. 

But since you're just viewing the very first record, just change fetch_array to fetch_assoc, then a loop isn't needed.

For the second one, you're not telling it to increment the id, you're just telling it to give you any id that's > some id (not the best way to do random id's).

if you want to get the next record (and not some random one), then do something like $id++ or $id = $id+1; (which is technically the same thing, but php can be picky that way).

<?php 
// change record code has to be on top in order to be displayed.
if(isset($_POST['submit'])) { // change yours back before you use it

    $id = $_POST['cur_id'] + 1; // id from previous record

    $sql=mysql_query("SELECT * FROM clients WHERE id = {$id} LIMIT 1,0");
    $row=mysql_fetch_array($sql);
    
    $id=$row['id'];
    $FirstName=$row['FirstName'];
    $Surname=$row['Surname'];
    $DOB=$row['DOB'];
    $Nationality=$row['Nationality'];

    echo $id;

}


<form method="post" action="">
<?php 
$sql=mysql_query("SELECT * FROM clients WHERE id > 0 ORDER BY id ASC LIMIT 1,0");

$row=mysql_fetch_assoc($sql);

$id=$row['id'];
$FirstName=$row['FirstName'];
$Surname=$row['Surname'];
$DOB=$row['DOB'];
$Nationality=$row['Nationality'];
?>
    <input type="hidden" name="cur_id" value="<?=$id?>">
    <input type="submit" name="submit" value="Next">
</form>

If you're not sure if information is getting through, you can use print_r() to view the post (or request) array on the screen.  you can also use echo var_dump(). both give the same information.

Thumbs up