How to insert data into MySQL using PHP

Home > Search > How-to
  by

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

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments