gpt4 book ai didi

php - 添加/删除/更新数据库元素不起作用 (Dreamweaver + phpMyAdmin)

转载 作者:行者123 更新时间:2023-11-29 12:57:51 26 4
gpt4 key购买 nike

我正在尝试为网络编程项目创建一个非常简单的控制面板,我的控制面板中的“显示”功能运行得很好,并显示了我的删除、添加和更新功能根本不起作用的所有元素。

这是我想要对每个函数执行的操作:添加功能 -> 我想从输入元素添加一个元素到我的数据库删除功能->我要删除用户在控制面板中输入其ID的元素更新 -> 使用此功能我想更改所选产品的产品标题。

这三个功能不起作用,当我在网页中输入数据时,我发现表格中没有添加/更改任何内容。

代码如下:

add.html

<title>Add</title>
</head>

<body>
<form action="add.php" method="post">
<font size="+2" color="#CC0033">ID</font><input type="text" name="ID" />
<font size="+2" color="#CC0033">Product Title</font><input type="text" name="ProductTitle" />
<font size="+2" color="#CC0033">Price</font><input type="text" name="Price" />
<font size="+2" color="#CC0033">Quantity</font><input type="text" name="Quantity" />
<input type="submit" value="Insert" />
</form>
</body>
</html>

add.php

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Done</title>
</head>

<body>
<?
$ID=$HTTP_POST_VARS["ID"];
$ProductTitle=$HTTP_POST_VARS["ProductTitle"];
$Price=$HTTP_POST_VARS["Price"];
$Quantity=$HTTP_POST_VARS["Quantity"];

$db=mysql_connect("localhost","root","");
if($db==false)
{
print "Error";
exit;
}
mysql_select_db("Computer");
$query=("insert into Products values('".$ID." ',' ".$ProductTitle."',' ".$Price."',' ".$Quantity."')");
mysql_query($query);


?>
</body>
</html>

删除.html

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
Enter the ID of the product that you wish to delete:
<form action="delete.php" method="post">
<input type="text" name="UserInput">
<br>
<input type="submit" value="Delete">
</form>
</body>
</html>

删除.php

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?
$UserInput=$HTTP_POST_VARS["UserInput"];

$db=mysql_pconnect("localhost","root","");

if(!db)
{
print "Error";
exit;
}

mysql_select_db("Computer");
$query=("delete from Products where ID=".'$UserInput');
mysql_query($query);


?>

</body>
</html>

update.html

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="update.php" method="post">
Product ID<input type="text" name="ProductID">
Product Name<input type="text" name="ProductTitle">
New Product Name<input type="text" name="NewProductTitle">
<br>
<input type="submit" value="Update">
</form>

</body>
</html>

更新.php

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?
$ProductID=$HTTP_POST_VARS["ProductID"];
$ProductTitle=$HTTP_POST_VARS["ProductTitle"];
$NewProductTitle=$HTTP_POST_VARS["NewProductTitle"];
$db=mysql_pconnect("localhost","root","");

if(!$db)
{
echo "Error";
exit;
}

mysql_select_db("Computer");

$query=("update Products set ProductTitle='".$NewProductTitle. "' where ID=$ProductID");
mysql_query($query);


?>
</body>
</html>

最佳答案

首先,感谢您尝试提出解决方案,但是,正如其他评论者所说,您的脚本中确实存在问题。首先,我会考虑替换 $HTTP_POST_VARS["ID"];使用 $_POST 全局变量,并运行 if(isset()) 以确保在用户提交表单时设置所有字段。

其次,您应该考虑使用 MySQLi 或 PDO 来处理数据库连接,因为它们还可以提供验证和过滤以防止注入(inject)攻击,正如 Marc B 提到的那样。如果您是一个绝对的初学者,这是一个很好的尝试,我觉得如果您坚持下去,您可以取得进一步的进步,并且这个社区非常适合获得答案并了解您哪里出了问题。希望这有帮助,祝你好运! :)

关于php - 添加/删除/更新数据库元素不起作用 (Dreamweaver + phpMyAdmin),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23761866/

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