Reputation: 57
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?
Upvotes: 1
Views: 187
Reputation: 470
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.
Upvotes: 1