任何人都可以告诉我我的问题在哪里,这个页面是从编辑按钮链接的,我已经嵌入到所有条目的表格中。但是,当我输入新值时,它将当前数据输入到字段中,而不是更新?任何帮助赞赏。从编辑按钮更新表条目
PHP:
<?php
include_once("config.php");
date_default_timezone_set('Europe/London');
//getting id from url
$id = $_GET['id'];
//selecting data associated with this particular id
$EditQuery = "SELECT * FROM ACW WHERE UserID=$id";
$results = sqlsrv_query ($conn, $EditQuery);
while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC))
{
$firstname = $row['FirstName'];
$lastname = $row['LastName'];
$location = $row['location'];
}
if(isset($_POST['update']))
{
$id = $_POST['UserID'];
$firstname = $_POST['FirstName'];
$lastname = $_POST['LastName'];
$location = $_POST['location'];
// checking empty fields
if(empty($firstname) || empty($lastname) || empty($location)) {
if(empty($firstname)) {
echo "<font color='red'>First Name field is empty.</font><br/>";
}
if(empty($lastname)) {
echo "<font color='red'>Last Name field is empty.</font><br/>";
}
if(empty($location)) {
echo "<font color='red'>Location field is empty.</font><br/>";
}
//link to the previous page
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
}
if (preg_match("/^[0-9-]+$/",$firstname || $lastname || $location)) {
echo "<font color='red'>Numbers are not valid in these fields</font><br/>";
}
} else {
//updating the table
$UpdateQuery ="UPDATE ACW SET FirstName='$firstname', LastName='$lastname', location='$location' WHERE UserID='$id'";
echo $UpdateQuery;
//echo "<font color='green'>Data has been updated.</font><br/>";
$results = sqlsrv_query ($conn, $UpdateQuery);
}
?>
形式:
<form name="form1" method="get" action="edit.php">
<table border="0">
<tr>
<td>First Name</td>
<td><input type="text" name="firstname" value="<?php echo $firstname;?>"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="lastname" value="<?php echo $lastname;?>"></td>
</tr>
<tr>
<td>Location</td>
<td><input type="text" name="location" value="<?php echo $location;?>"></td>
</tr>
<tr>
<td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>></td>
<td><input type="submit" name="update" value="Update"></td>
</tr>
</table>
</form>
不知道这是你的问题,但你有'$ _POST ['update']',但你的表单是'method ='get“' –
感谢您的帮助! – rizzkiks