我有一个页面“ elenco.php”,在其中显示了表中的所有数据库记录,并在末尾的每一行中都有一个“编辑”按钮,该按钮向页面“ edit.php”提交了“ ID”用户要编辑的行。
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();
?>
但是我不明白为什么在页面edit.php中,id值始终是表DB的最后一条记录。
例如,我的表有35条记录,如果我单击页面“ elenco.php”中第10行的编辑按钮,则在页edit.php中,我始终收到ID = 35(数据库的最后一条记录)
你能帮我吗?
答案 0 :(得分:2)
在elenco.php和edit.php中将mysql *函数替换为mysqli *函数。
然后检查post数组中的所有内容,并确保使用来获取所有数据
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<form action='#' method='post' id="4" class='item'>
<div class='item-edit' data-type='fileArray' data-name='file[]' data-maxFiles='1'>LOGO HERE</div>
<div class='item-edit' data-type='input' data-name='agnt[name]' data-require='1'>SOME NAME</div>
<div class='item-edit' data-type='textarea' data-name='agnt[contact]'>SOME CONTACT</div>
<div class='col-md-2 panel-menu'>
<span class='edit_msg'></span>
<a href='#' class='btn btn-success edit_save-btn hidden'>SAVE</a>
<a href='#' class='btn btn-success edit-btn'>EDIT</a>
</div>
</form>
和此代码:
var_dump($_POST);
我假设您的id是整数类型,因此您不必使用'',请尝试:
$query = "SELECT * FROM scadenziario WHERE ID = '$temp' ";
希望这会有所帮助。