我已经清理了我的代码,以便将以下内容作为我的表单。但我在发送数据和从新的update.php更新时遇到了问题。该表单可以正常检索数据并显示它。但是在提交时我得到了正确的更新消息,但记录在数据库中没有任何改变。 的index.php
<?php
include 'connectdb.php';
// include 'query.php';
$sql = "SELECT id, WeightorMeasure FROM weightsmeasures";
$result = $conn->query($sql)
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<form action=\"update.php\"method=\"post\">";
echo "<input type=\"text\" name=\"id\" value = ".$row["id"].">";
echo "<input type=\"text\" name=\"WeightorMeasure\" value = ".$row["WeightorMeasure"] .">";
echo "<input type=\"submit\" value=\" Submit \" name=\"Update\">";
}
echo "</form>";
} else {
echo "0 results";
}
$conn->close();
?>
update.php
<?php
include 'connectdb.php';
$wm = $_POST['id'];
$id = $_POST['WeightorMeasure'];
$sql = "UPDATE weightsmeasures SET WeightorMeasure='$wm' WHERE id='$id'";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
$conn->close();
?>
已更改为“下方”,现在出现此错误。 更新记录时出错:未知栏&#39; sdada&#39;在&#39;字段列表&#39;。所以看起来它试图使用表格值$ wm作为表格中的列标题而不是输入值。
$wm = $_POST['WeightorMeasure'];
$id = $_POST['id'];
$sql = "UPDATE weightsmeasures SET WeightorMeasure=$wm WHERE id=$id";
答案 0 :(得分:7)
$wm = $_POST['id'];
$id = $_POST['WeightorMeasure'];
也许你有这些错误的方式? :d
$wm = $_POST['WeightorMeasure'];
$id = $_POST['id'];
顺便说一句,你的查询是针对MySQL注入的,我们请考虑使用预备语句
答案 1 :(得分:3)
您是否意识到您已从$ _POST数据中更改了变量分配中的ID和WeightOrMeasure? 这会导致更新查询无法找到ID但不会遇到问题。从而告诉你操作成功了
答案 2 :(得分:0)
我发现代码中存在以下可能的问题。
String contact_str = "Contact: " + name;
String email_str = "Address: " + email;
String age_str = "Age: " + age;
DisplayAlert(contact_str +"\n"+ email_str +"\n"+ age_str);
和这一行
mysqli_query($conn, $sql); //should be $conn->query($sql);
订单错了。我希望你已经在dpconnect.php文件中创建了$ conn对象。
答案 3 :(得分:-1)
好的发现问题是上面有$ _POST [&#34; WeightorMeasure&#34;]的混合物。和$ _POST [&#34; id&#34;];混淆了,但最重要的因素是我从包含多行发布的表和在_POST上发布到update.php,它不知道如何处理所有不同的行,因为SQL只处理一行。一旦我通过帖子发送单行,它工作正常。现在按照建议学习和添加准备好的语句。
update.php
<?php
include 'connectdb.php';
$wm = $_POST["WeightorMeasure"];
$id = $_POST["id"];
echo $wm . "<br>";
echo $id . "<br";
$sql = "UPDATE weightsmeasures SET WeightorMeasure=\"$wm\", id=
$idWHERE id= $id";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
$conn->close();
?>
手动单输入。 的index.php
<?php
include 'connectdb.php';
// include 'query.php';
$sql = "SELECT id, WeightorMeasure FROM weightsmeasures WHERE id=11";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<form action=\"update.php\"method=\"post\">";
echo "<input type=\"text\" name=\"id\" value = ".$row["id"].">";
echo "<input type=\"text\" name=\"WeightorMeasure\" value = ".$row["WeightorMeasure"].">";
echo "<input type=\"submit\" value=\" Submit \" name=\"Update\">";
}
echo "</form>";
} else {
echo "0 results";
}
$conn->close();
?>