logo
g Text Version
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Sports
Travel & Culture
TV & Movies

dailyclick
Bored? Games!
Nutrition
Postcards
Take a Quiz
Rate My Photo

new
European Travel
Action Movies
Bible Basics
Houseplants
Romance Movies
Creativity
Family Travel


dailyclick
All times in EST

Full Schedule
g
g PHP Site

BellaOnline's PHP Editor

g

Personal Activities Calendar Program - INSERT SQL Statement and mysql_query( ) Function

Guest Author - Diane Cipollo

At the end of the previous tutorial, we had written the part of the ActivityEntry.php3 script that setup the connection to the MySQLR server in preparation to store the information about the new activity into the calendar database. Now we will write the INSERT sql statement and mysql_query( ) function that will create the new record in the activities table. You may want to refresh your memory for the rules for using sql queries.

Here is the code we will be adding to the ActivityEntry.php3 script. We will place it at the end of the script after the mysql_select_db function.

The sql INSERT statement

The first thing that we will do is set the sql INSERT statement to a variable which we will name $sql_send. However, you can use any name for this variable. It is not necessary to do this step, but it does make the code much easier to understand. The code for the sql INSERT statement itself is placed between quotation marks and placed after the equal size. The statement ends with a semicolon.

$sql_send = "sql statement goes here";

$sql_send = "INSERT INTO activities
(act_ID, act_title, act_desc, act_timestamp_1, act_timestamp_2, act_status)
values
(' ', '$pass_act_title', '$pass_act_desc', '$act_start_time', '$act_end_time', '$pass_act_status')";
Note--Due to space limitations, this code is wrapped but can really be all on one line.

$sql_send = "INSERT INTO name_of_table (list of field names) values (list of values)";
As you can see, the query name for this sql statement is INSERT and this tells the server to create a new record in the activities table and populate that record with the list of values that follow in the statement.

(list of field names)
The list of field names is optional. However, it is much easier to maintain the code later if you list the table fields in the code. In our calendar program, the activities table has six fields.

act_ID
act_title
act_desc
act_timestamp_1
act_timestamp_2
act_status

When we list these fields, we will separate each with a comma and place them all inside parenthesis. Please notice that the fields are listed in the same order that they appear in the activities table. We will discuss why below.

(act_ID, act_title, act_desc, act_timestamp_1, act_timestamp_2, act_status)

(list of values)
The list of values is not optional. Because we have already set these values to variables previously in the check_data process and the strtotime( ) timestamp process, we will place a list of these variables inside the parenthesis instead of the actual data. We will enclose each variable inside single quotation marks. As with the list of fields, separate each variable with a comma and place them all inside parenthesis. As you might guess, the order in which you list these values is important and must match the order in which you have listed the fields so that the server can match the correct value with each field. (NOTE: If you are not using the optional list of fields, the server will automatically place the values into the table in the order in which you have them listed. Therefore, you must be sure to list the values in the same order that the corresponding fields actually occur in the activities table.)

In our working example, we have one field in the table that will AUTO_INCREMENT. This is the act_ID field which is the first field in the table. Because of this, we do not need to supply an ID number for the new record. It will be automatically created for us. Therefore, we need to put a blank place holder in the list of values for this. As you can see below, we have set this first value to null by using two single quotation marks together.

(' ', '$pass_act_title', '$pass_act_desc', '$act_start_time', '$act_end_time', '$pass_act_status')

Next →



The MySQL Database and Rules for Using SQL Queries
Personal Activities Calendar Program Use the strtotime and mysql_select_db Functions
How to Connect to the MySQL Server with the PHP mysql_connect Function



This site needs an editor - click to learn more!

Add Personal+Activities+Calendar+Program+%2D+INSERT+SQL+Statement+and+mysql%5Fquery%28+%29+Function to Twitter Add Personal+Activities+Calendar+Program+%2D+INSERT+SQL+Statement+and+mysql%5Fquery%28+%29+Function to Facebook Add Personal+Activities+Calendar+Program+%2D+INSERT+SQL+Statement+and+mysql%5Fquery%28+%29+Function to MySpace Add Personal+Activities+Calendar+Program+%2D+INSERT+SQL+Statement+and+mysql%5Fquery%28+%29+Function to Del.icio.us Digg Personal+Activities+Calendar+Program+%2D+INSERT+SQL+Statement+and+mysql%5Fquery%28+%29+Function Add Personal+Activities+Calendar+Program+%2D+INSERT+SQL+Statement+and+mysql%5Fquery%28+%29+Function to Yahoo My Web Add Personal+Activities+Calendar+Program+%2D+INSERT+SQL+Statement+and+mysql%5Fquery%28+%29+Function to Google Bookmarks Add Personal+Activities+Calendar+Program+%2D+INSERT+SQL+Statement+and+mysql%5Fquery%28+%29+Function to Stumbleupon Add Personal+Activities+Calendar+Program+%2D+INSERT+SQL+Statement+and+mysql%5Fquery%28+%29+Function to Reddit




RSS | Related Articles | Editor's Picks Articles | Top Ten Articles | Previous Features | Site Map


For FREE email updates, subscribe to the PHP Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor


Content copyright © 2014 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.

g


g features
Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Poetry
Daily
Weekly
Monthly
Less than Monthly



BellaOnline on Facebook
g


| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2014 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor