Jump to content

SQL Joining in PHP


Recommended Posts

Can any one solve this ?


In my site i have three tables to store the login information of adminstrators and users


1. loginusers -> id, username, password, role, user_id, admin_id

2. user_details -> user_id, user_name, user_email and user_image

3. admin_details ->admin_id, admin_email and admin_image


The term role in loginusers table specifies weather login user is admin or user.


In my user_registration.php i have newuser form in which two field sets present , one with account information and another with personal information

when user clicks on submit button both the information is storing in tables loginuser and user_details


But the problem is with user_id in loginusers table. It should have the value to user_id in user_details How to perform this ?


Here is the code


PHP code in user_registration.php


$editFormAction = $_SERVER['PHP_SELF'];

if (isset($_SERVER['QUERY_STRING'])) {

$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);



if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "newuser")) {

$insertSQL = sprintf("INSERT INTO loginusers (username, password, `role`) VALUES (%s, %s, %s)",

GetSQLValueString($_POST['username'], "text"),

GetSQLValueString($_POST['password'], "text"),

GetSQLValueString($_POST['role'], "text"));


$insertSQL = sprintf("INSERT INTO user_details (full_name, user_email, user_phone, user_address) VALUES (%s, %s, %s, %s)",

GetSQLValueString($_POST['full_name'], "text"),

GetSQLValueString($_POST['user_email'], "text"),

GetSQLValueString($_POST['user_phone'], "text"),

GetSQLValueString($_POST['user_address'], "text"));


mysql_select_db($database_mysql_connect, $mysql_connect);

$Result1 = mysql_query($insertSQL, $mysql_connect) or die(mysql_error());


$insertGoTo = "newusersuccess.php";

if (isset($_SERVER['QUERY_STRING'])) {

$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";

$insertGoTo .= $_SERVER['QUERY_STRING'];


header(sprintf("Location: %s", $insertGoTo));




HTML code in user_registration.php :

Account Info:

User Name

User Name


Password4~9 characters4~9 CharactersStrength is Low

Confirm Password

Enter PasswordPassword don't match



Personal Information:




EmailEnter valid email


Phone NumberIntegers Only


Enter Address





Edited by khan.sikki
Link to comment
Share on other sites

You can use mysql_insert_id to retrieve the last ID generated by an insert operation (provided that the id field auto-increments). You could then use this for your second insert.


It is possible to use joins in conjunction with updates and deletes, but I've never used join with insert before. I don't think it's possible, although I stand to be corrected on that. (Anyone?)

Link to comment
Share on other sites

after inserting into loginusers but before inserting into next table add the following


$get_last_id = mysql_query("SELECT id FROM loginusers ORDER BY id DESC LIMIT 1");

while ($last_id = mysql_fetch_array( $get_last_id)) {

$the_last_id = $last_id['id'];



if you add a extra feild to user_details eg "loginusers_id" you can use this to link the records, however there could be a mix up if two people submit the form at the exact time.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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