我有一个像这样的html表单:
<form method="post" action="input.php">
<label for="name">NAME</label>
<input type="text" name="name" size="20">
<label for="address">ADDIE</label>
<input type="text" name="address" size="40">
<input type="submit" name="submit" value="Send">
我有:
<?php
mysql_connect("localhost","formuser","password") or die(mysql_error());
mysql_select_db("formpractice") or die(mysql_error());
$name = $_POST['name'];
$address = $_POST['address'];
$query = "INSERT INTO Users(name, address) VALUES ('$name', '$address')";
$qresult = mysql_query($query);
if ($qresult) {
echo "Success";
} else {
echo "Failed to submit";
}
?>
我正在尝试这个:
<form method="post" action="input.php">
<label for="name">NAME</label>
<input type="text" name="name" size="20">
<label for="address">ADDIE</label>
<input type="text" name="address" size="40">
<input type="submit" name="submit" value="Send">
// Here is where I get trouble. How do I get this button to execute a delete statement?
<input type="submit" name="Delete" value="Delete">
答案 0 :(得分:2)
<form action="something.php" method=post">
Name: <input type=text name="name">
Price: <input type=text name="othername">
<input type=submit value="remove this from the database on the web server">
</form>
添加此php:
<?=
$name=$_POST['name'];
$price=$_POST['price'];
$sql = "Delete * from (whatever) VALUES ('$name','$othername')";
$result = mysql_query($sql);
?>
答案 1 :(得分:1)
<form method="post" action="input.php">
<label for="name">NAME</label>
<input type="text" name="name" size="20">
<label for="address">ADDIE</label>
<input type="text" name="address" size="40">
<input type="submit" name="submit" value="Send">
// Here is where I get trouble. How do I get this button to execute a delete statement?
<input type="submit" name="Delete" value="Delete">
无论你在“名称”中输入什么内容,都会在用于表单的方法中发送(在本例中为POST);你可以在全局变量$ _POST中找到它。为了使该按钮“删除”,您需要在某处(在这种情况下,它将在您的forgm标记的“action”属性中的metnioned的.php文件中),php代码执行使用您在表单中传递的信息(名称和地址)删除。
如果您要保留相同的表单,这意味着您将使用两个提交按钮,那么您的PHP代码必须以某种方式确定您希望它执行的操作(输入或删除),这是哪里您将使用输入标记的name属性。
<?php
if($_POST['Delete']{
//connect to database, issue query, close database
}
if($_POST['submit']{
//the code you had for sending the data
}
?>
我强烈推荐使用Head First PHP和MySQL进行学习!