I am trying to create code to insert information from a form into three tables. The tables are:
1. alumni (for alumni of my association to enter year, firstname, lastname, e-mail)
2. guest (on the form are sports for four guests including firstname, lastname and relationship). The guest table aslo has a column for alumniID to link which guest belongs to which alumni.
3. rsvp_list (This contains eventID, GuestID and AlumniID to create an index of everyone that is going.
Some of the problems I am running into is the form has a checkbox for attending or not attending. We may get alumni who enter zero guest to alumni who enter 4. I tried writing the code as follows but I get a duplicate entry and I have spent a lot of time trying to figure out where I went wrong.
//Set variables for database and table
$table_alumni = "alumni";
$table_guest = "guest";
$table_rsvp = "rsvp_list";
//SQL to insert alumni info into database
mysql_select_db('chemdata') or exit(mysql_error());
$sql = "INSERT INTO $table_alumni SET lastname='$POST[lastname]', firstname='$POST[firstname]', mi='$POST[mi]', advisor='$POST[advisor]',
email='$POST[email]', year_grad='$POST[year]', degree='$POST[degree]', rsvp='$POST[attend]'";
$result = @($sql, $connection) or die(mysql_error());
$pid = mysql_insert_id();
mysql_close();
//if statements to determine if alumni entered a guest and insert guest into guest table as well as guest and alumni into rsvp table
if ($POST[glast] != "") {
include "mysql_connect_snacks.php";
mysql_select_db('chemdata') or exit(mysql_error());
$sql2 = "INSERT INTO $table_guest SET glastname='$POST[glast]', gfirstname='$POST[gfirst]', relation='$POST[relation]', pid='$pid'";
$result2 = @($sql2, $connection) or die(mysql_error());
$gid1 = mysql_insert_id();
$sql6 = "INSERT INTO $table_rsvp SET eid='1', pid='$pid', gid='$gid1'";
$result6 = @($sql6, $connection) or die(mysql_error());
mysql_close;
}
if ($POST[glast2] != "") {
include "mysql_connect_snacks.php";
mysql_select_db('chemdata') or exit(mysql_error());
$sql3 = "INSERT INTO $table_guest SET glastname='$POST[glast2]', gfirstname='$POST[gfirst2]', relation='$POST[relation2]', pid='$pid'";
$result3 = @($sql3, $connection) or die(mysql_error());
$gid2 = mysql_insert_id();
$sql7 = "INSERT INTO $table_rsvp SET eid='1', pid='$pid', gid='$gid2'";
$result8 = @($sql7, $connection) or die(mysql_error());
mysql_close;
}
if ($POST[glast3] != "") {
include "mysql_connect_snacks.php";
mysql_select_db('chemdata') or exit(mysql_error());
$sql4 = "INSERT INTO $table_guest SET glastname='$POST[glast3]', gfirstname='$POST[gfirst3]', relation='$POST[relation3]', pid='$pid'";
$result4 = @($sql4, $connection) or die(mysql_error());
$gid3 = mysql_insert_id();
$sql8 = "INSERT INTO $table_rsvp SET eid='1', pid='$pid', gid='$gid3'";
$result8 = @($sql8, $connection) or die(mysql_error());
mysql_close;
}
if ($POST[glast4] != "") {
include "mysql_connect_snacks.php";
mysql_select_db('chemdata') or exit(mysql_error());
$sql5 = "INSERT INTO $table_guest SET glastname='$POST[glast4]', gfirstname='$POST[gfirst4]', relation='$POST[relation4]', pid='$pid'";
$result5 = @($sql5, $connection) or die(mysql_error());
$gid4 = mysql_insert_id();
$sql9 = "INSERT INTO $table_rsvp SET eid='1', pid='$pid', gid='$gid4'";
$result9 = @($sql9, $connection) or die(mysql_error());
mysql_close;
}
Thanks
Yanks