Hello,

I am fairly new to PHP and MySQL so I apologize if I am just being lazy and/or stupid on this.

I searched previous threads and found this one Insert data into two tables using php and mysql It helped me tremendously but only to a point. Below is the code I am using and then the message I receive when I try the form out.

<?php

$con = mysql_connect("localhost", "XXXXXX","XXXXXX") or die('Sorry, could not connect to database server');
mysql_select_db("xxxxxxxxx", $con) or die('Sorry, could not connect to database');




if (!$con)
{
   echo "<h2>Sorry, we cannot process your request at this time, please try again later</h2>\n";
   echo "<a href=\"index.php?content=registration\">Please try again</a><br>\n";
   exit;
}


$firmname = $_POST['firmname'];
$firmother = $_POST['firmother'];

$numattfull = $_POST['numattfull'];
$numattpartial = $_POST['numattpartial'];
$totfees = $_POST['totfees'];
$primecontact = $_POST['primecontact'];
$pcphone = $_POST['pcphone'];
$pcemail = $_POST['pcemail'];

$att1fp = $_POST['att1fp'];
$att1name = $_POST['att1name'];
$att1title = $_POST['att1title'];
$att1phone = $_POST['att1phone'];
$att1email = $_POST['att1email'];
$att1diet = htmlspecialchars($_POST['att1diet']);

$att2fp = $_POST['att2fp'];
$att2name = $_POST['att2name'];
$att2title = $_POST['att2title'];
$att2phone = $_POST['att2phone'];
$att2email = $_POST['att2email'];
$att2diet = htmlspecialchars($_POST['att2diet']);

$att3fp = $_POST['att3fp'];
$att3name = $_POST['att3name'];
$att3title = $_POST['att3title'];
$att3phone = $_POST['att3phone'];
$att3email = $_POST['att3email'];
$att3diet = htmlspecialchars($_POST['att3diet']);

$att4fp = $_POST['att4fp'];
$att4name = $_POST['att4name'];
$att4title = $_POST['att4title'];
$att4phone = $_POST['att4phone'];
$att4email = $_POST['att4email'];
$att4diet = htmlspecialchars($_POST['att4diet']);

$att5fp = $_POST['att5fp'];
$att5name = $_POST['att5name'];
$att5title = $_POST['att5title'];
$att5phone = $_POST['att5phone'];
$att5email = $_POST['att5email'];
$att5diet = htmlspecialchars($_POST['att5diet']);

$att6fp = $_POST['att6fp'];
$att6name = $_POST['att6name'];
$att6title = $_POST['att6title'];
$att6phone = $_POST['att6phone'];
$att6email = $_POST['att6email'];
$att6diet = htmlspecialchars($_POST['att6diet']);


$baduser = 0;

if(!get_magic_quotes_gpc())
{
   $att1diet = addslashes($att1diet);
   $att2diet = addslashes($att2diet);
   $att3diet = addslashes($att3diet);
   $att4diet = addslashes($att4diet);
   $att5diet = addslashes($att5diet);
   $att6diet = addslashes($att6diet);
}

// Check if firmname was entered
if (trim($firmname) == '')
{
   echo "<h2>Sorry, you must select a Firm Name.</h2><br>\n";
   echo "<a href=\"index.php?content=registration\">Please try again</a><br>\n";
   $baduser = 1;
   exit;
}

//Check if Total Fees was  entered
if (trim($totfees) == '')
{
   echo "<h2>Sorry, you must enter the total amount of fees</h2><br>\n";
   echo "<a href=\"index.php?content=registration\">Please try again</a><br>\n";
   $baduser = 1;
   exit;
}

//Check if Primary Contact was  entered
if (trim($primecontact) == '')
{
   echo "<h2>Sorry, you must enter a primary contact</h2><br>\n";
   echo "<a href=\"index.php?content=registration\">Please try again</a><br>\n";
   $baduser = 1;
   exit;
}


//Check if Primary Contact Phone was  entered
if (trim($pcphone) == '')
{
   echo "<h2>Sorry, you must enter a phone number for the primary contact</h2><br>\n";
   echo "<a href=\"index.php?content=registration\">Please try again</a><br>\n";
   $baduser = 1;
   exit;
}


//Check if Primary Contact Email was  entered
if (trim($pcemail) == '')
{
   echo "<h2>Sorry, you must enter an email address for the primary contact</h2><br>\n";
   echo "<a href=\"index.php?content=registration\">Please try again</a><br>\n";
   $baduser = 1;
   exit;
}



//Check if Attendee 1 Registration Type was selected

if (trim($att1fp) == '')
{
   echo "<h2>Sorry, you must select a registration type for Attendee 1</h2><br>\n";
    echo "<a href=\"index.php?content=registration\">Please try again</a><br>\n";
   $baduser = 1;
   exit;
}





//Check if Attendee 1 name was  entered
if (trim($att1name) == '')
{
   echo "<h2>Sorry, you must enter a name for Attendee 1</h2><br>\n";
   echo "<a href=\"index.php?content=registration\">Please try again</a><br>\n";
   $baduser = 1;
   exit;
}

//Check if Attendee 1 title was  entered
if (trim($att1title) == '')
{
   echo "<h2>Sorry, you must enter a title for Attendee 1</h2><br>\n";
   echo "<a href=\"index.php?content=registration\">Please try again</a><br>\n";
   $baduser = 1;
   exit;
}

//Check if Attendee 1 phone number was  entered
if (trim($att1phone) == '')
{
   echo "<h2>Sorry, you must enter a phone number for Attendee 1</h2><br>\n";
   echo "<a href=\"index.php?content=registration\">Please try again</a><br>\n";
   $baduser = 1;
   exit;
}

//Check if Attendee 1 email address was  entered
if (trim($att1email) == '')
{
   echo "<h2>Sorry, you must enter an email address for Attendee 1</h2><br>\n";
   echo "<a href=\"index.php?content=registration\">Please try again</a><br>\n";
   $baduser = 1;
   exit;
}


{
   //Everything passed, enter information in database
   
 
  $add_to_firm_query="INSERT INTO firm (firmname,firmother) VALUES ($firmname, $firmother)";
  
  mysql_query($add_to_firm_query);
  
  $add_to_registration_query="INSERT INTO registration (regid,firmid,numattfull,numattpartial,totfees,primecontact,pcphone,pcemail,att1fp,att1name,att1title,att1phone,att1email,att1diet,att2fp,att2name,att2title,att2phone,att2email,att2diet,att3fp,att3name,att3title,att3phone,att3email,att3diet,att4fp,att4name,att4title,att4phone,att4email,att4diet,att5fp,att5name,att5title,att5phone,att5email,att5diet,att6fp,att6name,att6title,att6phone,att6email,att6diet) VALUES ('regid', LAST_INSERT_ID(), '$numattfull', '$numattpartial', '$totfees', '$primecontact', '$pcphone', '$pcemail', '$att1fp', '$att1name', '$att1title', '$att1phone', '$att1email', '$att1diet', '$att2fp', '$att2name', '$att2title', '$att2phone', '$att2email', '$att2diet', '$att3fp', '$att3name', '$att3title', '$att3phone', '$att3email', '$att3diet', '$att4fp', '$att4name', '$att4title', '$att4phone', '$att4email', '$att4diet', '$att5fp', '$att5name', '$att5title', '$att5phone', '$att5email', '$att5diet', '$att6fp', '$att6name', '$att6title', '$att6phone', '$att6email', '$att6diet')";
  
  mysql_query($add_to_registration_query);
  
  
   
   
   if (!mysql_query($sql,$con))
   {
      echo "<h2>Your information has been successfully received. Thank you!</h2><br /><br />\n";
      echo "<form method=\"post\"><input type=\"button\" value=\"Close Window\"onclick=\"window.close()\"></form>\n";
      exit;
   } else
   {
      echo "<h2>Sorry, there was a problem processing your information</h2><br />\n";
      echo "<form method=\"post\"><input type=\"button\" value=\"Close Window\"onclick=\"window.close()\"></form>\n";
      exit;
   }
}
?>

When I try the form out, I receive the following message:

Notice: Undefined variable: sql in C:...............\addregistration.inc.php on line 201

Your information has been successfully received. Thank you!
Also, no data is inserted into either table.

Line 201: if (!mysql_query($sql,$con))


I hope I'm making sense. Can someone help me?

Thank you.

make these your query statements:

mysql_query("INSERT INTO firm VALUES('$firmname', '$firmother')");

mysql_query("INSERT INTO registration VALUES('regid', LAST_INSERT_ID(), '$numattfull', '$numattpartial', '$totfees', '$primecontact', '$pcphone', '$pcemail', '$att1fp', '$att1name', '$att1title', '$att1phone', '$att1email', '$att1diet', '$att2fp', '$att2name', '$att2title', '$att2phone', '$att2email', '$att2diet', '$att3fp', '$att3name', '$att3title', '$att3phone', '$att3email', '$att3diet', '$att4fp', '$att4name', '$att4title', '$att4phone', '$att4email', '$att4diet', '$att5fp', '$att5name', '$att5title', '$att5phone', '$att5email', '$att5diet', '$att6fp', '$att6name', '$att6title', '$att6phone', '$att6email', '$att6diet')");

