First of all, I really examined the to levels of these forums trying to decide where best to post this, and I kept coming back here... so I hope this is ok.
I am going to submit a sample page of PHP code that makes connections to a mysql database.
I believe the code is generic enough that my hope is that some of you with lots of experience under your belts might share with me some ideas or critiques with regards to the code I submit--how it could be better, more optimized, etc.
Without any formal training my fear is that while my code works, and seems to work just fine, that there are my things I am not doing that I should/could be doing.
This is a basic form that contains a few text entry fields, a dynamically built pull-down menu etc. A few items are not included, such as a javascript based calendar that lets the user easily choose dates. Also the HTML skeleton is also not included that forms the head and footer of the page. I have validated it against w3 and it validates so I am not too concerned about that side of things. Without further ado...
<?php
include("includes/conf.php");
// begin to collect the info posted via the form IF anything has been posted yet
$addJob=(isset($_POST['job_form'])) ? $_POST['job_form'] : '';
$addBldg=(isset($_POST['bldg_form'])) ? $_POST['bldg_form'] : '';
$addDateposted=(isset($_POST['dateposted_form'])) ? $_POST['dateposted_form'] : '';
$addSchedC=(isset($_POST['schedc_form'])) ? $_POST['schedc_form'] : '';
$addExpire=(isset($_POST['expire_form'])) ? $_POST['expire_form'] : '';
$addDateexpire=(isset($_POST['dateexpire_form'])) ? $_POST['dateexpire_form'] : '';
$addDistrict=(isset($_POST['district_form'])) ? $_POST['district_form'] : '';
// end form POST collection
// The following IF.. ELSE.. checks to see if the form has been submitted. If it has, then process the results,
// otherwise just show the form to the user.
IF ($addJob) {
$title = 'Add record confirmation'; //Used to create the HTML <TITLE></TITLE> tag
$retstatus = '<p><span class="Infotext">Record successfully added.</span></p>';
// Begin conditional SQL query. Creates query depending on which fields are submitted
$sql_add = "INSERT INTO `postings`.`jobs` (`job`, `bldg`, `dateposted`";
IF ($addExpire) {
$sql_add .= ", `expire`, `dateexpire`";
}
IF ($addSchedC) {
$sql_add .= ", `schedc`";
}
IF ($addDistrict) {
$sql_add .= ", `district`";
}
$sql_add .= ") VALUES ('$addJob', '$addBldg', '$addDateposted'";
IF ($addExpire) {
$sql_add .= ", '$addExpire', '$addDateexpire'";
}
IF ($addSchedC) {
$sql_add .= ", '$addSchedC'";
}
IF ($addDistrict) {
$sql_add .= ", '$addDistrict'";
}
$sql_add .= ")";
// End conditional SQL query
//echo $sql_add;
$result = mysql_query($sql_add);
if (!$result) {
die('Invalid query: ' . mysql_error());
} ELSE {
// update the lastupdate field to the current timestamp. This will be used later to write to the web site the last time the database was updated.
$sql_modtime = "UPDATE `postings`.`modtime` SET `lastupdate` = Now() WHERE `modtime`.`mid` = '1' ";
$result2 = mysql_query($sql_modtime);
if (!$result2) {
die('Invalid query: ' . mysql_error());
}
include("includes/htmlhead.htm");
echo $retstatus;
include("includes/htmlfoot.htm");
}
} ELSE {
$title = 'Add a Job!'; //Used to create the HTML <TITLE></TITLE> tag
$today = date("n-j-Y"); //create today's date for use in the form
$todayplusfive = date("n-j-Y", strtotime("+5 days")); //Calculate 5 days from $today
/*
* Begin 'addjob' form
* The following form variable names will be defined:
* job_form , bldg_form , dateposted_form , schedc_form , expire_form , dateexpire_form , district_form
*
*/
$formbody = '
<form name="addjob" action="add.php" method="post">
<table border="0" cellspacing="0" cellpadding="2" bgcolor="black">
<tr>
<td>
<table border="0" cellspacing="1" cellpadding="5" bgcolor="gray">
<tr class="trcolor">
<td>
<span class="datalabel">Job Description:</span><br />
<input type="text" name="job_form" id="job" value="" size="30" />
<p></p>
</td>
<td>
<span class="datalabel">Building:</span>
<br />
<select name="bldg_form" id="bldg">
<option value="" selected="selected">Choose one ...</option>
';
// Build the drop-down menu dynamically from the "location" table in the database
$sql=mysql_query("SELECT DISTINCT fullname FROM location ORDER BY fullname ASC");
while ($row = mysql_fetch_object($sql)) {
$formbody .= '<option value="'.$row->fullname.'">'.$row->fullname.'</option>
';
}
$formbody .='</select>
<br />
<div class="small_info"><a href="location.php">Edit this list</a></div>
</td>
</tr>
<tr class="trcolor">
<td>
<span class="datalabel">Date Posted:</span>
<br />
<input type="text" name="dateposted_form" id="dateposted" value="'. $today /* Default to today's date */.'" />
<script language="javascript" type="text/javascript">
var basicCal = new calendar("FIELD:document.addjob.dateposted_form;FORMAT:0;DELIMITER:-;");
basicCal.writeCalendar();
</script>
</td>
<td nowrap="nowrap">
<input type="checkbox" name="schedc_form" id="schedc" value="1" />
<span class="datalabel">Schedule C</span>
</td>
</tr>
<tr class="trcolor">
<td>
<input type="checkbox" name="expire_form" id="expire" value="1" />
<span class="datalabel">Expiration Date?</span>
<br />
If so choose...<br />
<input type="text" name="dateexpire_form" id="dateexpire" value="'. $todayplusfive /* Default to 5 days from today */.'" />
<script language="javascript" type="text/javascript">
var basicCal2 = new calendar("FIELD:document.addjob.dateexpire_form;FORMAT:0;DELIMITER:-;");
basicCal2.writeCalendar();
</script>
</td>
<td nowrap="nowrap">
<input type="checkbox" name="district_form" id="district" value="1" />
<span class="datalabel">District-Wide</span>
</td>
</tr>
<tr class="trcolor">
<td colspan="2">
<div class="small_info"><input name="Add" value="Submit" type="submit" /></div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>';
// Begin writing HTML to the screen...
include("includes/htmlhead.htm");
echo $formbody;
echo '<div class="lastmodified">Last modified: ' . date ("F d Y h:i:s A.", getlastmod()) . '</div>'; //Insert file modification date and time
include("includes/htmlfoot.htm");
} // END IF.. ELSE... Statement
?>