我也尝试更新任何其他行时不会更新,但只更新最后一行......
<?php
session_start();
if( isset($_SESSION['username']) ){
include('../CIEcon.php');
echo "<form action= 'adminCleaning.php' method = 'post'>" ;
if(isset($_POST['update'])){
if( isset($_POST['id']) ){
if( empty($_POST['id']) || $_POST['id'] == 0 ){
echo"<h4> please choose something to delete </h4>";
}else{
echo $implid = implode("' , '", $_POST['id']);
$sqlUpdate = "UPDATE Cleaning SET JobName= '$_POST[jobname]',Description= '$_POST[description]',NoStudent='$_POST[nostudent]',DueDate='$_POST[duedate]' WHERE Id IN('" . $implid . "')";
$resultUpdate = mysqli_query($dbCIE,$sqlUpdate )or die(mysqli_error($dbCIE));
if (mysqli_affected_rows($dbCIE) > 0) {
echo "You have successfully updated your data.<br><br>";
}
else {
echo "The data you submitted matched the current data so nothing was changed.<br><br>";
}
} // end of else..
} // end of if isset($_POST['id']) ...
} // end of if isset($_POST['update']) ...
$sql = "SELECT * FROM Cleaning ";
$result = mysqli_query($dbCIE, $sql) or die(mysqli_error($dbCIE));
/// NOW DISPLAY ALL INFO FROM CHOSEN DATABASE...
echo "
<table cellpadding ='4' border='1' width='80%' align='center'>
<tr>
<th class='tt' >Check </th>
<th class='tt'> Job's Name</th>
<th class='tt' >Description</th>
<th class='tt' > No Students needed</th>
<th class='tt' >Due Date</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<br>";
echo "<tr>";
echo "<td> <input type='checkbox' name='id[]' value='". $row['Id'] ."' /> </td>"; // array[] cause to edit more than one record...
echo "<td><input type='text' name='jobname' value='" . $row['JobName'] . "'> </td>";
echo "<td><input type='text' name='description' value='" . $row['Description'] . "'> </td>";
echo "<td><input type='text' name='nostudent' value='" . $row['NoStudent'] . "'> </td>";
echo "<td><input type='text' name='duedate' value='" . $row['DueDate'] . "'> </td>";
echo "</tr>";
}
echo "</table>";
/// END THE SEARCH HERE...........
echo " <br>
<div align='center'>
<input type='reset' value='clear' />
<input type='submit' name='update' value='update' />
</div> ";
mysqli_close($dbCIE);
echo "</form>";
}
else{echo "must logout to see this page..!!";}
?>
<html>
<head><title> ..Cleanding.... </title></head>
<style type="text/css">
body{
margin-top: 70px; /*space above the table....*/
background-color: #23438e;
}
table{
background-color: white;
}
.tt{
background: #f26822;
color: white ;
}
</style>
<body>
<!-- <a href= "../AdminIndex.php" > <button> Main Page </button></a> -->
</body>
</html>
答案 0 :(得分:1)
问题是您的所有输入都具有相同的名称,并且您使用 IN 进行更新,而不是 =
更改输入标记以遵循此模式:
<input type='text' name='jobname[".$row['Id']."]' value='" . $row['JobName'] . "'>
以及使用此模式的SQL:
JobName= '$_POST[jobname][$id]' ... WHERE Id = $id
如果一次发送多个ID,您需要循环遍历所有输入并运行查询。
应将HTML表的每一行发送到具有唯一ID的服务器,并且应使用适当的数据和id将每个更新发送到数据库。