MySQL PHP Insert Tutorial

  • Share
  • Print this article

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).

  1. The Basics

    • 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 Script

    • The following script will allow you to take a first name and last name from a form and place the values into the database:

      <?php
      $fname=$_POST['firstname'];
      $lname=$_POST['lastname'];
      $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')");
      mysql_close($connection);
      ?>

      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" />
      </form>

      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.

Related Searches

References

Resources

Comments

You May Also Like

Related Ads

Featured
View Mobile Site