Home > Code Snippets > Add data to a MySQL database
Add data to a MySQL database
Description
How to insert and save information into a MySQL database table using PHP.
The code
<?php
/**
* Change the first line to whatever
* you use to connect to the database.
*
* We're using two values, title and
* text. Replace these with whatever
* you want to add to the database.
*
* Finally, change tablename to the
* name of your table.
*/
// Your database connection code
db_connect();
$query = "INSERT INTO tablename(title, text) VALUES('$title','$text')";
$result = mysql_query($query);
echo "The data has been added to the database.";
?>
Get the code
Download the file to your computer: Click here to get the file