添加/删除/更新数据库元素不工作(Dreamweaver+;phpMyAdmin)
我正在尝试为一个web编程项目创建一个非常简单的控制面板,我的控制面板中的“Show”功能工作得非常好,通过我的Delete、Add和Update功能显示所有元素,但这些功能根本不起作用 以下是我想对每个函数执行的操作: 添加函数->我想从输入元素向我的数据库添加一个元素 删除函数->我想删除用户在控制面板中输入其ID的元素 更新->使用此功能,我想更改所选产品的产品标题 这3个功能不起作用,当我在网页中输入数据时,我看到表中没有添加/更改任何内容 代码如下: add.html添加/删除/更新数据库元素不工作(Dreamweaver+;phpMyAdmin),php,mysql,phpmyadmin,Php,Mysql,Phpmyadmin,我正在尝试为一个web编程项目创建一个非常简单的控制面板,我的控制面板中的“Show”功能工作得非常好,通过我的Delete、Add和Update功能显示所有元素,但这些功能根本不起作用 以下是我想对每个函数执行的操作: 添加函数->我想从输入元素向我的数据库添加一个元素 删除函数->我想删除用户在控制面板中输入其ID的元素 更新->使用此功能,我想更改所选产品的产品标题 这3个功能不起作用,当我在网页中输入数据时,我看到表中没有添加/更改任何内容 代码如下: add.html <titl
<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>
<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>
<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>
添加
身份证件
产品名称
价格
量
add.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>
<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>
多恩
update.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>
<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>
<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>
无标题文件
产品ID
品名
新产品名称
update.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>
<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>
无标题文件
首先,很高兴你能想出一个解决方案,但是,正如其他评论员所说,你的脚本中确实存在问题。首先,我将考虑替换$HTPpPixIVARAR[[ ID ] ];使用$\u POST全局变量,并运行if(isset())以确保在用户提交表单时设置了所有字段
第二,您应该考虑使用MySQL或PDO来处理数据库连接,因为它们还可以提供验证和过滤,以防止Marc B提到的注入攻击。如果你是一个绝对的初学者,那么这是一个很好的尝试。我觉得如果你坚持下去,你可以取得更大的进步。这个社区非常有助于找到答案,了解你哪里出了错。希望这会有帮助,祝你好运!:)
$HTTP\u POST\u VARS[“ID”]
??你什么时候写的这段代码0您的代码有许多问题。您使用的是过时/不推荐的函数(mysql)和superglobals(http_post_vars),您很容易受到以下攻击:您的DB代码完全没有错误处理,只是假设成功;cargo cult编程的多个示例;错误的字符串用法(“$UserInput”
?)等等。。。这段代码是一场即将发生的灾难。