PHP - SQL INSERT statement in PHP

by
Jeremy Canfield |
Updated: March 11 2020
| PHP articles
Inserting data into MySQL using PHP requires two sets of code, an HTML form and PHP to process the data. Following is the minimal HTML needed.
<form action='insert.php' method='post'>
<input type='text' name='data1'>
<input type='submit'>
</form>
Notice the HTML form action is insert.php. We need to create a file named insert.php. Following is the minimal PHP needed to insert the data into MySQL. Notice also that the HTML form method='post'. When the method is post, the PHP must then use $_POST. The other option would be to use method='get' and then to use $_GET in the PHP. Notice also that the HTML input name='data1'. This needs to map to ['data1'] in the PHP.
<?php
$con = new mysqli('ip_or_domain','username','password','database');
$insert = "insert into tablename (columnname) values ('$_POST['data1']')";
$processing = $con->query($insert);
$con->close();
?>
Did you find this article helpful?
If so, consider buying me a coffee over at