Jump to content

Problems Inserting to multiple tables


Tripic

Recommended Posts

Ok So heres what i am trying to do. I am creating a system that will have four diferent user groups each usergroup will have there own sign up page. Now the USer login information for each group will all go under one table users but the rest of the information for each group will go to a different table for each group  for example

 

  • username
  • userid
  • userpassword
  • usertype

will all go into user table while userfirstname etc goes into table user_info for some reason i cant seem to get my insert statment to write to both at the same time can anyone tell me what im doing wrong

 

<?php require_once('Connections/live.php'); ?>
<?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){
if ($user_pass == $user_passv){
$live;
mysql_select_db ($database_live);
$query="INSERT INTO users (user_id, user_name, user_pass, user_type, Member_Since)VALUES ('NULL','".$user_username."','".$user_pass."','".$user_type."','NULL')";
$queryb="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)VALUES ('NULL','".$user_first_name."','".$user_last_name."','".$user_street."','".$user_state."','".$user_zip."','".$user_phone."','".$user_fax."','".$user_email."','".$user_city."','".$user_apt."')";
mysql_query($query) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_query($queryb) or trigger_error(mysql_error(),E_USER_ERROR);
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

A few things -

 

1 - You're putting $_POST values directly into your query string without any escaping, which poses a big security risk. Look up the php function mysql_real_escape_string and use it to escape your inputs.

 

2 - It might be easier to work out what's going on if you posted the fully assembled query strings as well, i.e. the contents of $query and $queryb, just before they get used in mysql_query()

 

3 - It would be good if you posted the results of mysql_error() if there are any.

Link to comment
Share on other sites

Tripic:

 

straylight has some great tips for you.

Additionally, I'd ask:

 

How is the user and userinfo table related?  Do you need to do the user query first and then get the AUTO_INCREMENT id generated, so that you can set one of the columns in userinfo to be the same?  If so you need to do the first query then call mysql_insert_id() to get the newly generated user_id.

 

Also, you might find that php's string interpolation could make writing your queries a lot simpler.

 

This works:

 $query = "INSERT INTO TABLENAME ('foo_id', 'fooname', ...) VALUES (NULL, '$variable', '{$arrayvar['somekey']}' ... "; 

 

 

Link to comment
Share on other sites

Ok the $query is defined in the first code i put up i think but here it is anyways i am using a new one now but if you think this was better i have it backed up my new one is below it doesnt work

 

$query="INSERT INTO users (user_id, user_name, user_pass, user_type, Member_Since)VALUES ('NULL','".$user_username."','".$user_pass."','".$user_type."','NULL')";	$queryb="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)VALUES ('NULL','".$user_first_name."','".$user_last_name."','".$user_street."','".$user_state."','".$user_zip."','".$user_phone."','".$user_fax."','".$user_email."','".$user_city."','".$user_apt."')";	

 

 

yes i need the id from users to post in user info and i rewrote the code but it is still only posting to users  and not user_info  I will post my new code and upload the file as well i cant figure out where im going wrong

 

<?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");
?>

 

[attachment deleted by admin]

Link to comment
Share on other sites

There's no way to know without you getting the value of mysql_error().  I suspect there's some issue with either the values or the column list, for example, is the key of user_info really named user_infoid?

 

At any rate, when you do a query you should get the return value.

 

 

 


$retval = mysql_query(....);

if (!$retval) {
  // query failed.
  // This is for debugging, should handle errors better than this, but for now, it's ok:
  echo 'Error: ' .  mysql_error();
  die(' insert failed!');
}

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.