Jump to content

Submit Form Data to Multiple MySQL Tables


LHClaassen

Recommended Posts

Hi All,

 

I am trying to creat a form that will submit data to multiple tables in the same database. I am using the code below, but this seems to only be writing data to the last table in the query.

 

<?php
$con = mysql_connect("localhost","root","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("databasename", $con);



$sql="INSERT INTO staff_churn
(username
,reference
,champ
,churn_type
,effective_date
,department
,exit_department
,position
,contract_type)

VALUES
('$_POST[username]'
,'$_POST[reference]'
,'$_POST[fname]'
,'$_POST[churn_type]'
,'$_POST[effective_date]'
,'$_POST[department]'
,'$_POST[exit_department]'
,'$_POST[position]'
,'$_POST[contract_type]'
)";




$sql="INSERT INTO staff_churn_access
(reference
,access_card
,champ_portal
,desktop
,email
,hard_phone
,lan
,laptop
,soft_phone
,g_drive
,g_folders
,h_drive
,h_folders
,distribution
,clarify_access)

VALUES
('$_POST[reference]'
,'$_POST[access_card]'
,'$_POST[champ_portal]'
,'$_POST[desktop]'
,'$_POST[email]'
,'$_POST[hard_phone]'
,'$_POST[lan]'
,'$_POST[laptop]'
,'$_POST[soft_phone]'
,'$_POST[g_drive]'
,'$_POST[g_drive] $_POST[drive_a] $_POST[drive_c] $_POST[drive_e] $_POST[drive_g]'
,'$_POST[h_drive]'
,'$_POST[h_drive] $_POST[drive_b] $_POST[drive_d] $_POST[drive_f] $_POST[drive_h]'
,'$_POST[list_a] $_POST[list_b] $_POST[list_c] $_POST[list_d] $_POST[list_e] $_POST[list_f] $_POST[list_g] $_POST[list_h]'
,'$_POST[clarify_access]'
)";





if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "<b><font color='white' face='segoe' size='2'>1 record added</b></font>";
include "/submit/redirect_churn.html";

mysql_close($con)
?> 

 

:wtf:

 

Can anyone help here?

Link to comment
Share on other sites

Thanks for this... I got it to work. I now have a new problem though where the last part doesn't seem to be working. When the form data is submitted it just displays a blank white page instead of displaying the echo and then running the redirect_churn.html page.

 

echo "<b><font color='white' face='segoe' size='2'>1 record added</b></font>";
include "submit/redirect_churn.html";

Link to comment
Share on other sites

Hi there,

 

I tried updating my query to

 

if(!mysql_query($sql, $con)
{
die("Error: ".mysql_error();
} 
else 
{
echo "<b><font color='white' face='segoe' size='2'>1 record added</b></font>";
include "/submit/redirect_churn.html";
}

 

But still I get the white screen when submitting the query. The redirect works perfectly when I am only submitting to one database so I know that the include "/submit/redirect_churn.html"; part is working correctly.

 

Here is the full code:

 

<?php
$con = mysql_connect("localhost","root","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("databasename", $con);



$sql="INSERT INTO staff_churn
(username
,reference
,champ
,churn_type
,effective_date
,department
,exit_department
,position
,contract_type)

VALUES
('$_POST[username]'
,'$_POST[reference]'
,'$_POST[fname]'
,'$_POST[churn_type]'
,'$_POST[effective_date]'
,'$_POST[department]'
,'$_POST[exit_department]'
,'$_POST[position]'
,'$_POST[contract_type]'
)";


if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "<b><font color='white' face='segoe' size='2'>1 record added</b></font>";


$sql="INSERT INTO staff_churn_access
(reference
,access_card
,champ_portal
,desktop
,email
,hard_phone
,lan
,laptop
,soft_phone
,g_drive
,g_folders
,h_drive
,h_folders
,distribution
,clarify_access)

VALUES
('$_POST[reference]'
,'$_POST[access_card]'
,'$_POST[champ_portal]'
,'$_POST[desktop]'
,'$_POST[email]'
,'$_POST[hard_phone]'
,'$_POST[lan]'
,'$_POST[laptop]'
,'$_POST[soft_phone]'
,'$_POST[g_drive]'
,'$_POST[g_drive], $_POST[drive_a], $_POST[drive_c], $_POST[drive_e], $_POST[drive_g]'
,'$_POST[h_drive]'
,'$_POST[h_drive], $_POST[drive_b], $_POST[drive_d], $_POST[drive_f], $_POST[drive_h]'
,'$_POST[list_a], $_POST[list_b], $_POST[list_c], $_POST[list_d], $_POST[list_e], $_POST[list_f], $_POST[list_g], $_POST[list_h]'
,'$_POST[clarify_access]'
)";





if (!mysql_query($sql,$con))
{
  die('Error: ' . mysql_error());
}
  else
{
echo "<b><font color='white' face='segoe' size='2'>1 record added</b></font>";
include "/submit/redirect_churn.html";
}
mysql_close($con)
?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.