Hi guys. I have been learning PHP for a couple of months now for a project at work. I need to display database records on a HTML form which i have managed to do. I have added 2 buttons that allow the users to select the next or previous record. This works fine.
The form is setup to submit to the same page the form is held on.
As you can see, i have setup 2 if statements at the start. These check to see what button was pressed (next or previous).
I want to know if i have done this using the best method? From what i can tell, everytime Next or Previous is pressed, the entire webpage is recreated. Is there a better way of doing this or have i done this correctly?
I have removed most of the code from this example because there are a lot more textboxes and other controls on the form.
#CONNECT TO SQL
#SELECT DATABASE
$query = "select * from tblEmployees where employeeID = $i";
$rs = mysql_query($query);
$row = mysql_fetch_array($rs);
#IF NEXT WAS PRESSED
if (isset($_POST['btnNext'])){
$i = $_POST['txtEmployee'];
$i++;
$query = "select * from tblEmployees where employeeID = $i";
$rs = mysql_query($query);
$row = mysql_fetch_array($rs);
}
#IF PREVIOUS WAS PRESSED
if (isset($_POST['btnPrevious'])){
$i = $_POST['txtEmployee'];
$i--;
$query = "select * from tblEmployees where employeeID = $i";
$rs = mysql_query($query);
$row = mysql_fetch_array($rs);
}
?>
<form action="" method="post" id="frmMain"
style="display:inline-block;background-color:lightgray" align ="center">
<table>
<tr>
<input name="txtEmployee" type="text" size="22" maxlength="30" value= <?php echo $i ?>>
<td><p>Title:</p></td>
<td><input type="text" style="border:1px solid" name="txtTitle" size="5"></td>
<td><p>First Name:</p></td>
<td><input name="txtFirstName" style="border:1px solid" type="text" size="15" value='<?php echo $row['firstName'] ?>'></td>
<td><p>Surname:</p></td>
<td><input name="txtLastName" style="border:1px solid" type="text" size="15" value='<?php echo $row['lastName'] ?>'></td>
<td><p>Status:</p></td>
<td><input name="txtStatus" style="border:1px solid" type="text" size="15" value='<?php echo $row['status'] ?>'></td>
</tr>
</table>
<table>
<tr>
<td><input type="submit" name="btnPrevious" value="Previous"/></td>
<td><input type="submit" name="btnNext" value="Next"/></td>
</tr>
</table>
</form>