Joanne_08 wrote:Hi there,
$course_name= "navigation & search";
- try and keep any db connection stuff out or your scripts.
So put this into a script called dbconnect.php.
<?php
$host = "localhost"; // Database server
$user = "root"; // Database username
$pass = "joanne"; // Database password
$db = "training"; // Database name
$db1=mysql_connect("$host","$user","$pass");
mysql_select_db("$db");
$ok = mysql_select_db("$db");
if (!ok)
{
die("<br>" . mysql_errno().":".mysql_errno()."<br>");
}
?>
Then include it at the top of your pages needing a connection to the database.
<?php
include("dbconnect.php");
?>
FYI. never include your username and passwords in any forums. :evilgrin:
Now I am assuming that the course name and course date is data stored per row.
Dummy Sql below.
CREATE TABLE `training` (
`id` int(5) unsigned NOT NULL auto_increment,
`course_name` varchar(32) default '',
`course_date` varchar(20) default '',
`course_cost` varchar(20) default '',
`valid` int(5) unsigned default '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 ;
INSERT INTO `training` VALUES(1, 'Navigation & Search', 'April 15th 2008', '$97.00', '1');
INSERT INTO `training` VALUES(2, 'Basic PhP 101', 'April 21st 2008', '$47.00','1');
[You may have other field names]
No I hope you noticed I added a field called valid with a default value of '0' in the dummy sql.
This is so you can add as many courses as you want in advance to the database and only have those with a value of 1 show as a return on your web page.
I assume the object is to get the relevant course date to the course being offered and cost involved.
I would write my submit form different so as to post the given id number that would then return the course_name, course_date and the cost of that particular course.
I would use the unique id number to pull all the relevant information from the db about a course to be displayed on the web site.
Here's my simple select form.
<form name='submit' method='post' action='<?=$php_self?>'>
<center><select name="id" size="1"><option value="" selected>Select Course</option><script language="php">
$query = "select id,course_name from training where valid ='1' order by id asc";
$result = mysql_query($query);
if(mysql_num_rows($result)) {
// we have at least one user, so show all users as options in select form
while($row = mysql_fetch_array($result))
{
print("<option value=\"$row[id]\">$row[course_name]</option>");
}
} else {
print("<option value=\"\">No Courses Added to Database yet.</option>");
}
</script></select></center>
</form>
<form action="<?= $php_SELF ?>" method="POST">
This implies you are posting the information from the submit form back to itself and NOT a second page per sae.
So now you have to have some code for your php script to read and request this information from the database.
<?php
if (isset($_POST['submit']))
{
// do some post checking to see if everything was sent properly
if(!$_POST['id']) //if id is blank after submit button pushed
{
echo("OOPs! You forgot to select a course for us to search. Please use your back button and correct.");
exit();//Stops the rest of the script being parsed if the above error occurs.
// you might want to add here your footer.php or code to call your footer information.
}// closes if
else{ //if everything is ok we connect and drag the information out and display it.
$id = addslashes(@$_POST['id']);
}// closes else
$select= "select * from training where id='$id' and valid='1'";
$result=mysql_query($select) or die ("can not select $id from training table");
while($row = mysql_fetch_array($result))
{
$id = stripslashes($row['id']);
$course_name = stripslashes($row['course_name']);
$course_date = stripslashes($row['course_date']);
$course_cost = stripslashes($row['course_cost']);
$valid = stripslashes($row['valid']);
}// closes while
if (!$course_name) // if no course_name returned in result
{
echo("OOPs. $course_name hasn't been confirmed yet.");
}// closes if
else// we found a result and can display it.
{
echo("Thanks for requesting information on $course_name. You will find the course starts on $course_date with a non refundable fee of $course_cost payable before the course starts.");
}// closes else
}//closes isset post
?>
Now you could even add a payment button so they can pay while it's fresh on their minds. :rolleyes:
Complete script below
<?php
include("dbconnect.php");
if (isset($_POST['submit']))
{
// do some post checking to see if everything was sent properly
if(!$_POST['id']) //if id is blank after submit button pushed
{
echo("OOPs! You forgot to select a course for us to search. Please use your back button and correct.");
exit();//Stops the rest of the script being parsed if the above error occurs.
// you might want to add here your footer.php or code to call your footer information.
}// closes if
else{ //if everything is ok we connect and drag the information out and display it.
$id = addslashes(@$_POST['id']);
}// closes else
$select= "select * from training where id='$id' and valid='1'";
$result=mysql_query($select) or die ("can not select $id from training table");
while($row = mysql_fetch_array($result))
{
$id = stripslashes($row['id']);
$course_name = stripslashes($row['course_name']);
$course_date = stripslashes($row['course_date']);
$course_cost = stripslashes($row['course_cost']);
$valid = stripslashes($row['valid']);
}// closes while
if (!$course_name) // if no course_name returned in result
{
echo("OOPs. $course_name hasn't been confirmed yet.");
}// closes if
else// we found a result and can display it.
{
echo("Thanks for requesting information on $course_name. You will find the course starts on $course_date with a non refundable fee of $course_cost payable before the course starts.");
}// closes else
}//closes isset post
?>
<form name='submit' method='post' action='<?=$php_self?>'>
<center><select name="id" size="1"><option value="" selected>Select Course</option><script language="php">
$query = "select id,course_name from training where valid ='1' order by id asc";
$result = mysql_query($query);
if(mysql_num_rows($result)) {
// we have at least one user, so show all users as options in select form
while($row = mysql_fetch_array($result))
{
print("<option value=\"$row[id]\">$row[course_name]</option>");
}
} else {
print("<option value=\"\">No Courses Added to Database yet.</option>");
}
</script></select></center>
</form>
Hope this helps ya.
Arty