2015-07-06 26 views
5

Sto lavorando a un progetto in cui devo ripristinare la funzionalità dei pulsanti. Sfortunatamente non riesco a capire come funziona. Ho creato un pulsante Indietro e funziona. Ma se l'utente desidera modificare il modulo precedente e quindi inviare nuovamente i dati, modifica la query. L'ho provato nel mio progetto ma aggiunge un altro record nel database e non aggiorna quello precedente. So che non sto usando nessuna query di aggiornamento qui. Ora sono bloccato come funzionerebbe Non riesco a pensare ad una buona logica. Ho fatto un semplice modulo per esempio. È scritto sotto.Logica dietro pulsante Indietro

<?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> 

Grazie per la consulenza di esperti in anticipo.

+0

Penso che il modo migliore per farlo sia per noi e sessione php per memorizzare i dati del modulo. – czeski

+0

Ai miei occhi, hai bisogno di un identificatore univoco dell'ultimo record memorizzato. Devi passare questo valore in avanti e indietro alla sessione utente. – reporter

risposta

3

Se ho capito bene ... hai molto lavoro da fare per essere in grado di "AGGIORNARE" un record che hai appena creato.

Non preoccupatevi di provare a tornare "indietro" al modulo originale, andare avanti a un nuovo modulo di "modifica", passando l'ID dal record appena creato.

Utilizzare l'ID per SELEZIONARE quel record dal DB e pre-compilare il modulo 'modifica'. Aggiungi un campo ID nascosto al modulo. Quindi, dopo aver inviato il modulo 'modifica', AGGIORNA il record esistente, utilizzando l'ID.

+0

grazie, ma questa è solo una query di modifica di base o un metodo di base. La cosa principale è che non voglio andare su un'altra pagina, voglio reindirizzare a quella precedente .. –

0

Penso che tu stia prendendo il pulsante "indietro" anche alla lettera. stai emulando il pulsante Indietro del browser. Invece, aggiorna il testo del link per "modificare il record inviato" e modifica la pagina di invio con la possibilità di modificarlo (ad esempio un campo di input nascosto con il valore di indice dell'ultimo elemento creato) o reindirizza a una pagina dedicato alla modifica del record presentato. l'utente vedrà come tornare indietro, ma la funzionalità sarà diversa.

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

Ecco la risposta alla mia question..Hope sarebbe aiutare qualcuno ..

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']; 
    [email protected]$_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> 
Problemi correlati