MySQL and PHP are part of the LAMP (Linux, Apache, MySQL, PHP) software stack. Combining PHP and MySQL allows you to create dynamic web pages and store user data such as login information or reporting purposes. The PHP MySQL functions use the same SQL query format as is found within the MySQL database. If you are not familiar with SQL, the format can be found in the MySQL manual (see Resources).
In order to access MySQL databases from within PHP, you must have the MySQL client libraries installed and PHP must be compiled with the "--with-mysql" directive. Most web hosting services already have these libraries enabled. If you are using a Linux server, you can use your distribution's package manager to install the software, and it will set it up correctly for you.
There are quite a few PHP functions for MySQL access which are listed in the PHP: MySQL manual (see Resources). These functions allow you perform administrative functions and queries within the MySQL database.
The following script will allow you to take a first name and last name from a form and place the values into the database:
$database = "mydatabase";
$connection = mysql_connect("localhost");
if (! $connection)
die ("Couldn't connect to MySQL");
mysql_select_db($database, $connection) or die ("Couldn't connect to the database: ".mysql_error());
mysql_query ("INSERT INTO name (fname, lname) VALUES ('$fname', '$lname')");
The first two lines access use the $_POST function to get the first name and last name values and place them into variable. The $database variable holds the name of the MySQL database.
The "mysql_connect" function opens a connection to the MySQL database. The connection is placed into a variable, so that it can be closed later in the script. If the connection cannot be made, the script stops (dies) and returns "Couldn't connect to MySQL."
The "mysql_select_db" function opens the correct database. Once again, if the database cannot be accessed for some reason, the script stops and returns "Couldn't connect to the database." The "mysql_error" function returns the reason for the failure to connect.
The "mysql_query" function allows you to send an SQL query to the database. The actual query is contained within the parentheses. This script sends an insert query and uses the variables obtained from the form.
Finally, the query is closed with the "mysql_close" function. The function uses the variable created by the "mysql_connect" function.
Embedding into Web Page
The script can be saved as "insert.php" and placed in the same directory as your web page, or in the PHP includes directory defined by the PHP configuration for your server.
Create a form in your web page with the following format:
<form action ="insert.php" method="post">
First Name: <input name="firstname" type="text" /><br />
Last Name: <input name="lastname" type="text" /><br />
<input type = "submit" />
When the user presses the "submit" button, they script will be activated and the database will be updated with the new first name and last name.
How to Insert Data From a Form in PHP to a MySQL Database
Inserting data into MySQL from a form on a website using PHP is something that developers often do. The task is not...
How to Use PHP Code in a Java Script Function
How to Insert a Decimal in PHP MySQL
PHP provides many standard functions for handling MySQL databases. This means that inserting data into a MySQL database table using a PHP...
How to Insert a Checkbox in PHP MySQL
PHP can select records from a MySQL database, and then provide a way to choose selected records using an HTML check box....
How to Convert Text Before Inserting MySQL in PHP
It's common for a PHP application to get user input through an HTML form and to insert that input into a MySQL...
How to Insert an Image Into an HTML Email Using PHP
Modern websites often employ PHP in their server-side development. The language was designed for the web and integrates seamlessly with HTML. PHP...
How to Insert an Image Into an HTML Email in PHP
PHP is a powerful programming language most often used for Internet applications. Its popularity is due to its efficient method for integrating...
PHP Tutorial: MySQL
MySQL is an open source database. SQL, or Structured Query Language, is the language used to create, query and manipulate MySQL databases....
MySQL PHP Query Tutorial
The mysql_query function sends a unique query to the active database. The function syntax is: mysql_query($query[$link]) The query can be any valid...
How to Insert Into a MySQL Database
Inserting new rows into a MySQL table is accomplished with the INSERT command. For the most part, it will be necessary to...