使用提交按钮通过 PHP 将条目插入 MySQL 数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15407802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:10:01  来源:igfitidea点击:

Using a Submit Button to insert an entry into a MySQL database via PHP?

phphtmlmysqlsubmit-button

提问by Tobo

I'm pretty new to PHP, so I'm not quite sure on what to do with this.

我对 PHP 很陌生,所以我不太确定该怎么做。

Basically I'm trying to insert an entry into my MySQL database, through a "submit" button in HTML. I can't seem to get this to work, is it possible?

基本上,我试图通过 HTML 中的“提交”按钮将条目插入到我的 MySQL 数据库中。我似乎无法让它工作,这可能吗?

    <?php
    include('db_connect.php');
    $SQL = "INSERT INTO chosenitems (ID, Name, Price) VALUES ('', '4-6 Days', '£75.00')";

    $result = mysql_query($SQL);
    ?>

The INSERT works perfectly fine on its own, but I want it to be executed when the "submit" button is pressed.

INSERT 本身工作得很好,但我希望在按下“提交”按钮时执行它。

Any help would be greatly appreciated.

任何帮助将不胜感激。

Thanks

谢谢

Tobo.

托博。

回答by Quentin

Just set the actionof the form to the URL of the script that performs the insert.

只需将action表单的设置为执行插入的脚本的 URL。

Note that since you are modifying a database, the request is probably non-idempotentand you should use the POST method.

请注意,由于您正在修改数据库,因此请求可能是非幂等的,您应该使用 POST 方法。

<form action="/path/to/your/script.php" method="post">
    <input type="submit">
</form>

回答by Devang Rathod

<form method="post">
    <input type="submit" name="submit" value="submt"/>
</form>

PHP

PHP

<?php
if(isset($_POST['submit']))
{
     $SQL = "INSERT INTO chosenitems (ID, Name, Price) VALUES ('', '4-6 Days', '£75.00')";
     $result = mysql_query($SQL);
}
?>

回答by Hardik

You can check button value is posted and can execute line of code in it.

您可以检查按钮值是否已发布并可以执行其中的代码行。

<?php
    include('db_connect.php');
    if(isset($_REQUEST['SUBMIT_BUTTON_NAME']))
    {
        $SQL = "INSERT INTO chosenitems (ID, Name, Price) VALUES ('', '4-6 Days', '£75.00')";

        $result = mysql_query($SQL);
    }
?>

Hope this will be helpful to you

希望这对你有帮助