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.
asked Mar 14, 2013 at 11:21
3
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>answered Mar 14, 2013 at 11:23
QuentinQuentin
876k121 gold badges1173 silver badges1288 bronze badges
0
<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); } ?>answered Mar 14, 2013 at 11:23
Devang RathodDevang Rathod
6,4842 gold badges22 silver badges31 bronze badges
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
answered Mar 14, 2013 at 11:22
HardikHardik
1,4192 gold badges18 silver badges36 bronze badges
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.
answered Nov 19, 2021 at 10:51
SpinstazSpinstaz
1504 silver badges10 bronze badges
Hello @Sign,
It is simple to create contact form to send email or any other information and storing in mysql database.
Using INSERT INTO statement you can insert new data into a database table.
See the example below:
Output:
Step 1: Connection with Database
This is dbConn.php file which is used to connect the database.
This dbConn.php file will use everywhere, where you want to perform a task with the database.
Here it is connected with the local database and check database connected or not.
dbConn.php
<?php $db = mysqli_connect("localhost","root","","testDB"); if(!$db) { die("Connection failed: " . mysqli_connect_error()); } ?>Step 2: Creating HTML form and inserting data
Here's a simple HTML form which is index.php file. It has two text input and a submit button.
The INSERT INTO statement is used to add new data into the database. This insert query will execute passing through to the PHP mysqli_query() function to insert new data.
And also created a simple HTML form which has two input text and a submit button which is used to take data from users.
In the index.php file the dbConn.php file includes for connecting with the database and insert new data. The form has two field fullname and age which are get data from users and store into the database.
In this example the PHP code and the HTML form coded in the single page. The PHP script embedded with the HTML page.
index.php
<!DOCTYPE html> <html> <head> <title>Add Records in Database</title> </head> <body> <?php include "dbConn.php"; // Using database connection file here if(isset($_POST['submit'])) { $fullname = $_POST['fullname']; $age = $_POST['age']; $insert = mysqli_query($db,"INSERT INTO `tblemp`(`fullname`, `age`) VALUES ('$fullname','$age')"); if(!$insert) { echo mysqli_error(); } else { echo "Records added successfully."; } } mysqli_close($db); // Close connection ?> <h3>Fill the Form</h3> <form method="POST"> Full Name : <input type="text" name="fullname" placeholder="Enter Full Name" Required> <br/> Age : <input type="text" name="age" placeholder="Enter Age" Required> <br/> <input type="submit" name="submit" value="Submit"> </form> </body> </html>Write PHP code as a separate file
Step 1: Creating HTML Form
First of all creating HTML form which is index.php file. This form has two text boxes and a submit button for inserting data.
This form also has an action attribute which contains insert.php. This will perform when clicking on submit button.
index.php
<!DOCTYPE html> <html> <head> <title>Add Records in Database</title> </head> <body> <form action="insert.php" method="POST"> Full Name : <input type="text" name="fullname" placeholder="Enter Full Name" Required> <br/> Age : <input type="text" name="age" placeholder="Enter Age" Required> <br/> <input type="submit" name="submit" value="Submit"> </form> </body> </html>Step 2: Connection with Database
This is dbConn.php file which connects the database.
dbConn.php
<?php $db = mysqli_connect("localhost","root","","testDB"); if(!$db) { die("Connection failed: " . mysqli_connect_error()); } ?>Step 3: Write PHP code for insert data
This is insert.php file. Here is written PHP code for inserting data when clicking on submit button.
insert.php
<?php include "dbConn.php"; // Using database connection file here if(isset($_POST['submit'])) { $fullname = $_POST['fullname']; $age = $_POST['age']; $insert = mysqli_query($db,"INSERT INTO `tblemp`(`fullname`, `age`) VALUES ('$fullname','$age')"); if(!$insert) { echo mysqli_error(); } else { echo "Records added successfully."; } } mysqli_close($db); // Close connection ?>Hope it helps!!
Thank you!