Display different rows of the database at the click of a button

I have a mosque database with the following fields: mosque_id, mosque_name, prefecture, address, zip code, website, email1, phone1. What I want to do is display information about the first mosque (mosque_id = 1) in the form, and when I click Next, information about the second mosque (mosque_id = 2) will be displayed.

So far, I could use mosque_id to select an entire line and display information, but I struggled to find a way to switch to the next. I am very new to PHP and MYSQL, any help would be appreciated.

<? $mosque_id = 1; $result = mysql_query("SELECT * FROM Mosque WHERE mosque_id='$mosque_id'"); $row = mysql_fetch_assoc($result); ?> <form action="insert.php" method="post"> Mosque Name: <?php print('<input type="text" name="firstname" value="' . $row['mosque_name'] . '"/><br>')?> Prefecture: <?php print('<input type="text" name="firstname" value="' . $row['prefecture'] . '"/><br>')?> Address: <?php print('<input type="text" value="' . $row['address'] . '"/><br>')?> Postal Code: <?php print('<input type="text" value="' . $row['postal_code'] . '"/><br>')?> Website: <?php print('<input type="text" value="' . $row['website'] . '"/><br>')?> Email 1: <?php print('<input type="text" value="' . $row['email1'] . '"/><br>')?> Contact number 1: <?php print('<input type="text" value="' . $row['phone1'] . '"/><br>')?> <input type="button" value="Next Mosque" onclick=""/> </form> 
+4
source share
3 answers
  <?php $mosque_id = (isset($_GET['mosque_id']) ? intval($_GET['mosque_id']) : 1); // suppose you know that '1' is the first id in your table $result = mysql_query("SELECT * FROM Mosque WHERE mosque_id>='$mosque_id' limit 2"); $row = mysql_fetch_assoc($result); // this is the row you're gonna display in the form $row2 = mysql_fetch_assoc($result); // this will tell us about the next row ?> <form action="insert.php" method="post"> Mosque Name: <?php print('<input type="text" name="firstname" value="' . $row['mosque_name'] . '"/><br>')?> Prefecture: <?php print('<input type="text" name="firstname" value="' . $row['prefecture'] . '"/><br>')?> Address: <?php print('<input type="text" value="' . $row['address'] . '"/><br>')?> Postal Code: <?php print('<input type="text" value="' . $row['postal_code'] . '"/><br>')?> Website: <?php print('<input type="text" value="' . $row['website'] . '"/><br>')?> Email 1: <?php print('<input type="text" value="' . $row['email1'] . '"/><br>')?> Contact number 1: <?php print('<input type="text" value="' . $row['phone1'] . '"/><br>')?> <input type="button" value="Next Mosque" onclick="window.location = 'path-to-your-page?mosque_id=<?php echo $row2['mosque_id'];"/> </form> 
+4
source

First: use mysqli functions or the PDO class, not mysql functions, they are deprecated.

Second: use long PHP tags

But you are on the right track:

Replace the first line as follows:

 $mosque_id = (isset($_GET['mosque_id']) ? intval($_GET['mosque_id']) : 1); 

And in the end

 <input type="button" value="Next Mosque" onclick="javascript: document.location = '?mosque_id=<?php echo ($mosque_id + 1);?>"/> 

You also need to write some checks after the request, if such an identifier exists and in fact, if you develop further, you also need to check the following mosque_id, since if you delete several lines, id-s may not be in the correct order as 1, 2, 3, 4, but 1, 3, 7, 9 (hint: for the next use id WHERE mosque_id> current_id ORDER BY mosque_id DESC LIMIT 1 in MySQL)

+1
source

Just enter the hidden input containing the identifier and increase it when the page loads. Then, when you submit, an identifier will also be sent.

 <? if (isset($_POST['id']) { $id = $_POST['id']; } else { $id = 1; } $result = mysql_query("SELECT * FROM Mosque WHERE mosque_id='$id'"); ... ?> <form action="insert.php" method="post"> ... <input type='hidden' name='id' value='<?php echo $mosque_id++; ?> /> <input value="Next Mosque" /> </form> 

And by the way, use PDO . It is very easy to learn and adds a huge level of security to your application.

+1
source

Source: https://habr.com/ru/post/1445716/


All Articles