使用提交按钮通过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