LHClaassen Posted June 15, 2012 Share Posted June 15, 2012 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) ?> Can anyone help here? Link to comment https://forums.phpfreaks.com/topic/264239-submit-form-data-to-multiple-mysql-tables/ Share on other sites More sharing options...
thara Posted June 15, 2012 Share Posted June 15, 2012 You need to execute 1st query too using mysql_query() function. You have done it for your 2nd query only. Link to comment https://forums.phpfreaks.com/topic/264239-submit-form-data-to-multiple-mysql-tables/#findComment-1354152 Share on other sites More sharing options...
SalientAnimal Posted June 18, 2012 Share Posted June 18, 2012 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 https://forums.phpfreaks.com/topic/264239-submit-form-data-to-multiple-mysql-tables/#findComment-1354791 Share on other sites More sharing options...
ShoeLace1291 Posted June 18, 2012 Share Posted June 18, 2012 Try this: 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"; } Link to comment https://forums.phpfreaks.com/topic/264239-submit-form-data-to-multiple-mysql-tables/#findComment-1354793 Share on other sites More sharing options...
SalientAnimal Posted June 18, 2012 Share Posted June 18, 2012 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 https://forums.phpfreaks.com/topic/264239-submit-form-data-to-multiple-mysql-tables/#findComment-1354809 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.