and your unidentified variable is $sql, which is line 197 if (!mysql_query($sql,$con)) is because $sql doesn't exist.

also remove your { & } from lines 182/208

Thank you so much for your help!

I removed the unidentified variable and the brackets as you instructed. However, now I am getting this message.

Notice: Use of undefined constant mysql_query - assumed 'mysql_query' in C:\.............addregistration.inc.php on line 197

Sorry, there was a problem processing your information

Here's the portion of the code I edited.

//Everything passed, enter information in database
   
 
 mysql_query("INSERT INTO firm VALUES('$firmname', '$firmother')");
 
mysql_query("INSERT INTO registration VALUES('regid', LAST_INSERT_ID(), '$numattfull', '$numattpartial', '$totfees', '$primecontact', '$pcphone', '$pcemail', '$att1fp', '$att1name', '$att1title', '$att1phone', '$att1email', '$att1diet', '$att2fp', '$att2name', '$att2title', '$att2phone', '$att2email', '$att2diet', '$att3fp', '$att3name', '$att3title', '$att3phone', '$att3email', '$att3diet', '$att4fp', '$att4name', '$att4title', '$att4phone', '$att4email', '$att4diet', '$att5fp', '$att5name', '$att5title', '$att5phone', '$att5email', '$att5diet', '$att6fp', '$att6name', '$att6title', '$att6phone', '$att6email', '$att6diet')");


$result = mysql_query
   
   
   if (!mysql_query)
   {
      echo "<h2>Your information has been successfully received. Thank you!</h2><br /><br />\n";
      echo "<form method=\"post\"><input type=\"button\" value=\"Close Window\"onclick=\"window.close()\"></form>\n";
      exit;
   } else
   {
      echo "<h2>Sorry, there was a problem processing your information</h2><br />\n";
      echo "<form method=\"post\"><input type=\"button\" value=\"Close Window\"onclick=\"window.close()\"></form>\n";
      exit;
   }

?>

I can't say "thank you" enough.

//Everything passed, enter information in database
   
$result=mysql_query("INSERT INTO firm VALUES('$firmname', '$firmother')");
 
$result2=mysql_query("INSERT INTO registration VALUES('regid', LAST_INSERT_ID(), '$numattfull', '$numattpartial', '$totfees', '$primecontact', '$pcphone', '$pcemail', '$att1fp', '$att1name', '$att1title', '$att1phone', '$att1email', '$att1diet', '$att2fp', '$att2name', '$att2title', '$att2phone', '$att2email', '$att2diet', '$att3fp', '$att3name', '$att3title', '$att3phone', '$att3email', '$att3diet', '$att4fp', '$att4name', '$att4title', '$att4phone', '$att4email', '$att4diet', '$att5fp', '$att5name', '$att5title', '$att5phone', '$att5email', '$att5diet', '$att6fp', '$att6name', '$att6title', '$att6phone', '$att6email', '$att6diet')");
   
   if (!$result || !$result2)
   {
      echo "<h2>Sorry, there was a problem processing your information</h2><br />\n";
      echo "<form method=\"post\"><input type=\"button\" value=\"Close Window\"onclick=\"window.close()\"></form>\n";
      exit;
   } else
   {
      echo "<h2>Your information has been successfully received. Thank you!</h2><br /><br />\n";
      echo "<form method=\"post\"><input type=\"button\" value=\"Close Window\"onclick=\"window.close()\"></form>\n";
      exit;
   }

?>

Thank you again! I'm making progress. At least I'm not getting any error messages but the data is not writing to the database. I've checked and double checked my settings such as the connection to the database, etc.

I perform a test from the form and get the message I have set up for when there is a processing error. "Sorry, there was a problem processing your information".

Any tips on where to begin working this wrinkle out?

$message = 'Invalid query: ' . mysql_error() . "\n";
die($message);
put that after your if statement ie. if(!$result||!$result2)

and it should give you the reason it's not being put into mysql

Thank you so much! I always forget about

$message = 'Invalid query: ' . mysql_error() . "\n";
die($message);

added that and found my error pretty quickly. I neglected to match the number of fields to the data input in my firm table. DOH!

Thank you again for your help AND your patience with me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.