gpt4 book ai didi

php - MySQL更新查询创建新表行而不是更新表行

转载 作者:行者123 更新时间:2023-11-29 22:16:02 25 4
gpt4 key购买 nike

当尝试在 PHP 中为表格创建编辑页面时,我在 edit.php 页面上遇到问题,当我单击编辑表格行按钮时,它显然会将我带到正确的页面(edit.php ),然后当我输入编辑后的详细信息并提交它时,它会生成一个全新的表行条目,而不是更新我选择要更新的条目。

我已检查以确保 id 已正确设置为数据库中正确的表行,确实如此。我不知道它为什么这样做。任何帮助将不胜感激。

<?php require("manage_post.php"); ?>
<?php
session_start();
if(!isset($_SESSION['userName'])){ //if login in session is not set
header("Location: login.php");
exit();
}
?>

<?php

$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}

mysql_select_db("cad", $con)
or die('Could not select database');

$query="SELECT `town`, `location`, `incident_type`, `time_date`, `admin`, `id`
FROM `cad`
WHERE `id` =
$_GET[id]";

$result=mysql_query($query)
or die(mysql_error());

while( false!=($row=mysql_fetch_array($result)) )
{
echo '<h1>Town name: ', htmlspecialchars($row['town']), '</td>';
}




$town=$row['town'] ;
$location= $row['location'] ;
$incident_type=$row['incident_type'] ;


if(isset($_POST['save']))
{
$town = $_POST['town'];
$location = $_POST['location'];
$incident_type = $_POST['incident_type'];

mysql_query("UPDATE cad SET town ='{$town}', location ='{$location}',
incident_type ='{$incident_type}' WHERE `id` = $_GET[id]") or die(mysql_error());

echo "Saved! Redirecting back to the home page.";

}
mysql_close($con);

$id=$_GET['id'];
?>




<!DOCTYPE html>
<html>
<head>
<title>Edit Incident</title>
</head>

<body>
<?php
echo "<h1>You are editing incident number # $id</h1>";
?>
<form method="post">
<table>
<tr>
<td>Town</td>
<td><input type="text" name="town" value="<?php echo $town; ?>"/></td>
</tr>
<tr>
<td>Location</td>
<td><input type="text" name="location" value="<?php echo $location; ?>"/></td>
</tr>
<tr>
<td>Incident Type</td>
<td><input type="text" name="incident_type" value="<?php echo $incident_type; ?>"/></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="save" value="Save" /></td>
</tr>
</table>

</body>
</html>

最佳答案

您必须使用如下查询

mysql_query("UPDATE cad SET town ='{$town}', location ='{$location}',
incident_type ='{$incident_type}' WHERE `id` = {$_GET['id']}") or die(mysql_error()); ;

关于php - MySQL更新查询创建新表行而不是更新表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31199657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com