artweb Posted February 19, 2008 Share Posted February 19, 2008 I have a mysql database set up. where I manual go in and put the usernames and Passwords of customer that register with me though my email. Then when they receive and email from me telling then they can login. I also have another table set up to record their login info, company name and usernmane and password. The problem is I want this to only work if the user is in the first table. But even if the user doesn't exist in my first table it records everything in my second table. So I need something like if login successful insert into second table. But only if successful. Please help ??? code bellow: <?php ob_start(); $host=""; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name=""; // Database name $tbl_name="members"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // Define $myusername and $mypassword $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); //mysql_num_row is counting table row $count=mysql_num_rows($result); //if result match $myusername and mypassword table row must be 1 row if($count==1){ session_register("myusername"); header("location:happy.php"); } else { echo "Wrong Username or Password"; } if($count==1){ mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("test_mysql")or die("cannot select DB"); } $sql="INSERT INTO test_mysql(companyname, myusername, mypassword)VALUES('$companyname', '$myusername', '$mypassword')"; $result=mysql_query($sql); ob_end_flush(); ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted February 19, 2008 Share Posted February 19, 2008 Your INSERT statement is outside of your IF $count function, you need to move it in. Also, combine those two IF $count statements into one, no need t have two when they both check the same variable. Quote Link to comment Share on other sites More sharing options...
aschk Posted February 19, 2008 Share Posted February 19, 2008 You're also potentially issuing your connection twice, and selecting from a table that is remarkably named the same as the database (this is valid, i'm just curious if it's a typo). Quote Link to comment Share on other sites More sharing options...
artweb Posted February 19, 2008 Author Share Posted February 19, 2008 Thanks for your help Ok, I changed it like this, code below, and now nothing get recorded into my second table. I still don't know what I'm doing. Please help more. <?php $tbl_name="members"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // Define $myusername and $mypassword $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); //mysql_num_row is counting table row $count=mysql_num_rows($result); //if result match $myusername and mypassword table row must be 1 row if($count==1){ session_register("myusername"); header("location:happy.php"); mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("test_mysql")or die("cannot select DB"); $sql="INSERT INTO test_mysql(companyname, myusername, mypassword)VALUES('$companyname', '$myusername', '$mypassword')"; $result=mysql_query($sql); } else { echo "Wrong Username or Password"; } ob_end_flush(); ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted February 19, 2008 Share Posted February 19, 2008 You need to do some trouble shooting. First, add a mysql_error() after all your querries. Second, echo your variables so you know what path your code is taking for your IF statement. Quote Link to comment Share on other sites More sharing options...
artweb Posted February 19, 2008 Author Share Posted February 19, 2008 I'm really new to this can you show me how? :-\ Quote Link to comment Share on other sites More sharing options...
artweb Posted February 19, 2008 Author Share Posted February 19, 2008 I did some testing and it say that it can't connect to database. I know that it connects to the first one but not the second. So how do you connect one database with two tables? Quote Link to comment Share on other sites More sharing options...
revraz Posted February 19, 2008 Share Posted February 19, 2008 No reason to connect twice, the connection is still open from the first connection. Remove the 2nd one. Quote Link to comment Share on other sites More sharing options...
artweb Posted February 19, 2008 Author Share Posted February 19, 2008 If I don't connect to the second table how will I be able to insert into. The first table is for storing username and passwords so when user fills out the form to login, it checks the database for that username and password if it finds it. It will let them in to the protected part. If not it makes them try again. The second is for recording when the user logs in. It records even if the user has to try again. Quote Link to comment Share on other sites More sharing options...
revraz Posted February 19, 2008 Share Posted February 19, 2008 You don't "connect" to a table, you connect to a Database. Once that DB connection is made, you can use it within the script. You Query a table. Quote Link to comment Share on other sites More sharing options...
artweb Posted February 19, 2008 Author Share Posted February 19, 2008 Thanks for all your help. Sorry to be so new. But with what you told me and some hard work, I got it to work perfectly. 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.