gpt4 book ai didi

php - 使用 PHP HTML 表单的 MySQL 表更新不起作用

转载 作者:行者123 更新时间:2023-11-29 00:19:39 25 4
gpt4 key购买 nike

我正在尝试使用带有 PHP 的表单来更新 mySQL 表。目前我已经设置了所有代码,但是当我更新我的表数据年龄时,它会将表中的所有年龄设置为“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 将只更新与整个表的值相对应的一条记录。

此外,将原始发布值直接放入 SQL 字符串中也不好(又名 大量 安全风险)。在将它们放入数据库查询之前,您应该始终全面检查您的值。

关于php - 使用 PHP HTML 表单的 MySQL 表更新不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21557978/

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