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