使用提交按鈕通過PHP將條目插入MySQL數據庫?

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


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.

3 个解决方案

#1


5  

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

只需將表單的操作設置為執行插入的腳本的URL即可。

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

請注意,由於您正在修改數據庫,因此請求可能是非冪等的,您應該使用POST方法。

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

#2


2  

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

希望這對你有所幫助

#3


2  

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

PHP

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/03/14/79078894a3e4b435507aadf155ed06db.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com