我想编辑单行而不是全部,但现在我添加了 ID,现在他什么都不做,没有任何改变:(,在我给他 ID 之前,他们编辑所有行。
变量
$titletxt = ($_POST['title_txt']);
$value1 = ($_POST['value1_txt']);
$value2 = ($_POST['value2_txt']);
$ID = ($_POST['id']);
<?php
if( isset($_POST['edit']) ){
$con=mysqli_connect("localhost","root","","hanza");
if (mysqli_connect_errno()){
echo "Neizdevas savienoties ar MySQL: " . mysqli_connect_error();
}
$sql="UPDATE dati SET title='$titletxt', value1='$value1', value2='$value2' WHERE ID = '$ID'";
if (!mysqli_query($con,$sql)){
die('Error: ' . mysqli_error($con));
}
echo '<script>
alert(" Ieraksts ir veiksmigi labots! ");
window.location.href = "index.php";
</script>';
mysqli_close($con);
}
?>
该代码来自表单,我显示来自数据库的结果。
<form action="insert.php" method="post">
<table>
<tr>
<td class="td_opt">Title</td>
<td class="td_opt">Value 1</td>
<td class="td_opt">Value 2</td>
<td class="td_opt"> </td>
</tr>
<tr>
<input type="hidden" name="id" value="">
<td class="td_title"><input type="text" name="title_txt" value=""></td>
<td class="td_value"><input type="text" name="value1_txt" value=""></td>
<td class="td_value"><input type="text" name="value2_txt" value=""></td>
<td class="td_value"></td>
</tr>
<?php
$con=mysqli_connect("localhost","root","","hanza");
if (mysqli_connect_errno()) {
echo "Neizdevas savienoties ar MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM dati");
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<input type='hidden' value='" . $row['ID'] . "'>";
echo "<td><input type='text' value='" . $row['title'] . "'></td>";
echo "<td><input type='text' value='" . $row['value1'] . "'></td>";
echo "<td><input type='text' value='" . $row['value2'] . "'></td>";
echo "<td><button name='edit' class='frm_btns'>Edit</button></td>";
echo "</tr>";
}
mysqli_close($con);
?>
</table>
<div class="atp"></div>
<button class="frm_btns" name="pievienot">New</button> // add new row
</form>
也许某些变量不能只编辑一行?