About the JobBoard pt. II
<-back

9/13/00 - To write to the InterNetworkers Database table 'jobboard', we have to look at the /jobboard/post.php3 and /lib/jobdb.php3 pages. The form is on the /post.php3 page, and is quite different from the normal formmail-style 'get' forms. However, we can distill it for clarity. If you use your browser's 'view source' feature, you can see it in all its glory.

To start, the form has essentially 1 field: form_field[]. There's also a hidden field telling it to 'add' it to the database.

<form action="/lib/jobdb.php3" method="post" name="add">

<input type="hidden" name="job_action" value="add" checked>

Company Name: <input type="text" name="form_field[]" size="30"><br>
Company Location: <input type="text" name="form_field[]" size="30"><br>
Company Contact: <input type="text" name="form_field[]" size="30"><br>
<input type="submit">  <input type="Reset"> 
</form>                                
You can see that each field corresponds to a field name in the db table jobboard. When this gets posted to the /lib/jobdb.php3 page, things get a bit more complex. First it connects to the database.
	// Connect to the Database
$link = mysql_connect("path.to.the.db", "username", "password");
	
	// list the tables in the db
$result = mysql_list_tables ("InterNetWorkers");

	// create an array that contains the table names above
$tb_names[$i] = mysql_tablename ($result, "0");

Then it counts the number of rows.
if ($job_action == "add") {
// count the number of fields
$num_fields = count($form_field);
Then it cycles through the form fields using a while function, either formatting them for submission to the jobboard (adding '' marks) or assigning a null value (telling it in a nice way that you're not sending it any information).
$i = 0;
while ($i < $num_fields) {
if ($form_field[$i] != "") {
	$form_field_submit[$i] = "'$form_field[$i]'";
} else {
$form_field_submit[$i] = "NULL";
};
$i++;
}
It then concatenates the values (strings them all together), and places commas between them.
$i = 0;
while ($i < $num_fields) {
if ($query_values == "") {
$query_values = $form_field_submit[$i];
$query_do = $form_field[$i];
} else {
$query_values = $query_values . ", " . $form_field_submit[$i];
$query_do = $query_do . $form_field[$i];
};
$i++;
}
It then places the info into the database and sends an email to the dba for review. The $form_action is to let the person know if everything went OK.
if ($query_do != ""){
	$form_action = "Job Added";
	$Add_address = mysql_query ("INSERT INTO jobboard 
		VALUES ($query_values)");
	mail("joe@komejo.com", "Jobboard Posting", "$query_values");
} else {
	$form_action = "No Job Added";
}
Finally, we send the user information on how things went, or an error, and close the connection to the database.
?>

<b>The Jobboard:</b>
<?print $form_action;?><br>
Thanks for posting - the DBA has been sent an e-mail.

<?
 
// return clean results of query 
$i = 0;
while ($i < $num_fields) {
print "$form_field[$i]<br>";
$i++;
}
} else {
	print "Error. Hit the 'back' button 
	and you won't lose your data...";
};

mysql_close ($link);
That's about it! There's an Authorization field in the table that the dba edits to approve the post, after which the post will appear for 30 days. What could be simpler?

~Joe K



<-back
Questions? Comments? Send them to Joe K