Php MySQL更新查询生成新的表行,而不是更新表行

Php MySQL更新查询生成新的表行,而不是更新表行,php,mysql,mysqli,Php,Mysql,Mysqli,在尝试用PHP为表格创建编辑页面时,我在edit.PHP页面上遇到了一个问题,当我单击edit table row按钮时,它会将我带到正确的页面(edit.PHP),然后当我输入编辑的详细信息并提交时,它生成一个全新的表行条目,而不是更新我为其选择的要更新的表行条目 我已检查以确保id正确设置为数据库中正确的表行,并且它是正确的。我不知道它为什么这样做。任何帮助都将不胜感激 <?php require("manage_post.php"); ?> <?php session_

在尝试用PHP为表格创建编辑页面时,我在edit.PHP页面上遇到了一个问题,当我单击edit table row按钮时,它会将我带到正确的页面(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()); ;

添加新的隐藏密钥到post

<input type="hidden" name="savedid" value="<?php echo $_GET['id']?>" />

如果你考虑使用ItValid用于IDS

,你应该过滤你的VAR,你的$GET(ID)有一些价值吗?在运行查询之前,请尝试打印查询。当打印在页面上时,id是正确的。您可以执行echo“更新cad集town='{$town}',location='{$location}',incident_type='{$incident_type}'其中
id
={$\u GET['id']}”,并向我显示您正在执行的查询。
echo
您的查询并发布它too@Uchiha,你想让我回应哪个问题?这个问题<代码>mysql\u查询(“更新cad集town='{$town}',location='{$location}',incident\u type='{$incident\u type}',其中``id
=$\u GET[id]')或die(mysql\u error())我忘了提到,我的编辑页面上的输入框一直是空的(并没有预先填入已经提交的信息)。如下所示:。在while中放入$town和其他变量,并在第一个选择sql中将$\u GET[id]更改为$id,然后在文件开头添加$id=$\u GET['id'],并确保url请求不是大写的“id=20”
$town = $_POST['town'];
$location = $_POST['location'];
$incident_type = $_POST['incident_type'];
$savedid = $_POST['savedid'];
mysql_query("UPDATE cad SET town ='{$town}', location ='{$location}',
     incident_type ='{$incident_type}' WHERE `id` = $savedid") or die(mysql_error()); 

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