[英]MySQL Table Update with PHP HTML Form isn't Working
我正在尝试使用PHP表单更新mySQL表。 当前,我已经设置了所有代码,但是当更新表数据age时,它将表中的所有年龄设置为“ 0”。 我不知道为什么,但是任何指导都会受到高度赞赏。 谢谢。
凯尔西
<?php
$hostname = "---------";//host name
$dbname = "-------";//database name
$username = "-------------";//username you use to login to php my admin
$password = "--------";//password you use to login
//CONNECTION OBJECT
//This Keeps the Connection to the Databade
$conn = new MySQLi($hostname, $username, $password, $dbname) or die('Can not connect to database')
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$id=$_GET['FirstName'];
//Create a query
$sql = "SELECT * FROM Persons";
//submit the query and capture the result
$result = $conn->query($sql) or die(mysql_error());
$query=getenv(QUERY_STRING);
parse_str($query);
?>
<h2>Update Record <?php echo $sql;?></h2>
<form action="" method="post">
<?php
while ($row = $result->fetch_assoc()) {?>
<table border="0" cellspacing="10">
<tr>
<td>age:</td> <td><input type="text" name="Age" value="<?php echo $row['Age']; ?>"></td>
</tr>
<tr>
<td><INPUT TYPE="Submit" VALUE="Update the Record" NAME="Submit"></td>
</tr>
</table>
<?php }
?>
</form>
<?php
if(isset($_POST['Submit'])){//if the submit button is clicked
$sql="UPDATE Persons SET Age='".$_POST['Age']."'";
$conn->query($sql) or die("Cannot update");//update or error
}
?>
</body>
</html>
现在,按照其书面说明, UPDATE
查询将更新整个Persons
表,而不是单个记录。
UPDATE Persons SET Age=15 WHERE id = 5
将仅更新一条记录,该记录与整个表的值相对应。
security risk) to put raw post values directly into an SQL string. 另外,将原始发布值直接放入SQL字符串也不是一件好事(又有安全风险)。 在将值放入数据库查询之前,应始终保持头脑清醒。
在语句本身内部添加POST的好方法。
尝试这个:
<?php
if(isset($_POST['Submit'])){//if the submit button is clicked
$age = $_POST['Age'];
$sql="UPDATE Persons SET Age='".$age."' WHERE firstname ='".$id."'";
$conn->query($sql) or die("Cannot update");//update or error
}
?>
您的页面令人困惑。 您甚至没有正确输出结果。 尝试这个:
<?php
$hostname = "---------";//host name
$dbname = "-------";//database name
$username = "-------------";//username you use to login to php my admin
$password = "--------";//password you use to login
//CONNECTION OBJECT
//This Keeps the Connection to the Databade
$conn = new MySQLi($hostname, $username, $password, $dbname) or die('Can not connect to database')
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
//$id=$_GET['FirstName'];
//Create a query
$sql = "SELECT * FROM Persons";
//submit the query and capture the result
$result = $conn->query($sql) or die(mysql_error());
$query=getenv(QUERY_STRING);
parse_str($query);
?>
<h2>Update Record <?php echo $sql;?></h2>
<form action="" method="post" enctype='multipart/form-data'>
<?php
while ($row = $result->fetch_assoc()) {?>
<table border="0" cellspacing="10">
<tr>
<td>Age:</td>
<td><?php echo $row['Age'];?></td>
<td><?php echo $row['FirstName'];?></td>
<td><input type="text" name="Age"></td>
<td><input type=hidden" name="firstName" value="<?php echo $row['FirstName'];?>"></td>
</tr>
<tr>
<td><INPUT TYPE="Submit" VALUE="Update the Record" NAME="Submit"></td>
</tr>
</table>
<?php }
?>
</form>
<?php
if(isset($_POST['Submit'])){//if the submit button is clicked
$age = $_POST['Age'];
$id = $_POST['firstName'];
$sql="UPDATE Persons SET Age='".$age."' WHERE firstname ='".$id."'";
$conn->query($sql) or die("Cannot update");//update or error
}
?>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.