grglaz Posted January 9, 2008 Share Posted January 9, 2008 hi i want to use 1 register system with 2 different mysql databases. the register system uses these files. register html, register php, about the html page there is no problem because it doesnt need any information about the database. the php pages goes like this: <?PHP //Database Information $dbhost = "localhost"; $dbname = "your database name"; $dbuser = "username"; $dbpass = "yourpass"; //Connect to database mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error()); mysql_select_db($dbname) or die(mysql_error()); $name = $_POST['name']; $email = $_POST['email']; $username = $_POST['username']; $password = md5($_POST['password']); // lets check to see if the username already exists $checkuser = mysql_query("SELECT username FROM users WHERE username='$username'"); $username_exist = mysql_num_rows($checkuser); if($username_exist > 0){ echo "I'm sorry but the username you specified has already been taken. Please pick another one."; unset($username); include 'register.html'; exit(); } // lf no errors present with the username // use a query to insert the data into the database. $query = "INSERT INTO users (name, email, username, password) VALUES('$name', '$email', '$username', '$password')"; mysql_query($query) or die(mysql_error()); mysql_close(); echo "You have successfully Registered"; // mail user their information $yoursite = ‘www.blahblah.com’; $webmaster = ‘yourname’; $youremail = ‘youremail’; $subject = "You have successfully registered at $yoursite..."; $message = "Dear $name, you are now registered at our web site. To login, simply go to our web page and enter in the following details in the login form: Username: $username Password: $password Please print this information out and store it for future reference. Thanks, $webmaster"; mail($email, $subject, $message, "From: $yoursite <$youremail>\nX-Mailer:PHP/" . phpversion()); echo "Your information has been mailed to your email address."; ?> so can anybody help me on how can i edit this php file in a way so i can use 2 different databases? also if need we can use 1 database including inside tables from both existing databases. to be more specific what i want is when a user register,the values of 'user name' can store in 2 tables with different name,and the same with password. So in this way i can use the 2 login forms from different sites. Is proper if i write in the register php the same code one after the other each time with different names of tables?or there is another way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/85215-how-to-manage-1-register-system-with-2-logins-and-2-different-mysql-databases/ Share on other sites More sharing options...
revraz Posted January 9, 2008 Share Posted January 9, 2008 You can use 2 if you want, just add new variables with the 2nd DB info just like you did the first. Quote Link to comment https://forums.phpfreaks.com/topic/85215-how-to-manage-1-register-system-with-2-logins-and-2-different-mysql-databases/#findComment-434736 Share on other sites More sharing options...
chronister Posted January 9, 2008 Share Posted January 9, 2008 //Database Information <?php function connectdb($dbname) { $dbhost = "localhost"; $dbuser = "username"; $dbpass = "yourpass"; //Connect to database mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error()); mysql_select_db($dbname) or die(mysql_error()); } connectdb('db1'); //run the query for db1 connectdb('db2'); //run the query for db2 ?> I would do it like this. That way you only have 1 function to handle your db connections and you can pass in the name of the db and use as many db's as you want. Nate Quote Link to comment https://forums.phpfreaks.com/topic/85215-how-to-manage-1-register-system-with-2-logins-and-2-different-mysql-databases/#findComment-434766 Share on other sites More sharing options...
grglaz Posted January 9, 2008 Author Share Posted January 9, 2008 //Database Information <?php function connectdb($dbname) { $dbhost = "localhost"; $dbuser = "username"; $dbpass = "yourpass"; //Connect to database mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error()); mysql_select_db($dbname) or die(mysql_error()); } connectdb('db1'); //run the query for db1 connectdb('db2'); //run the query for db2 ?> I would do it like this. That way you only have 1 function to handle your db connections and you can pass in the name of the db and use as many db's as you want. Nate First thanks for the replies and keep in mind that i m new on mysql. ok i see what u changed.so u think is better to use 2 different databases?or is better if i use one database and include the tables from both databases?or is the same with both ways? again thank u for replying Quote Link to comment https://forums.phpfreaks.com/topic/85215-how-to-manage-1-register-system-with-2-logins-and-2-different-mysql-databases/#findComment-435032 Share on other sites More sharing options...
chronister Posted January 9, 2008 Share Posted January 9, 2008 Well it depends on what your trying to save. There are very valid reasons to keep databases seperate, so if you need to keep them seperate then do so... but if they don't need to be then I would combine them. Nate Quote Link to comment https://forums.phpfreaks.com/topic/85215-how-to-manage-1-register-system-with-2-logins-and-2-different-mysql-databases/#findComment-435038 Share on other sites More sharing options...
grglaz Posted January 10, 2008 Author Share Posted January 10, 2008 ok i modified <?php //Database Information function connectdb($dbname) { $dbhost = "localhost"; $dbuser = "username"; $dbpass = "yourpass"; } //Connect to database mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error()); mysql_select_db($dbname) or die(mysql_error()); connectdb('db1'); $name = $_POST['name']; $email = $_POST['email']; $member_name = $_POST['username']; $member_key = md5($_POST['password']); // lets check to see if the username already exists $checkuser = mysql_query("SELECT username *FROM members WHERE member_name='$username'"); $username_exist = mysql_num_rows($checkmember); if($member_name_exist > 0){ echo "I'm sorry but the username you specified has already been taken. Please pick another one."; unset($member_name); include 'register.html'; exit(); } // lf no errors present with the username // use a query to insert the data into the database. $query = "INSERT INTO members (name, email, username, password) VALUES('$name', '$email', '$member_name', '$member_key')"; mysql_query($query) or die(mysql_error()); mysql_close(); //*****DO I HAVE TO PUT SOMETHING HERE TO CONTINUE AT THE SECOND DB?***** connectdb('db2'); $name = $_POST['name']; $email = $_POST['email']; $username = $_POST['username']; $password = md5($_POST['password']); //i think we dont need to check this database because if the username does not exist in the first database //means that does not exist also in the second $query = "INSERT INTO users (name, email, username, password) VALUES('$name', '$email', '$username', '$password')"; mysql_query($query) or die(mysql_error()); mysql_close(); echo "You have successfully Registered"; ?> i have not tested it,just tell me if u see any basic wrongs on it because am not sure if i modifieded well. if this way will work is ok but if not we will try to combine the databases Quote Link to comment https://forums.phpfreaks.com/topic/85215-how-to-manage-1-register-system-with-2-logins-and-2-different-mysql-databases/#findComment-435067 Share on other sites More sharing options...
Pancake Posted January 10, 2008 Share Posted January 10, 2008 <?PHP $name = $_POST['name']; $email = $_POST['email']; $username = $_POST['username']; $password = md5($_POST['password']); // lets check to see if the username already exists $checkuser = mysql_query("SELECT username FROM users WHERE username='$username'"); $username_exist = mysql_num_rows($checkuser); /* Stuff removed to shorten post */ $query = "INSERT INTO users (name, email, username, password) VALUES('$name', '$email', '$username', '$password')"; mysql_query($query) or die(mysql_error()); ?> AAHHHH!!! SQL INJECTION!!!! try something like this: <?PHP $name = escapeString($_POST['name']); $email = escapeString($_POST['email']); $username = escapeString($_POST['username']); $password = escapeString(md5($_POST['password'])); // lets check to see if the username already exists $checkuser = mysql_query("SELECT username FROM users WHERE username='$username'"); $username_exist = mysql_num_rows($checkuser); /* Stuff removed to shorten post */ $query = "INSERT INTO users (name, email, username, password) VALUES('$name', '$email', '$username', '$password')"; mysql_query($query) or die(mysql_error()); /* Removed to shorten post */ function escapeString($str) { if(get_magic_quotes_gpc()) stripslashes($str); return mysql_real_escape_string($str); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/85215-how-to-manage-1-register-system-with-2-logins-and-2-different-mysql-databases/#findComment-435081 Share on other sites More sharing options...
grglaz Posted January 10, 2008 Author Share Posted January 10, 2008 <?php //Database Information function connectdb($dbname) { $dbhost = "localhost"; $dbuser = "username"; $dbpass = "yourpass"; } //Connect to database mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error()); mysql_select_db($dbname) or die(mysql_error()); connectdb('db1'); $name =(escapeString ($_POST['name']); $email =(escapeString ($_POST['email']); $member_name =(escapeString ($_POST['username']); $member_key =(escapeString( md5($_POST['password'])); // lets check to see if the username already exists $checkuser = mysql_query("SELECT username *FROM members WHERE member_name='$username'"); $username_exist = mysql_num_rows($checkmember); function escapeString($str) { if(get_magic_quotes_gpc()) stripslashes($str); return mysql_real_escape_string($str); } // lf no errors present with the username // use a query to insert the data into the database. $query = "INSERT INTO members (name, email, username, password) VALUES('$name', '$email', '$member_name', '$member_key')"; mysql_query($query) or die(mysql_error()); mysql_close(); connectdb('db2'); $name = $_POST['name']; $email = $_POST['email']; $username = $_POST['username']; $password = md5($_POST['password']); //i think we dont need to check this database because if the username does not exist in the first //database //means that does not exist also in the second $query = "INSERT INTO users (name, email, username, password) VALUES('$name', '$email', '$username', '$password')"; mysql_query($query) or die(mysql_error()); mysql_close(); echo "You have successfully Registered"; ?> Ok i modifieded the code again,so u think is ok now? but if the code exists what code do then?do we have to 'echo' something? ok i will test it and i will tell the results tommorow. Also is ok the way we jump from the 1 db to another or we must put any code between them? Quote Link to comment https://forums.phpfreaks.com/topic/85215-how-to-manage-1-register-system-with-2-logins-and-2-different-mysql-databases/#findComment-435113 Share on other sites More sharing options...
revraz Posted January 10, 2008 Share Posted January 10, 2008 If you don't have a good reason to use 2 DBs, then don't IMO. Quote Link to comment https://forums.phpfreaks.com/topic/85215-how-to-manage-1-register-system-with-2-logins-and-2-different-mysql-databases/#findComment-435114 Share on other sites More sharing options...
grglaz Posted January 10, 2008 Author Share Posted January 10, 2008 If you don't have a good reason to use 2 DBs, then don't IMO. yea i have to find a solution because i have 2 forum systems and i will use them together in my server and they use their unique login/register systems so i have to create a register system to send the values to both databases. i think is not so usefull if the guests come to my site and they need to register twice and have to login 2 times. Quote Link to comment https://forums.phpfreaks.com/topic/85215-how-to-manage-1-register-system-with-2-logins-and-2-different-mysql-databases/#findComment-435119 Share on other sites More sharing options...
chronister Posted January 10, 2008 Share Posted January 10, 2008 <?php //Database Information function connectdb($dbname) { $dbhost = "localhost"; $dbuser = "username"; $dbpass = "yourpass"; //Connect to database mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error()); mysql_select_db($dbname) or die(mysql_error()); } /* I moved this below the connect and select statement because those need to be part of the function.*/ /* this function needs to be defined before you can use it */ function escapeString($str) { if(get_magic_quotes_gpc()) stripslashes($str); return mysql_real_escape_string($str); } $name =escapeString ($_POST['name']);//removed extra () $email =escapeString ($_POST['email']); //removed extra () $member_name =escapeString ($_POST['username']); /* removed extra () */ $member_key =escapeString( md5($_POST['password'])); // check to make sure our username don't exist in either db connectdb('db1'); $checkuser = mysql_query("SELECT username FROM members WHERE username='$member_name"); $dup1= mysql_num_rows($checkuser); connectdb('db2'); $checkuser= mysql_query("SELECT username FROM users WHERE username = '$member_name "); $dup2=mysql_num_rows($checkuser); $userNameExists = ($dup1 > 0 || $dup2 > 0) ? 'yes' : 'no' ; // shorthand if / else statement connectdb('db2'); if($userNameExists == 'yes') { connectdb('db1'); $query = "INSERT INTO members (name, email, username, password) VALUES ('$name', '$email', '$member_name', '$member_key')"; $result = mysql_query($query) or die(mysql_error()); mysql_close(); if(mysql_affected_rows($result) > 0){$success = 1;} connectdb('db2'); $query = "INSERT INTO users (name, email, username, password) VALUES ('$name', '$email', '$member_name', '$member_key')"; $result = mysql_query($query) or die(mysql_error()); mysql_close(); if(mysql_affected_rows($result) > 0){$success = 2;} if($success == 2){echo "You have successfully Registered";}; } else { $error = 'This username already exists, please try again.'; } ?> Ok... I restructured some things and added some things.... I did this in notepad and did not have code coloring so there may be a forgotten ' or " somewhere Basically is what I did is started with defining the functions. Then we set the form vars to friendly vars... This only needs to be done once not twice like you had. Then we move onto ensuring the username don't exist in either.. these username fields should be set as unique to prevent dups anyway... but we should handle it gracefully if there are duplicates. Then we say if dup1 is > 0 or dup2 is > 0 set our var to yes or no. Then we move onto the actual account creation... if there are dups then we set an error message.. otherwise we run the query. Once the queries have been run we make sure that both were successful.. you should probably write in a process that deals with the possibility of one query failing. In that case you would want to remove the successful one and try again or give an error message to the user. Thats how I would lay it out. This may be a good solution for dealing with 2 login systems. If your using a couple pre-built systems, you want them to work together and changing the db's would be a major pain... Just figure out how each one authenticates whether by sessions or dbs or cookies or what and make your login script setup what both of them need as far as sessions go. I just looked back over it, this appears to be a registration script. You should really have the user input the pass twice and compare them for typo errors as one of the first things you do after setting friendly vars for the input. Hope this helps Nate Quote Link to comment https://forums.phpfreaks.com/topic/85215-how-to-manage-1-register-system-with-2-logins-and-2-different-mysql-databases/#findComment-435155 Share on other sites More sharing options...
grglaz Posted January 10, 2008 Author Share Posted January 10, 2008 i tested this code: <?php //Database Information function connectdb($dbname) { $dbhost = "localhost"; $dbuser = "user"; $dbpass = "pass"; } //Connect to database mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error()); mysql_select_db($dbname) or die(mysql_error()); function escapeString($str) { if(get_magic_quotes_gpc()) stripslashes($str); return mysql_real_escape_string($str); } connectdb('db1'); $email =(escapeString ($_POST['email']); $name =(escapeString ($_POST['username']); $member_login_key =(escapeString( md5($_POST['password'])); $checkmember = mysql_query("SELECT name *FROM ibf_members WHERE name='$name'"); $dup1 = mysql_num_rows($checkmember); connectdb('db2'); $email = $_POST['email']; $username = $_POST['username']; $password = md5($_POST['password']); $checkuser = mysql_query("SELECT username *FROM users WHERE username='$username'"); $dup2 = mysql_num_rows($checkuser); $userNameExists = ($dup1 > 0 || $dup2 > 0) ? 'yes' : 'no' ; if($userNameExists == 'yes') { connectdb('offsite'); $query = "INSERT INTO members (email, username, password) VALUES('$email', '$name', '$member_login_key')"; mysql_query($query) or die(mysql_error()); mysql_close(); if(mysql_affected_rows($result) > 0){$success = 1;} connectdb('offsite_tracker'); $query = "INSERT INTO users (email, username, password) VALUES('$email', '$username', '$password')"; mysql_query($query) or die(mysql_error()); mysql_close(); if(mysql_affected_rows($result) > 0){$success = 2;} if($success == 2){echo "You have successfully Registered";}; } else { $error = 'This username already exists, please try again.'; } ?> i modifieded a bit because as i said these two databases use different table names db1 uses:'ibf_members' table and has 'name' table to stock the value of username and 'member_login_key' to stock the value of password db2 uses:'users' table and has 'username' to stock the value of username and 'password' to stock the value of password. Also i have erase the 'name' from the register.html and register.php because we dont need this value. Do you see anything wrongs on the code? I HAVE A RESULT : "Parse error: parse error in c:\..\..\..\register.php on line 25" line 25 is: "$email =(escapeString ($_POST['email']); " what does this mean i dont know. Quote Link to comment https://forums.phpfreaks.com/topic/85215-how-to-manage-1-register-system-with-2-logins-and-2-different-mysql-databases/#findComment-435407 Share on other sites More sharing options...
chronister Posted January 11, 2008 Share Posted January 11, 2008 That line should be $email = escapeString ($_POST['email']); To set a var you use $varName no quotes around it $varName = value; value can be a function... functions are all set as functionName()... that is what escapeString is... it's a function. Your passing the var of $_POST['email'] into it. Quote Link to comment https://forums.phpfreaks.com/topic/85215-how-to-manage-1-register-system-with-2-logins-and-2-different-mysql-databases/#findComment-436261 Share on other sites More sharing options...
grglaz Posted January 11, 2008 Author Share Posted January 11, 2008 That line should be $email = escapeString ($_POST['email']); To set a var you use $varName no quotes around it $varName = value; value can be a function... functions are all set as functionName()... that is what escapeString is... it's a function. Your passing the var of $_POST['email'] into it. ok chronister i will fix it and try again and i will tell the result soon,thank u Quote Link to comment https://forums.phpfreaks.com/topic/85215-how-to-manage-1-register-system-with-2-logins-and-2-different-mysql-databases/#findComment-436286 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.