khan.sikki Posted December 23, 2008 Report Share Posted December 23, 2008 (edited) 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 Password Password4~9 characters4~9 CharactersStrength is Low Confirm Password Enter PasswordPassword don't match Personal Information: Name Name Email EmailEnter valid email Phone Phone NumberIntegers Only Address Enter Address Image Edited December 23, 2008 by khan.sikki Quote Link to comment Share on other sites More sharing options...
monkeysaurus Posted December 23, 2008 Report Share Posted December 23, 2008 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?) Quote Link to comment Share on other sites More sharing options...
sjhwebdesign Posted December 31, 2008 Report Share Posted December 31, 2008 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. Quote Link to comment Share on other sites More sharing options...
khan.sikki Posted January 2, 2009 Author Report Share Posted January 2, 2009 Recently i came across foreign keys in MYSQL can't i use it for this Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.