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.

3
  • 2
    You are using an obsolete database API and should use a modern replacement. Commented Mar 14, 2013 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. Commented Mar 14, 2013 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". Commented Mar 14, 2013 at 12:27

4 Answers 4

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>
Sign up to request clarification or add additional context in comments.

Comments

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);
}
?>

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.