3

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.

Tobo
  • 31
  • 1
  • 1
  • 3
  • 2
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Mar 14 '13 at 11:22
  • This is for a Computing A-Level Coursework Project mate, I'm not too fussed that I'm not using the most modern thing possible. – Tobo Mar 14 '13 at 12:12
  • — There is a difference between "using the most modern thing possible" and "not using the thing that is marked 'this is £$%^!' and will be removed from PHP soon". – Quentin Mar 14 '13 at 12:27

4 Answers4

6

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>
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
4
<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);
}
?>
Devang Rathod
  • 6,650
  • 2
  • 23
  • 32
3

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

Hardik
  • 1,429
  • 2
  • 19
  • 37
0

I had for the submit details:

<form id = "submitForm" action="config/profile_save.php" method="post">

<button type="submit" class="button"  name="submit" value="submit">Save Profile</button></form>

Inside each input field on the page, I placed form = "submitForm" I then changed the name too.(This is the super global variable later)

<input type="text" autofocus="true" class="custom_link_url_text" id="custom_link_url_text" 
name="custom_link_email" placeholder="Enter your public email address" spellcheck="false" 
style="width: 245px;" maxlength="75" form = "submitForm">

I was then able to capture the data on the next page using the name as POST variable.

if(isset($_POST['submit'])) {
    $custom_link_email = $_POST['custom_link_email'];
}

Once I did that it was just a case of inserting data into the database.

Spinstaz
  • 287
  • 6
  • 12