Logical button behind

I am working on a project in which I have to return the functionality of a button. Unfortunately, I can’t figure it out. I created a back button and it works. But if the user wants to edit the previous form and then send the data again, then he is editing the request. I tried this in my project, but it adds another record to the database and does not update the previous one. I know that I do not use any update requests here. Now I'm stuck, how would it work, I can’t think of good logic. For example, I made a simple form. It is written below.

<?php $con = mysqli_connect('localhost','root','','test1'); if (isset($_POST['sub'])) { $first_name = $_POST['fname']; $second_name = $_POST['lname']; $sql = "INSERT INTO `tbl`(`first_name`, `last_name`) VALUES ('$first_name', '$second_name')"; $run = mysqli_query($con, $sql); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form method="POST" action="nextpage.php"> First_name: <input type="text" name="fname"> Second_name: <input type="text" name="lname"> <input type="submit" name="sub" value="submit"> </form> </body> </html> 

nextpage.php

 <?php $con = mysqli_connect('localhost','root','','test1'); ?> <html> <body> <h1>Next Page</h1> <p>The query is submitted press ok to forward and back if you want to go back</p> <button>OK</button> <button onclick="history.go(-1);">Back</button> </body> </html> 

Thank you for the expert advice.

+5
source share
3 answers

If I understand correctly ..... you have a lot of work to be able to “UPDATE” the record you just created.

Do not try to "return" to the original form, go to the new "change" form, passing the identifier from the record you just created.

Use the identifier to select this entry from the database and pre-fill the "Edit" form. Add a hidden id field to the form. Then, when submitting the “Change” form, UPDATE an existing record using the identifier.

+3
source

I think you take the back button too literally. you emulate the back button. Instead, update the link text to "edit the provided entry", and either change the send page with the ability to edit it (for example, a hidden input field with the index of the last element created), or redirect the page dedicated to editing the submitted entry. the user will see it as returning, but the functionality will be different.

SubmitForm:

 <?php $edit = false; if(isset($_GET['editid']){ $edit = true; //get the record, and display it in the fields below } $con = mysqli_connect('localhost','root','','test1'); if (isset($_POST['sub'])) { $first_name = $_POST['fname']; $second_name = $_POST['lname']; $sql = "INSERT INTO `tbl`(`first_name`, `last_name`) VALUES ('$first_name', '$second_name')"; $run = mysqli_query($con, $sql); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form method="POST" action="nextpage.php"> First_name: <input type="text" name="fname" <?=($edit)?"value=\"".$req["fname"]."\":""?>> Second_name: <input type="text" name="lname"> <input type="submit" name="sub" value="submit"> </form> </body> </html> 

nextpage.php

 <?php $con = mysqli_connect('localhost','root','','test1'); ?> <html> <body> <h1>Next Page</h1> <p>The query is submitted press ok to forward and back if you want to go back</p> <button>OK</button> <button onclick="submitform.php?editid=<?=mysqli_last_return_id()?>">Back</button> </body> </html> 
0
source

Here is the answer to my own question ... I hope this would help someone ..

test.php

  <?php session_start(); $con=mysqli_connect('localhost','root','','test1'); if(isset($_POST['sub'])){ $first_name=$_POST['fname']; $second_name=$_POST['lname']; $sess_username=$_SESSION['user_name']; $sql="INSERT INTO `tbl`(`first_name`, `last_name`,`sess_username`) VALUES ('$first_name','$second_name','$sess_username')"; $run=mysqli_query($con, $sql); header("location:nextpage.php"); } if(isset($_POST['upd'])){ $first_name=$_POST['fname']; $second_name=$_POST['lname']; $id=@ $_GET['id']; $upd="UPDATE `tbl` SET `first_name`='$first_name',`last_name`='$second_name' WHERE `id`='$id'"; $query=mysqli_query($con, $upd); header("location:nextpage.php"); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="http://code.jquery.com/jquery.js"> </script> <script type="text/javascript"> $(document).ready(function () { if(window.location.href.indexOf("id") > -1) { $('.submit').hide(); $('.update').show(); } }); </script> <title>Untitled Document</title> </head> <body> <form method="POST" action=""> First_name:<input type="text" name="fname"> Second_name:<input type="text" name="lname"> <input class="update" type="submit" value="Update" name="upd" style="display:none;"> <input class="submit" type="submit" name="sub" value="submit"> </form> </body> </html> nextpage.php <?php session_start(); $con=mysqli_connect('localhost','root','','test1'); $sql="SELECT * FROM `tbl` WHERE `sess_username`='".$_SESSION['user_name']."'"; $run=mysqli_query($con, $sql); while($res=mysqli_fetch_assoc($run)){ $id=$res['id']; $firstname=$res['first_name']; $lastname=$res['last_name']; $sess=$res['sess_username']; } ?> <html> <body> <h1>Next Page</h1> <p>The query is submitted press ok to forward and back if you want to go back</p> <button >OK</button> <form method="POST" action="test.php"> <?php echo "<a href='test.php?id=$id;'>Back</a>"; ?> </form> </body> </html> 
0
source

All Articles