dangar Posted September 20, 2017 Share Posted September 20, 2017 I want to have a next button on my Productdetails.php page that would show the next image fetched from the database using while loop. Below is the productdetails.php page shows details of a particular product. I am using simple php format, How can I go about it? I have come this far at this point. <div class = "uniqueproduct"> <?php if (isset($_GET['id'])) { $id = $_GET['id']; $query1 = mysqli_query($conn, "select * from products where PRODUCT_ID = $id"); while ($row1 = mysqli_fetch_array($query1)) { ?> <div class = "singleproduct"> <?php echo '<img src = "data:image/jpeg;base64,'.base64_encode($row1['PRODUCT_IMAGE']).'" alt = "" width = "250px" height ="300px"/>'; ?> </div> <div class = "productName"> <?php echo $row1['PRODUCT_NAME']; ?> </div> <div class = "productDescription"> <?php echo $row1['PRODUCT_DESCRIPTION'];?> </div> <div class = "productPrice"> <?php echo $row1['PRODUCT_PRICE']; ?> </div> <?php } } ?> <button class = "btnPicture"> ..............</button> </div> Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 20, 2017 Share Posted September 20, 2017 If you're not doing so already, you should look into using prepared statements. You'll want to avoid use the raw GET variable, at least one that's not tested, in a query. Using the raw (untested) value makes the query susceptible to SQL injection attacks, among other issues. Assuming $_GET['id'] should contain a number, you can make sure it does with ctype_digit(). More information can be found here: http://php.net/manual/en/function.ctype-digit.php Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 20, 2017 Share Posted September 20, 2017 As for your question, I assume that the button will lead to the same productdetails.php page. You could run another query to get the ID for the next record. Then just create a button (or link) to pass the value. Note: MySQL doesn't always return the values the same order. The order it uses doesn't always make sense. For the next-record feature to work, you'll want to look into sorting (ORDER BY clause). Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 20, 2017 Share Posted September 20, 2017 You state that you want to use a button to get your next record and that you want to use a while loop. Uh... that won't work since your button is on the client and your while loop would be on the server. As indicated above you will display a single record that has a key in it. Somehow you will need to pass that key back to the server/script and use it to query for the 'next' record, whatever it is. There would be no loop involved. 1 Quote Link to comment Share on other sites More sharing options...
dangar Posted September 20, 2017 Author Share Posted September 20, 2017 Thanks for that, cyberRobot. I've tried this : <a href="Productdetails.php?id=$id + 1">Next</a> shows error. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 20, 2017 Share Posted September 20, 2017 (edited) PHP variables need to be enclosed with PHP tags. You could try something like the following: <a href="Productdetails.php?id=<?php echo $id + 1; ?>">Next</a> Or <a href="Productdetails.php?id=<?php echo $id++; ?>">Next</a> Note: you'll want to double check the ID exists before displaying the Next link. Edited September 20, 2017 by cyberRobot Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 20, 2017 Share Posted September 20, 2017 Note: you'll want to double check the ID exists before displaying the Next link. Correct. There is no guarantee that the next record will be +1. A better solution is to provide the actual ID of the next record. Use a query such as this: SELECT * FROM table WHERE id >= [passed_id] ORDER BY id LIMIT 2 Use a 0 for the first request to start the process. Then use the first record to display the current record. If a 2nd record is returned, use its id in the link/button to get the next record. If no second record is returned, then don't provide a link/button for the next record. 1 Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 20, 2017 Share Posted September 20, 2017 Correct. There is no guarantee that the next record will be +1. A better solution is to provide the actual ID of the next record. Use a query such as this: SELECT * FROM table WHERE id >= [passed_id] ORDER BY id LIMIT 2 Good point. I was only thinking about the last record. There's a good chance that there will be gaps in the middle, eventually. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.