我覺得這個問題有一個簡單的解決方案,但我無法弄清楚。我正在使用while循環顯示我的數據庫的字段。每個while循環結束時都是管理員可以批准用戶的表單輸入。 (所有批准的需求都是在該特定記錄的批准列中將1更改爲2)。更新單數據庫記錄,使用PHP雖然循環
所有批准的記錄都被更改而不是我想要的特定記錄。我的問題是如何一次選擇和更改一條記錄。 (獲得批准的記錄)。非常感謝你爲我提供的任何幫助。
<?php
//select the database to write to
$unapprovedTeachers = mysql_query("SELECT * FROM members_teachers WHERE approved = '1'", $dbc) or die("Could not select the unapproved teachers at this time.");
//While loop to cycle through the rows
while($row = mysql_fetch_assoc($unapprovedTeachers)){
$teacher_id = $row['id'];
echo $teacher_id;
?>
<ul class="admin-fields">
<?php
foreach($row as $field){
if(empty($field)){
echo "....";
}
print '<li>' .$field.' </li>';
}//End For Each Loop
//print $teacher_id;
?>
</ul>
<footer>
<?php
if(isset($_POST['approve'])){
mysql_query("UPDATE members_teachers SET approved = '2' WHERE id= ".$teacher_id."", $dbc) or die ("Oops something went wrong");
}
?>
<ul>
<li>
<form method="post">
<button name="approve" id="approve" type="submit">approve</button>
</form>
</li>
</ul>
</footer>
<!--End New Row-->
</section>
<?php
}//End While Lopp
mysql_close($dbc);
?>
</div>
您的表單上的哪個位置是您要說要更新哪個ID? – Anigel
這就是您需要將演示文稿與處理分開的原因。並追加'LIMIT 1;'到你的更新。 'POST'處理不是內聯完成的,而是在腳本的頂部,在任何輸出之前進行處理,以便您有機會重定向。 – CodeAngry