Home arrow MySQL arrow Page 2 - The Three Most Important MySQL Queries

The INSERT MySQL Query Command - MySQL

Doing PHP queries to a MySQL database is one of the most important processes in any web application. This is where data is being fetched from or inserted into the database. If you are a beginning PHP web developer, then learning the most important MySQL queries is essential to your success in dealing with dynamic websites.

TABLE OF CONTENTS:
  1. The Three Most Important MySQL Queries
  2. The INSERT MySQL Query Command
  3. The SELECT MySQL Query Command
  4. The UPDATE MySQL Query Command
By: Codex-M
Rating: starstarstarstarstar / 27
July 06, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Okay, before we can grab data from the database, we must know how to insert data. Assuming you have already created a database with a table name, then inserting data can be done with the INSERT SQL command. I've given an example below. Assuming that you are inserting three pieces of data simultaneously into three different fields in the MySQL database, it will look something like this:

INSERT INTO `tablename` (`fieldname1`,`fieldname2`,``,`fieldname3`) VALUES('$datatobeinsertedtofieldname1','$datatobeinsertedtofieldname2',
'$datatobeinsertedtofieldname3')

When using this query with PHP, be careful about the exact use of quote symbols and exact spelling of fieldnames and variables. This is the common cause of errors in PHP programming that involve the use of the INSERT query.

Note that the table name and field names in the query command should be used with back ticks (`) , and that you need to maintain consistent cases between the actual MySQL tablenames and the ones used with your script. This will avoid case sensitivity issues, which could be common in Linux/PHP and MySQL environment.

For example: if your table name is SongArchives, use also:

INSERT INTO `SongArchives` .....

Finally, to formulate a complete MySQL query in PHP using the INSERT command, the following is the recommended script:

<?php

//Step 1 Connect to database

$username = "Your MySQL username here";

$password = "Your MySQL password";

$hostname = "Hostname";

$table = "MySQL Table name where the data will be inserted";

$database = "The name of the MySQL database which holds the table";

$dbhandle = mysql_connect($hostname, $username, $password)

or die("Unable to connect to MySQL");

$selected = mysql_select_db($database,$dbhandle)

or die("Could not select $database");

//Step 2. Insert other PHP scripts here (such as grabbing data from HTML forms, etc)

//Step 3. Sanitize variables before inserting to database. This will prevent MySQL injection.

$datatobeinsertedtofieldname1 = mysql_real_escape_string(stripslashes($datatobeinsertedtofieldname1));

$datatobeinsertedtofieldname2 = mysql_real_escape_string(stripslashes($datatobeinsertedtofieldname2));

$datatobeinsertedtofieldname3 = mysql_real_escape_string(stripslashes($datatobeinsertedtofieldname3));

mysql_query("INSERT INTO `tablename` (`fieldname1`,`fieldname2`,``,`fieldname3`) VALUES('$datatobeinsertedtofieldname1','$datatobeinsertedtofieldname2',
'$datatobeinsertedtofieldname3')")

or die(mysql_error());

?>



 
 
>>> More MySQL Articles          >>> More By Codex-M
 

blog comments powered by Disqus
   

MYSQL ARTICLES

- Xeround Releases Free Version of MySQL Cloud...
- Oracle Announces New MySQL Specialization
- Constant Contact Chooses SkySQL for MySQL Su...
- Revoke Statement in MySQL
- The Grant Statement in MySQL
- SuccessBricks Announces ClearDB Availability...
- Building a PHP ORM: Deploying a Blog
- TROSYS Launches Free MySQL Manager and Admin...
- Building an ORM in PHP: Domain Modeling
- Building an ORM in PHP
- MySQL Leads Open Source Market, Gets Cluster...
- Oracle Announces Milestone Release for MySQL
- How to Stop SQL Injection Attacks
- New Defragmentation Solution for SQL Server
- Comparison of MyISAM and InnoDB MySQL Databa...


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap

Dev Shed Tutorial Topics: