Jump to content

Running 4 queries... in a row..


simplyrichard

Recommended Posts

I think this is a simple answer but I cant think of how to put in to words what I am trying to do well enough to google it. Your help would be great!

 

I have the following queries that must be ran upon a user signing up for access to my site. First I check to see if the username is already in use, if that no then three queries run:

 

//Everything is A-OK, Lets insert the information in the campus_users table.
mysql_query("INSERT INTO campus_users (username, password) VALUES ('$username','$pass') ") 
or die(mysql_error()); 

$query  = "SELECT user_id FROM campus_users WHERE username ='$username' AND password='$pass'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

$user_id = $row['user_id'];

mysql_query("INSERT INTO campus_personal (First_Name, Last_Name, Title, suffix, PersonalEmail, affiliation, user_id) VALUES ('$first_name','$last_name','$title','$suffix','$email','$affiliation','$user_id') ") 
or die(mysql_error()); 

mysql_query("INSERT INTO campus_addresses (street_address, aline2, city, state, zip, country, phone, user_id) VALUES ('$street', '$aline2','$city', '$state', '$zip','$country','$phone','$user_id') ") 
or die(mysql_error()); 

 

However, I want all the queries to fail if one of them do fail. So, if inserting into the users table (the first query fails) I dont what the other two to insert. Also, you will see that after inserting into the user table I do a query to find out what the user_id number is to use in my last two queries.. (Is there a better way to do this?)

 

Thanks for your help.

 

Richard

Link to comment
https://forums.phpfreaks.com/topic/125975-running-4-queries-in-a-row/
Share on other sites

After the first insert, you can get the new id with

 

$user_id = mysql_insert_id();

 

instead of

$query  = "SELECT user_id FROM campus_users WHERE username ='$username' AND password='$pass'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

$user_id = $row['user_id'];

 

Is there any reason to have the other data split across the 2 tables instead of one?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.