Forum Moderators: coopster
//code below
<?php
// include the database configuration and
// open connection to database
include ('config.php');
include ('opendb.php');
// check if the form is submitted
if(isset($_POST['Submit']))
{ //validate input with PHP classes validator
include ('habProcessor.php');
// get the input from $_POST variable
// trim all input to remove extra spaces
$fname = trim($_POST['fname']);
$lname = trim($_POST['lname']);
$email = trim($_POST['email']);
$subject = trim($_POST['subject']);
$topic = trim($_POST['topic']);
$grade = trim($_POST['grade']);
$standards = trim($_POST['standards']);
$description = trim($_POST['description']);
$lessonplan = trim($_POST['lessonplan']);
$phone = trim($_POST['phone']);
$contact_me = trim($_POST['contact_me']);
reset ($_POST);
while (list ($key, $val) = each ($_POST)) {
if ($val == "") $val = "NULL";
$key = (get_magic_quotes_gpc())? $val : addslashes($val);
if ($val == "NULL")
$_POST[$key] = NULL;
else
$_POST[$key] = $val;
}
$query = "INSERT INTO `tbllessonplans` (`Grade`, `Subject`, `Description`, `Topic`, `Standards`, `LessonPlan`)
VALUES
('".$_POST[grade]."', '".$_POST[subject]."', '".$_POST[description]."', '".$_POST[topic]."', '".$_POST[standards]."', '".$_POST[lessonplan]."')";
//echo $query;
$result = mysql_query($query) or die("Invalid query: " . mysql_error() . "<br><br>". $query);
//get LPID from tbllessonplans to insert into tbllpcontacts for referential integrity
$insertid = mysql_insert_id();
$query1 = "INSERT INTO `tbllpcontact`
(`LPID`, `Firstname`, `Lastname`, `Phone`, `Email`, `Contact_me`) VALUES
('".$insertid."',
'".$_POST[fname]."',
'".$_POST[lname]."', '".$_POST[phone]."', '".$_POST[email]."', '".$_POST[contact_me]."')";
/*
$query1 = 'INSERT INTO `tbllpcontact` (`LPID`, `Firstname`, `Lastname`, `Phone`, `Email`, `Contact_me`)
VALUES
($insertid, $_POST['fname'],
$_POST['lname'], $_POST['phone'],
$_POST['email'], $_POST['contact_me'])';
*/
// execute the query to insert the input to database
// if query fail the script will terminate
$result1 = mysql_query($query1) or die("Invalid query: " . mysql_error() . "<br><br>". $query1);
//redirect to thank you
header("Location: ../../thanks/thankyou.php");
// quit script
exit;
}
?>
The following code should work
$query1 = "INSERT INTO `tbllpcontact`
(`LPID`, `Firstname`, `Lastname`, `Phone`, `Email`, `Contact_me`) VALUES
('".$insertid."',
'".$_POST['fname']."',
'".$_POST['lname']."', '".$_POST['phone']."', '".$_POST['email']."', '".$_POST['contact_me']."')";
However you have some security issues:
make $_POST safe, eg. by using:
$_POST = array_map("mysql_real_escape_string", $_POST);
and I would use as Duskrider suggested:
$_POST = array_map("mysql_real_escape_string", $_POST);
$query1 = "INSERT INTO `tbllpcontact`
(`LPID`, `Firstname`, `Lastname`, `Phone`, `Email`, `Contact_me`) VALUES
('$insertid','{$_POST['fname']}',
'{$_POST['lname']}', '{$_POST['phone']}', '{$_POST['email']}', '{$_POST['contact_me']}')";
this way the code is a bit clearer.
Regards and have fun!
Michal
at first glance it all seems fine, try something simple and make your query all one line like so
$query1 = "INSERT INTO `tbllpcontact` (`LPID`, `Firstname`, `Lastname`, `Phone`, `Email`, `Contact_me`) VALUES ('".$insertid."', '".$_POST[fname]."', '".$_POST[lname]."', '".$_POST[phone]."', '".$_POST[email]."', '".$_POST[contact_me]."')";
at very least we can see if that changes the line number
require, so I don't know where this error is coming from...
Glad you got everything resolved, though. :)