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

Tobo picture Tobo · Mar 14, 2013 · Viewed 88.1k times · Source

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

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?

    <?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.

Any help would be greatly appreciated.

Thanks

Tobo.

Answer

Quentin picture Quentin · Mar 14, 2013

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

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

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