PHP Classes

PHP5 and MySQL

Recommend this page to a friend!

      Top level forums  >  PHP Specialists  >  General  >  PHP5 and MySQL  
Subject:PHP5 and MySQL
Summary:forign key and insert problems
Messages:4
Author:Karen Bellware
Date:2011-03-01 17:26:42
Update:2011-07-15 15:54:59
 

  1. PHP5 and MySQL   Reply   Report abuse  
Picture of Karen Bellware Karen Bellware - 2011-03-01 21:32:51
Hi, I have am developing a registration form for a summer childnres event in our religous community.

I have the form created, the php written, and the database designed BUT when I complete the form I get the following error:

te Error: Cannot add or update a child row: a foreign key constraint fails (`ilmcompetition/Partners2011`, CONSTRAINT `Partners2011_ibfk_3` FOREIGN KEY (`ID`) REFERENCES `Registration2011` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE)

I'm using phpmyadmin to create and manage the mysql database. I have two tables as follows:

Registration2011
ID int(10) Not Null Primary Key Auto Increment
Level char(50) No
LastName char(50) No
FirstName char(50) No
Grade int(2) No
DOB char(10) No
eMail char(50) No
Phone char(15) No
Address char(100) No
City char(50) No
State char(10) No
Zip int(5) No
ParentsFirstName char(50) No
ParentsLastName char(50) No
Location char(20) No
Date date No

ID int(10) Not Null Primary Key Auto Increment (this is also a FK to ID in Registration2011 table)
pLevel char(50) Not Null (inxexed and also FK of Level in Registration Table)
PartnerLastName char(50) No
PartnerFirstName char(50) No
PartnerGrade int(2) No
PartnerDOB char(20) No
PartnerEmail char(50) No
PartnerPhone char(15) No
PartnerAddress char(100) No
PartnerCity char(20) No
PartnerState char(10) No
PartnerZip int(5) No
PartnerParentFirstName char(50) No
PartnerParentLastName char(50) No
Date date No
************NO stands for Not Null*****************

-----------------------------PHP CODE---------------------------------

$con = mysql_pconnect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
mysql_query($con);
mysql_select_db($dbname);

//Short Variables
$level=addslashes($_POST['level']);
$fname=addslashes($_POST['fname']);
$lname=addslashes($_POST['lname']);
$grade=addslashes($_POST['grade']);
$dob=addslashes($_POST['dob']);
$email=addslashes($_POST['email']);
$ph=addslashes($_POST['phone']);
$addy=addslashes($_POST['address']);
$city=addslashes($_POST['city']);
$st=addslashes($_POST['state']);
$zip=addslashes($_POST['zip']);
$ptLname=addslashes($_POST['parentLname']);
$ptFname=addslashes($_POST['parentFname']);
$location=addslashes($_POST['location']);
$pLname=addslashes($_POST['pLname']);
$pFname=addslashes($_POST['pFname']);
$pGrade=addslashes($_POST['pGrade']);
$pDob=addslashes($_POST['pDOB']);
$pEmail=addslashes($_POST['pEmail']);
$pPh=addslashes($_POST['pPhone']);
$ppLname=addslashes($_POST['pParentLname']);
$ppFname=addslashes($_POST['pParentFname']);
$pAddy=addslashes($_POST['pAddress']);
$pCity=addslashes($_POST['pCity']);
$pSt=addslashes($_POST['pState']);
$pZip=addslashes($_POST['pZip']);
$location=addslashes($_POST['location']);
$date=time();

if (isset($_POST['Registration'])) {
import_request_variables("post", "new");
$missingfields = array();
$required = array("$level"=>"Level participating in", "$fname"=>"First Name", "$lname"=>"Last Name", "$grade"=>"Grade Completed", "$dob"=>"DOB", "$email"=>"e-Mail", "$phone"=>"Phone Number", "$address"=>"Address", "$city"=>"City", "$state"=>"State", "$zip"=>"Zip Code", "$parentFname"=>"Parents First Name", "$parentLname"=>"Parents Last Name", "$location"=>"Location", "$pFname"=>"Partners First Name", "$pLname"=>"Partners Last Name", "$pGrade"=>"Partners Grade", "$pDOB"=>"Partners DOB", "$pEmail"=>"Partners Email", "$pPhone"=>"Partners Phone Number", "$pParentFname"=>"Partners Parents First Name", "$pParentLname"=>"Partners Parents Last Name", "$pAddress"=>"Partners Address", "$pCity"=>"Partners City", "$pState"=>"Partners State", "$pZip"=>"Partners Zip Code", "$location"=>"Your test location");

while (list($var, $val) = each($required)) {
if (isset($newForm[$var]) && $newForm[$var] != '') {
// check this value further here
} else {
$missingfields[$var] = $val;
}
}

if (count($missingfields)) {
print "You missed out one or more fields:<br />Please go <a href=\"registration2011.html\"> BACK </a> and complete all fields.<br />";

while(list($var, $val) = each($missingfields)) {
print $val . "<br />";
}
} else {
print "Jazak Allah Khair! Your Registration has been submitted and you should receive a confirmation email soon. If you do not receive an email soon, please <a href=\"mailto:registration@ilmcompetition.com\">email</a> us to let us know.<br />";
var_dump($newForm['location']);
exit;
}
}

$sql="INSERT INTO Registration2011 (Level, LastName, FirstName, Grade, DOB, eMail, Phone, Address, City, State, Zip, ParentsFirstName, ParentsLastName, Location, Date)
VALUES
('$level','$lname','$fname','$grade','$dob','$email','$ph','$addy','$city','$st','$zip]','$ptLname','$ptFname','$location','$date')";
mysql_query($sql);

$sql="INSERT INTO Partners2011 (PartnerLastName, PartnerFirstName, PartnerGrade, PartnerDOB, PartnereMail, PartnerPhone, PartnerAddress, PartnerCity, PartnerState, PartnerZip, PartnerParentFirstName, PartnerParentLastName)
VALUES
('$pLname','$pFname','$pGrade','$pDob','$pEmail','$pPh','$ppLname','$ppFname','$pAddy','$pCity','$pSt','$pZip')";
mysql_query($sql);

if (!mysql_query($sql, $con))
{
die('Error: ' . mysql_error());
}

mysql_close($con);
?>

---------------------HTML FORM-----------------------------------

<form action="registration_processor.php" method="post">
<fieldset><legend>Your Information</legend>
<table width="90%" align="center" cellpadding="5" cellspacing="10" style="background-color:#FFE4C4;"><tr>
<td>Level:<br /> <select name=" level " />
<option value="PKorKG">PK or KG</option><option value="g1to3">Grade 1 thru 3</option><option value="g4to6">Grade 4 thru 6</option><option value="g7to9">Grade 7 thru 9</option><option value="g10to12">Grade 10 thru 12</option></select></td><td>Last Grade completed:<br />
<select name="grade"><option value="PS">Pre Kindergarden</option><option value="KG">KG</option><option value="1">First</option><option value="2">Second</option><option value="3">Third</option><option value="4">Fourth</option><option value="5">Fifth</option><option value="6">Sixth</option><option value="7">Seventh</option><option value="8">Eighth</option><option value="9">Nineth</option><option value="10">Tenth</option><option value="11">Eleventh</option><option value="12">Twelth</option></select></td><td>DOB: (dd/mm/yyyy)<br /> <input type="text" name=" dob " /></td></tr>
<tr>
<td>First name:<br /> <input type="text" name=" fname " /></td>
<td width="300">Last name:<br /> <input type="text" name=" lname " /></td><td width="100">Phone:<br /> <input type="tel" name=" phone " /></td></tr>
<tr><td>Parent's First Name:<br /> <input type="text" name=" parentFname " /></td><td width="300">Parent's Last Name:<br /><input type="text" name=" parentLname " /></td><td colspan="2">Address:<br /><input name=" address " type="text" size="50" maxlength="50" /></td></tr>
<tr><td>City:<br /><input type="text" name=" city " /></td><td width="300">State:<br /><input type="text" name=" state " /></td><td width="100">Zip Code:<br /><input type="text" name=" zip " /></td></tr>
<tr>
<td>e-mail Address:
<input type="text" name=" email " size="50" /></td><td>Which location will you test at?<br /><input type="checkbox" name=" location [ " value="tawheed" />Masjid Tawheed of Detroit<br /><input type="checkbox" name=" location [ " value="canton" />Canton Masjid</td><td><p style="color:red;">You must complete the partner section below before submitting your registration.</p></tr>
</table></fieldset>

<fieldset><legend>Your Partner's Information</legend>
<table width="90%" align="center" cellpadding="5" cellspacing="10" style="background-color:#FFE4C4;">
<tr><td colspan="3"><p style="color:red; font-size:14px;">***Only ONE of you need to fill out the registration form. PLEASE make sure to inform your partner BEFORE you complete the form to prevent duplicate registrations! A partner is REQUIRED this year due to the great disadvantage you will have without a partner. Jazak Allah Khair for your cooperation!***</p></td></tr>
<tr><td>Partner's First Name: <input type=; font-weight: bold;"text" name=" pFname " /></td><td>Partner's Last Name: <input type="text" name=" pLname " /></td><td>Partner's Last Grade Completed: <select name=" pGrade "><option value="PS">Pre School</option><option value="KG">Kindergarden</option><option value="1">First</option><option value="2">Second</option><option value="3">Third</option><option value="4">Fourth</option><option value="5">Fifth</option><option value="6">Sixth</option><option value="7">Seventh</option><option value="8">Eighth</option><option value="9">Nineth</option><option value="10">Tenth</option><option value="11">Eleventh</option><option value="12">Twelfth</option></select></td></tr>
<td>Partner's DOB: <input type="text" name=" pDOB " /></td>
<td>Partner's e-mail Address:
<input type="email" name=" pEmail " /></td><td>Partner's Phone: <input type="tel" name=" pPhone " /></td></tr>
<tr><td>Partner's Parent First Name: <input type="text" name=" pParentFname " /></td><td>Partner's Parent Last Name: <input type="text" name=" pParentLname " /></td><td>Partner's Address: <input type="text" name=" pAddress " /></td></tr>
<tr><td>Partner's City: <input type="text" name=" pCity " /></td><td>Partner's State: <input type="text" name=" pState " /></td><td>Partner's Zip Code: <input type="text" name=" pZip " /></td></tr>
<tr><td style="text-align:center" colspan="3"><input style="width:150px; text-align:center; font-size:24px; font-weight:bold;" type="submit" name=" submit " value="Register" /></td></tr>
</table></fieldset>
</form>

---------------------------PLEASE HELP-------------------------------

There are 3 replies in this thread, which are not being displayed.
Browsing this forum thread replies is available only to premium subscribers.


Go to the premium subscriptions page to learn how to become a premium subscriber and have full access to this forum.