You are here: Help with Forms « PHP Programming Language « IT news, forums, messages
Help with Forms

Posted by bob.herbst on 11/19/42 11:47

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($query) or exit(mysql_error);
$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($guest_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_array($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_text">' .
$row['glastname'] . '</span></div></td>
<td><div align="center"><span class="black_text">' .
$row['gfirstname'] . '</span></div></td>
<td><div align="center"><span class="black_text">' . $row['mi'] .
'</span></div></td>
<td><div align="center"><span class="black_text">' .
$row['relation'] . '</span></div></td>
</tr>';
$i++;
}
}
$guest_table .= '</table>';
echo "$guest_table";

} 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.umass.edu/session\">Please 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_block";
} else if ($_POST[op] == "ds") {
// array of required fields:
$required = array('lastname','firstname','relation');
// loop through required fields
foreach($required as $value)
{
// if a value is not set or is empty:
if(!isset($_POST[$value]) || trim($_POST[$value]) == '')
{
$errors[] = 'Your ' . $value . ' is still needed';
}
}
// if there were any errors:
if(isset($errors))
{
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_quotes_gpc() == 1)
{
$$key = trim(stripslashes($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_escape_string($glastname)
.. "',
gfirstname = '" . mysql_real_escape_string($gfirstname) .
"',
mi = '" . mysql_real_escape_string($_POST['mi']) . "',
relation = '" . mysql_real_escape_string($relation) . "',
pid = '" . mysql_real_escape_string($pid) . "',
eid = '" . mysql_real_escape_string($eid) . "'";
// execute the query
$result = mysql_query($guest_query) or die(mysql_error());
exit;
}
}
//close MySQL
$close = mysql_close();
exit;
header("location: guest_reg.php");
?>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация