1

I have a page "elenco.php" where i show all DB record in a table and in each row at the end i have a button "edit" that submit to the page "edit.php" the "ID" of the row that the user would like to edit .

elenco.php

  <?php 
    include '/var/www/phpMyEdit/open_dati.php';


    $query = "SELECT * FROM scadenziario"; 
    $result= mysql_query($query);


    echo "<h2>Elenco Scadenziario</h2>";

        echo "<table border>";
            echo "<tr><td>ID</td><td>Aspetto Generale</td><td>Descrizione</td><td>Ragione Sociale</td><td>Numero Civico</td><td>Validita</td><td>Data</td><td>Preavviso</td><td>Scadenza</td><td>Prescrizioni</td><td>Frequenza</td><td>Data Controllo</td><td>Prossimo Controllo</td><td>Note</td>";
                while($row = mysql_fetch_array($result)){
                    echo "<tr><td>" . $row[ID] . 
                    "</td><td>". $row[Aspetto_Generale]. 
                    "</td><td>". $row[Descrizione].
                    "</td><td>". $row[Ragione_Sociale].
                    "</td><td>". $row[Num_Civico].
                    "</td><td>". $row[Validita].
                    "</td><td>". $row[Data].
                    "</td><td>". $row[Preavviso].
                    "</td><td>". $row[Scadenza].
                    "</td><td>". $row[Prescrizioni].
                    "</td><td>". $row[Frequenza].
                    "</td><td>". $row[Data_Controllo].
                    "</td><td>". $row[Prox_Controllo].
                    "</td><td>". $row[Note].
                    "</td><td><form action='edit.php' method='POST'><input type='hidden' name='tempID' value='".$row['ID']."'/><input type='submit' name='submit-btn' value='Edit' /><form></td></tr>";
                }
        echo "</table>";

    mysql_close();
    ?> 

edit.php

 <?echo'
        <html>
        <head>
        <link rel="stylesheet" type="text/css" href="form.css">
        </head>';

        include '/var/www/phpMyEdit/open_dati.php';

        $temp = $_POST['tempID'];

        $query = "SELECT * FROM scadenziario WHERE ID = '$temp' "; 
        $result= mysql_query($query);

            while($row = mysql_fetch_array($result)){
                    $aspettogen = $row['Aspetto_Generale'];
                    $desc = $row['Descrizione'];
                    $ragsoc = $row['Ragione_Sociale'];
                    $numcivico = $row['Num_Civico'];
                    $validita = $row['Valdita'];
                    $odierna = $row['Data'];
                    $preavviso = $row['Preavviso'];
                    $scadenza = $row['Scadenza'];
                    $presc = $row['Prescrizioni'];
                    $freq = $row['Frequenza'];
                    $datacontr = $row['Data_Controllo'];
                    $proxcontr = $row['Prox_Controllo'];
                    $note = $row['Note'];
            }?>



    <? echo'
        <body>
            <div class="container">  
                <form id="contact" method="POST">'?>



                <? echo'
                                <img src="logo.jpg"> 
                                <fieldset>
                                <input name="aspettogen2" type="text" value="'.$aspettogen.'">
                                </fieldset>
                                <fieldset>
                                    <input name="desc" type="text" maxlength="255" value="'.$desc.'">
                                </fieldset>
                                <fieldset>
                                    <input name="ragsoc" type="text" maxlength="100">
                                </fieldset>
                                <fieldset>
                                    <input name="numcivico"  type="text" maxlength="20">
                                </fieldset>
                                <fieldset>
                                    <input name="validita" type="text">
                                </fieldset>
                                <fieldset>
                                    <input name="odierna" type="data">
                                </fieldset>
                                <fieldset>
                                    <input name="preavviso" type="text">
                                </fieldset>
                                <fieldset>
                                    <input name="scadenza" type="text">
                                </fieldset>
                                <fieldset>
                                    <input name="presc" type="text" maxlength="255">
                                </fieldset>
                                <fieldset>
                                    <input name="freq" type="number">
                                </fieldset>
                                <fieldset>
                                    <input name="datacontr" type="text">
                                </fieldset>
                                <fieldset>
                                    <input name="proxcontr" type="text">
                                </fieldset>
                                <fieldset>
                                    <input name="note" type="text">
                                </fieldset>             
                            </form>
                        </div>
                    </body>
                </html>';

    mysql_close(); 
    ?>

but i cant understand why in the page edit.php the id value is always the last record of the table DB.

for example my table has 35 record, if i click the edit button of the row 10 in the page "elenco.php", in the page edit.php i receive always ID = 35 ( the last record of the DB).

Can u help me guys?

12
  • 1
    Did you check the source code? Commented Jul 2, 2018 at 6:55
  • Do you see 35 on all hidden inputs on your browser console? Commented Jul 2, 2018 at 6:56
  • in the console of the page i see the correct value on each button edit, but in the page edit.php i receive always the last record Commented Jul 2, 2018 at 6:58
  • echo "<tr><td>ID</td><td> //table; missing closing quotes there Commented Jul 2, 2018 at 7:00
  • there are few quote (' and ") mistakes in elenco.php file. Correct it and check again. Commented Jul 2, 2018 at 7:02

1 Answer 1

1

Replace your mysql* functions for mysqli* functions in elenco.php and edit.php.

then check everything inside the post array, and make sure you're getting all the data with

var_dump($_POST);

and this code:

$query = "SELECT * FROM scadenziario WHERE ID = '$temp' "; 

i assume your id is an integer type, so you don't have to use ' ', try:

 $query = "SELECT * FROM scadenziario WHERE ID = $temp "; 

hope this can help.

Sign up to request clarification or add additional context in comments.

3 Comments

array(2) { ["tempID"]=> string(2) "35" ["submit-btn"]=> string(4) "Edit" } i receive 35 i dont understand why
ok, did you try replacing the mysql with mysqli functions? mysql is deprecated, so it might give you some problems (you need to pass the connection link as the first param of the mysqli_query($connection, $query))
i just noticed something, in your elenco.php, in the form with the id value, you're not closing the form, you have <form><form>, and should be </form>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.