School Map:
Home / Enroll
$ Student Records; Class
$ News @ LIVE
Tech Radio * Support/FAQ
| Store | FORUMS
Class / Forums / mysql101 ![]() |
Week #3 - Basic to-do assignment here |
(#23 2003-08-22 13:14:34) Post Reply |
|
TDavid
|
Your Week #3 Basic TO-DO assignment should be posted here in this thread only. If you have specific questions about the week 1 course text then please also post them in this thread so that other students can learn from the information and we can collectively build a knowledge base.
Here is the details on what you need to do in this assignment: Create forms for the Websites and Sponsors database tables, and the PHP script to prepare and INSERT the data into their respective tables in the database. Post all source code to this board. |
(#28 2003-09-01 21:16:05) Post Reply |
|
lizabit
|
This is my code for the basic week 3. It is exactly like the one you posted in the text. I had to follow it exactly inorder to understand what i was doing.
********************************************* <?php // Using sessions: /*check if session was started by an error redirect: **SESSIONS - session variables retain **their value from page to page during session*/ if(isset($_GET["PHPSESSID"])){ // Assign it for current Session: $Sess_ID = $_GET["PHPSESSID"]; session_id("$Sess_ID");} // Start Session to show form inputs on error: session_start(); // Create a function to show errors: function show_error($field){ if($_SESSION[$field]){ echo '<br><font color="#FF0000" size="2" face="Arial"><br> <b>ERROR: Blank or only contains disallowed tag(s).</b></font>'; } } // Create a function to show previously selected status on error: function show_selected($field, $status){ if($status == $_SESSION[$field]){ echo 'selected'; }else if($status == 'Pending'){ echo 'selected'; } } /*Logic sequence: ********************************/ // If data was submitted from a form: if(isset($_POST['Submit_websites']) || isset($_POST['Submit_Sponsors'])){ // Note: magic quotes does addslashes() compare to mysql_escape_string() in text above // determine if magic quotes are on: if(get_magic_quotes_gpc()){ // we'll use mysql_escape_string() for database insert // go through $_POST and stripslashes: foreach($_POST as $key => $value){ // Note: Changing the values in the $_POST array: $_POST[$key] = stripslashes($value); } } // Store values in session variables // to re-write form if there is an error: foreach($_POST as $key => $value){ $_SESSION[$key] = $value; } // Remove tags and whitespaces: foreach($_POST as $key => $value){ // Note: Changing the values in the $_POST array: $_POST[$key] = strip_tags(trim($value)); } // Check for errors, // (see if required fields are empty) // since only one form can be submitted at a time, // check each individually: if(isset($_POST['Submit_websites'])){ if(empty($_POST['w_name'])){ // Specific error flag in session variable: $_SESSION['w_name_err'] = 1; // General error flag: $err = 1; } if(empty($_POST['w_url'])){ $_SESSION['w_url_err'] = 1; $err = 1; } } if(isset($_POST['Submit_Sponsors'])){ if(empty($_POST['s_name'])){ $_SESSION['s_name_err'] = 1; $err = 1; } if(empty($_POST['s_url'])){ $_SESSION['s_url_err'] = 1; $err = 1; } } /***Check general error flag: ******************************/ if($err){ // If there is an error then redirect back to form page to show errors: header("Location:".$_SERVER['PHP_SELF']."?".SID); } else {// no errors // Unset all of the session variables. $_SESSION = array(); // Destroy the session. session_destroy(); // Get data ready for insertion into database: foreach($_POST as $key => $value) { // Note: Changing the values in the $_POST array: $_POST[$key] = mysql_escape_string(htmlentities($value)); } /* Enter data intodatabase sequence *****************************************/ // connect to mysql $db = mysql_connect('localhost','lgrace','nicho6uz') or die('Could not connect to MySQL Server.'); //connect to database //NOTE: @ suppresses regular error reporting @mysql_select_db('MySponsors',$db) or die('Could not connect to database.'); //See if submit button named 'Submit_websites was posted if(isset($_POST['Submit_websites'])) { //Write Insert Query to a Variable: $query_insert_website = "INSERT INTO `websites` SET w_name = '".$_POST['w_name']."', w_url = '".$_POST['w_url']."', w_desc = '".$_POST['w_desc']."', w_status = '".$_POST['w_status']."' "; //Do websites Table Data Insert, check if Row added: if((@ mysql_query($query_insert_website, $db)) && @ mysql_affected_rows() == 1) { echo'<h3>Website Data inserted into websites Table.</h3>'; //success }else { echo'<h3>Could not add data to websites Table. </h3>'; } } //end isset websites //See if submit button named 'Submit_Sponsors was posted if(isset($_POST['Submit_Sponsors'])) { //Write Insert Query to a Variable: $query_insert_website = "INSERT INTO 'Sponsors' SET s_name = '".$_POST['s_name']."', s_url = '".$_POST['s_url']."', s_staturl = '".$_POST['s_staturl']."', s_email = '".$_POST['s_email']."', s_uname = '".$_POST['s_uname']."', s_pass = '".$_POST['s_pass']."', s_status = '".$_POST['s_status']."', s_date = NOW() "; //Do Sponsors Table Data Insert, check if Row added: if((@ mysql_query($query_insert_sponsor, $db)) && @ mysql_affected_rows() == 1) { echo'<h3>Sponsor Data inserted into Sponsors Table.</h3>'; //success }else { echo'<h3>Could not add data to Sponsors Table. </h3>'; } } //end isset websites }//end if($err) }//end if submit form data ?> <html> <head> <title>Script School - MySQL001 - Wk 3 -Script</title> </head> </body> <form name="websitesForm" method="post" action="<?php echo ''.$_SERVER['PHP_SELF'].''; ?> "> <table width="632" border="1" align="center" cellpadding="2" cellspacing="1" bordercolor="#0000FF" bgcolor="#99CCFF" rules="rows"> <caption> <strong>websites Form</strong> </caption> <tr> <td width="135" align="right">Website Name</td> <td width="5" align="right"> </td> <td width="480" align="left"> <input name="w_name" type="text" id="w_name" value="<?php echo $_SESSION['w_name']; ?>" size="58" maxlength="255"> <?php #show_error('w_name_err'); ?> </td> </tr> <tr> <td align="right">Website URL</td> <td align="right"> </td> <td align="left"> <input name="w_url" type="text" id="w_url" value="<?php echo $_SESSION['w_url']; ?>" size="58" maxlength="255"> <?php #show_error('w_url_err'); ?> </td> </tr> <tr> <td align="right">Description</td> <td align="right"> </td> <td align="left"> <textarea name="w_desc" cols="50" wrap="HARD" id="w_desc"> <?php echo $_SESSION['w_desc']; ?> </textarea> </td> </tr> <tr> <td align="right">Status</td> <td align="right"> </td> <td align="left"> <select name="w_status" id="w_status"> <!-- Note: PHP function to show selected value if error found --> <option value="Pending" <?php show_selected('w_status','Pending'); ?> >Pending</option> <option value="Active" <?php show_selected('w_status','Active'); ?> >Active</option> <option value="Inactive" <?php show_selected('w_status','Inactive'); ?> >Inactive</option> <option value="Defunct" <?php show_selected('w_status','Defunct'); ?> >Defunct</option> </select> </td> <tr> <td colspan="3" align="center"> <input name="Reset-websites" type="reset" id="Reset-websites" value="Clear Form"> <!-- Note: button name="Submit_Sponsors" used to check for submission --> <input type="submit" name="Submit_websites" value="Submit websites"> </td> </tr> </table> </form> <br> <p> <!--Now the text for the sponsors form ********************************************************************************--> <form name="SponsorsForm" method="post" action="<?php echo ''.$_SERVER['PHP_SELF'].''; ?>"> <table width="632" border="1" align="center" cellpadding="2" cellspacing="1" bordercolor="#0000FF" bgcolor="#99CCFF" rules="rows"> <caption> <strong>Sponsors Form</strong> </caption> <tr> <td width="135" align="right">Sponsor Name</td> <td width="5" align="right"> </td> <td width="480" align="left"> <!-- Note: Value is PHP Session Variable, possibly blank --> <input name="s_name" type="text" id="s_name" value="<?php echo $_SESSION['s_name']; ?>" size="58" maxlength="255"> <!-- Note: Use PHP function to report error, if found --> <?php show_error('s_name_err'); ?> </td> </tr> <tr> <td align="right">Sponsor URL</td> <td align="right"> </td> <td align="left"> <input name="s_url" type="text" id="s_url" value="<?php echo $SESSION['s_url']; ?>" SIZE="58" MAXLENGTH="255"> <?php show_error('s_url_err'); ?></TD> </tr> <tr> <td align="right">Statistics URL</td> <td align="right"> </td> <td align="left"> <input name="s_staturl" type="text" id="s_staturl" value="<?php echo $SESSION['s_staturl']; ?>" SIZE="58" MAXLENGTH="255"> <?php show_error('s_staturl_err'); ?></TD> </tr> <tr> <td align="right">Sponsors Email</td> <td align="right"> </td> <td align="left"> <input name="s_email" type="text" id="s_email" value="<?php echo $SESSION['s_email']; ?>" SIZE="58" MAXLENGTH="255"> <?php show_error('s_email_err'); ?></TD> </tr> <tr> <td align="right">User Name</td> <td align="right"> </td> <td align="left"> <input name="s_uname" type="text" id="s_uname" value="<?php echo $SESSION['s_uname']; ?>" SIZE="58" MAXLENGTH="255"> <?php show_error('s_uname_err'); ?></TD> </tr> <tr> <td align="right">Password</td> <td align="right"> </td> <td align="left"> <input name="s_pass" type="password" id="s_pass" value="<?php echo $SESSION['s_pass']; ?>" SIZE="58" MAXLENGTH="255"> <?php show_error('s_pass_err'); ?></TD> </tr> <tr> <td align="right">Status</td> <td align="right"> </td> <td align="left"> <select name="select" id="select"> <option value="Pending" <?php show_selected('s_status', 'Pending'); ?> >Pending</option> <option value="Active" <?php show_selected('s_status', 'Active'); ?> >Active</option> <option value="Inactive" <?php show_selected('s_status', 'Inactive'); ?> >Inactive</option> <option value="Defunct" <?php show_selected('s_status', 'Defunct'); ?> >Defunct</option> </select> </td> </tr> <tr> <td colspan="3" align="center"> <input name="reset-Sponsors" type="reset" id="Reset_Sponsors" value="Clear Form"> <input type="submit" name="Submit_Sponsors" value="Submit Sponsor"></td> </tr> </table> </form> </body> </html> Regards Liz |
(#29 2003-09-02 23:29:08) Post Reply |
|
logspirit
|
Liz, It looks good! As we discussed in the Workshop, foreach($array as $key => $value){ do... } deals with EACH key/value pair in the array one at a time and does whatever You have in the 'do...' block of code.
Don't worry about the 'fog' of understanding at this time, programming has it's learning curve --- once You get a good feel for the basic logic of programming, the rest is just syntax, which changes from language to language, and even within languages as they improve. I'm already looking forward to the sites You will be developing!!! Can I say "good`aye" if I'm not Australian? Oh well, cut an old Florida swamp hound some slack... he he Y'all come on back now, ya hear? logspirit |
|
View Previous Thread Print This Page |
School Map:
Home / Enroll
$ Student Records; Class
$ News @ LIVE
Tech Radio * Support/FAQ
| Store
Advertising
| Link To Us | Privacy
Copyright 2000-2002 Script School Productions / KMR
Enterprises
No part of this website may be reproduced, copied and/or distributed
in any medium
without express written permission