我试图显示和更新一次的所有记录表。我可以成功显示记录。但是我无法更新我的数据库,请帮助我更新表格中的记录
<?php while($rows=mysqli_fetch_array($result)){ ?>
<tr>
<td align="center">
<input name="id[]" type="text" id="name" value="<?php echo $rows['id']; ?>">
</td>
<td align="center">
<input name="name[]" type="text" id="name" value="<?php echo $rows['name']; ?>">
</td>
<td align="center">
<input name="lastname[]" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>">
</td>
<td align="center">
<input name="email[]" type="text" id="email" value="<?php echo $rows['email']; ?>">
</td>
</tr>
<?php } ?>
<tr>
<td colspan="4" align="center">
<input type="submit" name="Submit" value="Submit">
</td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php if(isset($_POST[ 'Submit'])) { $id_array=$_POST[ 'id']; $name_array=$ _POST[ 'name']; $year_array=$_POST[ 'lastname']; $emil_array=$_POST[ 'email']; for($i=0;$i<$count;$i++){ $sql1="UPDATE test_mysql SET name='$name_array[$i]', lastname='$year_array[$i]', email='$emil_array[$i]' WHERE id='id_array[$i]'"
; } mysqli_close($con); } ?>
答案 0 :(得分:0)
您的表单没有操作处理程序。
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
答案 1 :(得分:0)
尝试:
HTML:
<table>
<form name="form1" method="post"> <!-- add form tag -->
<?php while($rows=mysqli_fetch_array($result)){ ?>
<tr>
<td align="center">
<input name="id[]" type="text" id="name" value="<?php echo $rows['id']; ?>">
</td>
<td align="center">
<input name="name[]" type="text" id="name" value="<?php echo $rows['name']; ?>">
</td>
<td align="center">
<input name="lastname[]" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>">
</td>
<td align="center">
<input name="email[]" type="text" id="email" value="<?php echo $rows['email']; ?>">
</td>
</tr>
<?php } ?>
<tr>
<td colspan="4" align="center">
<input type="submit" name="Submit" value="Submit">
</td>
</tr>
</table>
</form>
</table>
PHP:
if(isset($_POST['Submit'])) {
$id_array=$_POST['id'];
$name_array=$ _POST['name'];
$year_array=$_POST['lastname'];
$emil_array=$_POST['email'];
for($i=0;$i<$count;$i++){
$id = $name_array[$i];
$name = $year_array[$i];
$year = $emil_array[$i];
$email = $id_array[$i]; // use $id_array[$i] to access id, as in above code you did not use $ in id_array
$sql="UPDATE test_mysql SET name='$name', lastname='$year', email='$email' WHERE id='$id'";
if ($conn->query($sql) === TRUE) { // execute query like this
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
}
$conn->close();
}
答案 2 :(得分:0)
在您的表单中,您没有提及任何操作处理程序。代码必须是
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
答案 3 :(得分:0)
希望这会对你有所帮助,我没有把mysql_escape_string放在你仍在启动但它有助于逃避mysql攻击。还包装
<form>
if(isset($_POST['Submit'])) {
$id_array= $_POST['id'];
$name_array=$ _POST['name'];
$year_array=$_POST['lastname'];
$emil_array=$_POST['email'];
if($id_array){
foreach($id_array as $key=>$id_val){
$name = $name_array[$key];
$year = $year_array[$key];
$email = $emil_array[$key];
$sql = "update activity_logs set name='{$name}' , lastname = '{$year}' , email='{$email}' where activity_logs_id = {$id_val}";
$res = mysql_query($sql);
}
}
}