-6

我有一个像这样的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">
4

2 回答 2

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); 
?>
于 2013-04-15T22:33:32.703 回答
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”属性中的 .php 文件中)具有执行的 php 代码使用您通过表格传递的信息(姓名和地址)进行删除。

如果您要保持相同的表单,这意味着您将使用两个提交按钮,那么您的 php 代码必须以某种方式确定您希望它执行什么操作(输入或删除),这就是您将使用的地方输入标签的名称属性。

<?php
     if($_POST['Delete']{ 
         //connect to database, issue query, close database
     }

     if($_POST['submit']{
           //the code you had for sending the data
     }
?>

I strongly recommend the book Head First PHP and MySQL for learning!

于 2013-04-15T23:27:47.007 回答