了解$ _REQUEST。为什么这没有获得$ _REQUEST ['submit']?

时间:2013-04-17 18:25:22

标签: php mysql insert

以下代码可以从DB中删除行,但无法插入。那是为什么?

<?php

    mysql_connect("localhost","formuser","password") or die(mysql_error());
    mysql_select_db("formpractice") or die(mysql_error());


    $name = $_POST['name'];
    $address = $_POST['address'];

    // Add into DB Query
    $insert = "INSERT INTO Users(name, address) VALUES ('$name', '$address')";
    $insert_result = mysql_query($insert);

    // Delete from DB Query
    $delete = "DELETE FROM Users WHERE name = '$name' OR address = '$address'";
    $delete_result = mysql_query($delete);

    if(isset($_REQUEST['submit'])) {
        if ($insert_result) {
            echo "Success";
        } else {
            echo "Failed to submit";
        }
        echo 'Submit Request'; 

    } else if (isset($_REQUEST['delete'])) {
        if ($delete_result) {
            echo "Deleted";
        } else {
            echo "Failed to submit";
        }
    }


?>

1 个答案:

答案 0 :(得分:1)

试试这个:

mysql_connect("localhost","formuser","password") or die(mysql_error());
mysql_select_db("formpractice") or die(mysql_error());

$name = $_POST['name'];
$address = $_POST['address'];

if(isset($_REQUEST['submit'])) {
     // Add into DB Query
    $insert = "INSERT INTO Users(name, address) VALUES ('$name', '$address')";
    $insert_result = mysql_query($insert);
    if ($insert_result) {
        echo "Success";
    } else {
        echo "Failed to submit";
    }
    echo 'Submit Request'; 

} else if (isset($_REQUEST['delete'])) {
    // Delete from DB Query
    $delete = "DELETE FROM Users WHERE name = '$name' OR address = '$address'";
    $delete_result = mysql_query($delete);
    if ($delete_result) {
        echo "Deleted";
    } else {
        echo "Failed to submit";
    }
}