Jump to content

How can I write a php mysql page the will post to more then one table


Tripic

Recommended Posts

Ok this is my lates attempt but now its not posting anything at all prior to this if i tried to run one then use mysql close(); it would only run the first of the 2 i cant figure out where i am going wrong Please help

 

if  ($user_email == $user_emailv){
if ($user_pass == $user_passv){
$live;
mysql_select_db ($database_live);
$sql ="INSERT INTO users (user_id, user_name, user_pass, user_type, Member_Since)VALUES ('NULL','".$user_username."','".$user_pass."','".$user_type."','NULL')";
if( isset($user_first_name) )
{
  #  get the user id  
  $user_id = mysql_insert_id( $conn );
$sql .="INSERT INTO user_info (user_infoid, user_first_name, user_last_name, user_street_address, user_state, user_zip, user_phone, user_fax, user_email_address, user_city, user_apartment_number, user_id)VALUES ('NULL','".$user_first_name."','".$user_last_name."','".$user_street."','".$user_state."','".$user_zip."','".$user_phone."','".$user_fax."','".$user_email."','".$user_city."','".$user_apt."','".$user_id."')";
  $result = mysql_query( $sql,$conn );
}
mysql_free_result( $result );	

mysql_close();

echo "Did it work";
}
else{
echo "Your passwords do not match";
}
}
else{
echo "Your email adreeses do not match";
}
?>

Link to comment
Share on other sites

The problem here is that you are overwriting your $sql variable. I only see you running the mysql_query() function once when you should run it after both.

 

$sql ="INSERT INTO users (user_id, user_name, user_pass, user_type, Member_Since)VALUES ('NULL','".$user_username."','".$user_pass."','".$user_type."','NULL')";

 

should be followed by:

 

$result = mysql_query($sql);

 

--

 

But beware, you are then going to overwrite the $result variable when you run the second query unless you preserve this one by giving them both dif names ($result1, $result2) or storing them in an array with

 

$result[] = mysql_query($sql);

 

--

 

By the way, instead of encapsulating large blocks of code in if else statements due to simple arguments, try to use return statements like so:

 

if($var1 != $var2){
echo 'Var1 does not match Var2';
return;
}

if($var3 != $var4){
echo 'Var3 does not match Var4';
return;
}

// anything AFTER these two checks will be safe as these two statements must have validated in order for the script to reach this line

 

That's the beauty of effective returns. Now you don't have tons of if and else statements encapsulating large blocks of code.

 

Hope that helps.

Link to comment
Share on other sites

Ok I tried that but now its not adding anything to the database at all I know im missing something really stupid here but i cant figure out whati am missing i have atached 3 files live.php is the conection file i know theres no pass its not ready for publication im working on it on a linux server isetup on my network createemployee.php is the actual form that the data comes from and createemp.php is the file that should be doing what i want it to but i cant seem to get it to work. Ohh and thanks for the return; ideah i should have known that but for some reason compleley forgot about it guess thats what happens after 5 years of no real programing

 

[attachment deleted by admin]

Link to comment
Share on other sites

Ok here goes I cleaned up my code some what still not the best in the world but easier to understand i think the first table post fine but nothing post to the second table at all the first table is users the second is user_info any ideahs or sugestions are greatley apreciated

 

<?php


$user_email = $_POST['user_email']; 
$user_emailv = $_POST['user_emailv'];
$user_pass = $_POST['user_passv'];
$user_passv = $_POST['user_pass'];
$user_first_name = $_POST['user_first_name'];
$user_last_name = $_POST['user_last_name'];
$user_street = $_POST['user_street_address'];
$user_apt = $_POST['user_apartment_number'];
$user_city = $_POST['user_city'];
$user_state = $_POST['user_state'];
$user_zip = $_POST['user_zip'];
$user_phone = $_POST['user_phone'];
$user_fax = $_POST['user_fax'];
$user_username = $_POST['user_esername'];
$user_type = $_POST['type'];
?>
<?php
if($user_email != $user_emailv){ 
	echo 'Verification Email  does not match Email Please Use your Browsers Back button to fix this ';
	return;
}
if($user_pass != $user_passv){
	echo 'Your passwords Do not match Please Use your Browsers Back button to fix this ';
	return;
}
?>


<?php require_once('Connections/live.php'); 
$live;
mysql_select_db ($database_live);
// Insert Data into User Table of database
mysql_query("INSERT INTO users (user_id, user_name, user_pass, user_type, Member_Since)VALUES ('NULL','".$user_username."','".$user_pass."','".$user_type."','NULL');");
// End of User Table Insert 
$id = mysql_insert_id();//Get the id of the last record inserted to the database 
// Insert Data into User Information Table of database 
mysql_query("INSERT INTO user_info (user_infoid, user_first_name, user_last_name, user_street_address, user_state, user_zip, user_phone, user_fax, user_email_address, user_city, user_apartment_number, user_id)VALUES ('NULL','".$user_first_name."','".$user_last_name."','".$user_street."','".$user_state."','".$user_zip."','".$user_phone."','".$user_fax."','".$user_email."','".$user_city."','".$user_apt."','".$id."');");
// End of User Information Table Insert
mysql_close();
header("location: ./index.php");
?>

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.