下午好,
我正在建立一个期刊,我有一个单独的div。
每个div元素显示一个不同的日志,其中包含div中每个日志的特定数据。
对于每个日志,我有一个提交按钮,允许更新和编辑表单。
正在发生的事情是,当我提交表单时,它正在更新我的数据库中的每个条目,而不是仅仅单个条目。 我知道麻烦在于我的foreach循环,但我不确定如何解决这个问题。
非常感谢任何帮助或指示。
以下代码:
import java.util.LinkedHashMap;
import java.util.HashMap;
class LinkedHasMapDemo
{
@SuppressWarnings("unchecked")
public static void main(String[] args)
{
LinkedHashMap l = new LinkedHashMap();
//{116=kumar, 116=kumar, kumar=kumar, 117=Ram charan, 105=Yash}
//HashMap l = new HashMap();
//{116=kumar, 117=Ram charan, 116=kumar, kumar=kumar, 105=Yash}
l.put("116","kumar"); //key is String Object
l.put(116,"kumar"); //key is Integer Object
l.put("kumar","kumar");
l.put(117,"Ram charan");
l.put(105,"Yash");
System.out.println(l);
}
}
提交按钮的PHP:
<?php
$sqlGrow = "SELECT * FROM grow_details ";
$query = $conn->query($sqlGrow);
$grows = array();
while ($grow = mysqli_fetch_assoc($query) ) {
$grows[] = $grow;
}
foreach($grows as $grow) {
$id = $grow['id'];
$growName = $grow['name'];
?>
<div class="container">
<div class="details">
<h2><?php echo $grow['name']; ?></h2>
<p class="growNum">Grow #: <?php echo $id; ?></p>
<table class="growDetails">
<form method="POST" action="">
<tr>
<td class="label_growDetails"><label for="datePlanted">Date Planted:</label></td>
<td><input type="text" name="edit_datePlanted" id="edit_datePlanted" value="<?php echo $grow['datePlanted']; ?>" />
</tr>
<tr>
<td class="label_growDetails"><label for="strain">Strain:</label></td>
<td><input type="text" name="edit_strain" id="edit_strain" value="<?php echo $grow['strain']; ?>" /></td>
</tr>
<tr>
<td class="label_growDetails"><label for="toMaturity">Days to mature:</label></td>
<td><input type="text" name="edit_toMaturity" id="edit_toMaturity" value="<?php echo $grow['toMaturity']; ?>" /></td>
</tr>
<tr>
<td class="label_growDetails"><label for="type">Type:</label></td>
<td><input type="text" name="edit_type" id="edit_type" value="<?php echo $grow['type']; ?>" /></td>
</tr>
<tr>
<td class="label_growDetails"><label for="gender">Gender:</label></td>
<td><input type="text" name="edit_gender" id="edit_gender" value="<?php echo $grow['gender']; ?>" /></td>
</tr>
<tr>
<td class="label_growDetails"><label for="medium">Medium:</label></td>
<td><input type="text" name="edit_medium" id="edit_medium" value="<?php echo $grow['medium']; ?>" /></td>
</tr>
<tr>
<td class="label_growDetails"><label for="watts">Watts:</label></td>
<td><input type="text" name="edit_watts" id="edit_watts" value="<?php echo $grow['watts']; ?>" /></td>
</tr>
<tr>
<td class="label_growDetails"><label for="lightType">Light Type:</label></td>
<td><input type="text" name="edit_lightType" id="edit_lightType" value="<?php echo $grow['lightType']; ?>" /></td>
<td class="edit"><input type="submit" name="submit_editGrowDetails" id="submit_editGrowDetails" value=" Save Edits " /></td>
</tr>
</form>
</table>
</div>
答案 0 :(得分:2)
您应该始终按表的ID
更新,而不是名称,因为有些名称可能相同。
我们说你有:
ID | Name
1 | Test 1
2 | Test 2
3 | Test 1
使用您现在拥有的更新查询,如果您更新&#34;测试1&#34;,它将同时更新&#34;测试1&#34; ID为1和3。
要解决这个问题,在你的html表单上,你应该在表单下面放一个隐藏的字段,如下所示:
<form method="POST" action="">
<input type="hidden" value="<?php echo $grow['id']; ?>" name="edit_id">
...
...
rest of code
这将显示您正在编辑的项目的ID。
然后在php方面,你应该这样做:
<?php
if(isset($_POST['submit_editGrowDetails'])){
$edit_datePlanted = mysqli_real_escape_string($conn, $_POST['edit_datePlanted']);
$edit_strain = mysqli_real_escape_string($conn, $_POST['edit_strain']);
$edit_toMaturity = mysqli_real_escape_string($conn, $_POST['edit_toMaturity']);
$edit_type = mysqli_real_escape_string($conn, $_POST['edit_type']);
$edit_gender = mysqli_real_escape_string($conn, $_POST['edit_gender']);
$edit_medium = mysqli_real_escape_string($conn, $_POST['edit_medium']);
$edit_watts = mysqli_real_escape_string($conn, $_POST['edit_watts']);
$edit_lightType = mysqli_real_escape_string($conn, $_POST['edit_lightType']);
$id = mysqli_real_escape_string($conn, $_POST['edit_id']);
$name = $grow['name'];
$edit_growDetails = "UPDATE grow_details
SET datePlanted = '$edit_datePlanted',
strain = '$edit_strain',
toMaturity = '$edit_toMaturity',
type = '$edit_type',
gender = '$edit_gender',
medium = '$edit_medium',
watts = '$edit_watts',
lightType = '$edit_lightType'
WHERE id = '$id'; ";
$query_edit_growDetails = $conn->query($edit_growDetails);
if($query_edit_growDetails) {
echo '<p class="success" id="success">Successfully updated log for '.$name.'! <a class="refresh" href="journal.php">Refresh</a></p>';
} else {
echo '<p class="error" id="error">There was an error: '. $conn->error .'</p>';
}
}
?>
答案 1 :(得分:0)
我认为您的问题在于提交处理程序中的以下行:
$name = $grow['name'];
如果引用此$grow
,我看不到,但您应该在表单中将该名称添加为隐藏输入。这样处理程序就知道要更新哪个元组。例如:
<input type="hidden" value="<?php echo $grow['name']; ?>" name="grow_name">
然后,您可以像表格中的任何其他数据一样访问它,即
$name = mysqli_real_escape_string($conn, $_POST['grow_name']);
更多建议:
最后@Carlos,@ Riggsfolly,我不是想复制你的答案。我以为我可以更好地构建答案。