I am trying to write a form that will add a guest to a guest table in a
MySQL database and then reload the page to display any guests that have
been added as well as redisplay the guest form to add more guests.
I have two problems I am running into. I can get the script to run just
fine and it will come up with the event, any guest that has been added
and the form, but when I click submit it doesn't reload the page and
for some unknown reason it resets my $_SESSION['firstname'] and
['lastname'], because on the top of the page I ahve you are logged on
as X and after hitting submit if I hit reload everything except the
form pops back up and changes the "you are logged in as X" to "you are
logged in as guest"
The form itself is pretty standard, containing fields for first, last,
mi, relation, hidden and submit. THe values are set to $_POST['value'].
The code on the page I am running and for the processing of the form I
will show below.
Thanks for any help
I am pretty new to PHP and MySQL so I know my code might not be the
most fine tuned.
code of guest page (the one the person logged on will see)
<?php
session_start() ;
include "links.php" ;
$name = $_SESSION['firstname'];
$name .= " ". $_SESSION['lastname'];
echo "You are logged on as $name<br />";
include "mysql_connect_ snacks.php";
mysql_select_db ("chemdata") ;
$pid = $_SESSION['pid'];
$query = "SELECT email FROM alumni WHERE pid='$pid'";
$result = mysql_query($qu ery) or exit(mysql_erro r);
$row = mysql_num_rows( $result);
if ($row == 1)
{
$event = $_SESSION['event'];
$pid = $_SESSION['pid'];
echo "<p><font size=+3>$event</font></p>";
include "mysql_connect_ snacks.php";
mysql_select_db ("chemdata") ;
$guest_qry = "SELECT * FROM guest WHERE pid='$pid'";
$guest_res = mysql_query($gu est_qry);
$num = mysql_num_rows( $guest_res);
if ($num != 0) {
$guest_table = '
<table width =\"50%\">
<tr><th colspan=5><div align="center"> Guests That Have Been
Added</div></th></tr>
<tr><th>Check </th><th>Last Name</th><th>First
Name</th><th>M.I.</th><th>Relation </th></tr>';
while ($row = mysql_fetch_arr ay($guest_res)) {
$i = 0;
while ($i < $num) {
$guest_table .= '
<tr>
<td width=5%><input type="checkbox" name="update['. $i .']
value=/"1/" /></td>
<td><div align="center"> <span class="black_te xt">' .
$row['glastname'] . '</span></div></td>
<td><div align="center"> <span class="black_te xt">' .
$row['gfirstname'] . '</span></div></td>
<td><div align="center"> <span class="black_te xt">' . $row['mi'] .
'</span></div></td>
<td><div align="center"> <span class="black_te xt">' .
$row['relation'] . '</span></div></td>
</tr>';
$i++;
}
}
$guest_table .= '</table>';
echo "$guest_tab le";
} else {
echo "No guests are yet registered";
}
include "guest_code.php ";
include "guest_add.php" ;
} else {
session_destroy ();
echo "This is a restricted site, <a
href=\"http://snacks.chem.uma ss.edu/session\">Pleas e Click here to
login</a>";
}
?>
and the code that process the form
<?php
if ($_POST[op] != "ds") {
//this will show them the form
echo "$form_bloc k";
} else if ($_POST[op] == "ds") {
// array of required fields:
$required = array('lastname ','firstname',' relation');
// loop through required fields
foreach($requir ed as $value)
{
// if a value is not set or is empty:
if(!isset($_POS T[$value]) || trim($_POST[$value]) == '')
{
$errors[] = 'Your ' . $value . ' is still needed';
}
}
// if there were any errors:
if(isset($error s))
{
echo '<h2>Error!</h2>';
echo '<p>The following errors occurred:</p>';
echo '<ul>';
foreach($errors as $value)
{
echo '<li>' . $value . '</li>';
}
echo '</ul>';
echo '<p><a href="">Please go back and try again.</a></p>';
}
// else there were no errors:
else {
foreach($_POST as $key=>$value)
{
if(trim($value) != '')
{
if(get_magic_qu otes_gpc() == 1)
{
$$key = trim(stripslash es($value));
}
else {
$$key = trim($value);
}
}
else {
$$key = '';
}
}
// CONNECT TO DB:
include "mysql_connect_ snacks.php";
// select the DB
$db = mysql_select_db ('chemdata') or die(mysql_error ());
$pid = $_SESSION['pid'];
$eid = 1;
// create a query statement:
$guest_query = "INSERT INTO guest
SET glastname = '" . mysql_real_esca pe_string($glas tname)
.. "',
gfirstname = '" . mysql_real_esca pe_string($gfir stname) .
"',
mi = '" . mysql_real_esca pe_string($_POS T['mi']) . "',
relation = '" . mysql_real_esca pe_string($rela tion) . "',
pid = '" . mysql_real_esca pe_string($pid) . "',
eid = '" . mysql_real_esca pe_string($eid) . "'";
// execute the query
$result = mysql_query($gu est_query) or die(mysql_error ());
exit;
}
}
//close MySQL
$close = mysql_close();
exit;
header("locatio n: guest_reg.php") ;
?>
MySQL database and then reload the page to display any guests that have
been added as well as redisplay the guest form to add more guests.
I have two problems I am running into. I can get the script to run just
fine and it will come up with the event, any guest that has been added
and the form, but when I click submit it doesn't reload the page and
for some unknown reason it resets my $_SESSION['firstname'] and
['lastname'], because on the top of the page I ahve you are logged on
as X and after hitting submit if I hit reload everything except the
form pops back up and changes the "you are logged in as X" to "you are
logged in as guest"
The form itself is pretty standard, containing fields for first, last,
mi, relation, hidden and submit. THe values are set to $_POST['value'].
The code on the page I am running and for the processing of the form I
will show below.
Thanks for any help
I am pretty new to PHP and MySQL so I know my code might not be the
most fine tuned.
code of guest page (the one the person logged on will see)
<?php
session_start() ;
include "links.php" ;
$name = $_SESSION['firstname'];
$name .= " ". $_SESSION['lastname'];
echo "You are logged on as $name<br />";
include "mysql_connect_ snacks.php";
mysql_select_db ("chemdata") ;
$pid = $_SESSION['pid'];
$query = "SELECT email FROM alumni WHERE pid='$pid'";
$result = mysql_query($qu ery) or exit(mysql_erro r);
$row = mysql_num_rows( $result);
if ($row == 1)
{
$event = $_SESSION['event'];
$pid = $_SESSION['pid'];
echo "<p><font size=+3>$event</font></p>";
include "mysql_connect_ snacks.php";
mysql_select_db ("chemdata") ;
$guest_qry = "SELECT * FROM guest WHERE pid='$pid'";
$guest_res = mysql_query($gu est_qry);
$num = mysql_num_rows( $guest_res);
if ($num != 0) {
$guest_table = '
<table width =\"50%\">
<tr><th colspan=5><div align="center"> Guests That Have Been
Added</div></th></tr>
<tr><th>Check </th><th>Last Name</th><th>First
Name</th><th>M.I.</th><th>Relation </th></tr>';
while ($row = mysql_fetch_arr ay($guest_res)) {
$i = 0;
while ($i < $num) {
$guest_table .= '
<tr>
<td width=5%><input type="checkbox" name="update['. $i .']
value=/"1/" /></td>
<td><div align="center"> <span class="black_te xt">' .
$row['glastname'] . '</span></div></td>
<td><div align="center"> <span class="black_te xt">' .
$row['gfirstname'] . '</span></div></td>
<td><div align="center"> <span class="black_te xt">' . $row['mi'] .
'</span></div></td>
<td><div align="center"> <span class="black_te xt">' .
$row['relation'] . '</span></div></td>
</tr>';
$i++;
}
}
$guest_table .= '</table>';
echo "$guest_tab le";
} else {
echo "No guests are yet registered";
}
include "guest_code.php ";
include "guest_add.php" ;
} else {
session_destroy ();
echo "This is a restricted site, <a
href=\"http://snacks.chem.uma ss.edu/session\">Pleas e Click here to
login</a>";
}
?>
and the code that process the form
<?php
if ($_POST[op] != "ds") {
//this will show them the form
echo "$form_bloc k";
} else if ($_POST[op] == "ds") {
// array of required fields:
$required = array('lastname ','firstname',' relation');
// loop through required fields
foreach($requir ed as $value)
{
// if a value is not set or is empty:
if(!isset($_POS T[$value]) || trim($_POST[$value]) == '')
{
$errors[] = 'Your ' . $value . ' is still needed';
}
}
// if there were any errors:
if(isset($error s))
{
echo '<h2>Error!</h2>';
echo '<p>The following errors occurred:</p>';
echo '<ul>';
foreach($errors as $value)
{
echo '<li>' . $value . '</li>';
}
echo '</ul>';
echo '<p><a href="">Please go back and try again.</a></p>';
}
// else there were no errors:
else {
foreach($_POST as $key=>$value)
{
if(trim($value) != '')
{
if(get_magic_qu otes_gpc() == 1)
{
$$key = trim(stripslash es($value));
}
else {
$$key = trim($value);
}
}
else {
$$key = '';
}
}
// CONNECT TO DB:
include "mysql_connect_ snacks.php";
// select the DB
$db = mysql_select_db ('chemdata') or die(mysql_error ());
$pid = $_SESSION['pid'];
$eid = 1;
// create a query statement:
$guest_query = "INSERT INTO guest
SET glastname = '" . mysql_real_esca pe_string($glas tname)
.. "',
gfirstname = '" . mysql_real_esca pe_string($gfir stname) .
"',
mi = '" . mysql_real_esca pe_string($_POS T['mi']) . "',
relation = '" . mysql_real_esca pe_string($rela tion) . "',
pid = '" . mysql_real_esca pe_string($pid) . "',
eid = '" . mysql_real_esca pe_string($eid) . "'";
// execute the query
$result = mysql_query($gu est_query) or die(mysql_error ());
exit;
}
}
//close MySQL
$close = mysql_close();
exit;
header("locatio n: guest_reg.php") ;
?>