GameYin Posted March 27, 2008 Share Posted March 27, 2008 www.gameyin.com/register.html Ok so I have this code.. <?php $host="localhost"; // Host name $username="gameyinc"; // Mysql username $password="*********"; // Mysql password $db_name="members"; // Database name $tbl_name="users"; // Table name $link = mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB") or die ("Could not connect to mysql because ".mysql_error()); $check = "select id from $tbl_name where username = '".$_POST['username']."';"; $qry = mysql_query($check) or die ("Could not match data because ".mysql_error()); $num_rows = mysql_num_rows($qry); if ($num_rows != 0) { echo "Sorry, there the username $user is already taken.<br>"; echo "<a href=register.html>Try again</a>"; exit; } else { $user=$_POST['username']; // insert the data $insert = mysql_query("insert into $tbl_name values ('NULL', '".$_POST['username']."', '".$_POST['password']."')") or die("Could not insert data because ".mysql_error()); // print a success message echo "Your user account has been created!<br>"; echo "Now you can <a href=login.html>log in</a>"; } ?> It won't let me connect to my database. My database is called members..and table users. Help? Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/ Share on other sites More sharing options...
Anzeo Posted March 27, 2008 Share Posted March 27, 2008 What's the error message you receive? Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502167 Share on other sites More sharing options...
MadTechie Posted March 27, 2008 Share Posted March 27, 2008 the error is "cannot connect"; but change $link = mysql_connect("$host", "$username", "$password")or die("cannot connect"); to this $link = mysql_connect("$host", "$username", "$password")or die(mysql_error()); now whats the error ? Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502168 Share on other sites More sharing options...
Anzeo Posted March 27, 2008 Share Posted March 27, 2008 the error is "cannot connect"; Completely overlooked that one >< Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502171 Share on other sites More sharing options...
GameYin Posted March 27, 2008 Author Share Posted March 27, 2008 @Anzeo, it was only error there... Now I have... "cannot select DB" Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502174 Share on other sites More sharing options...
MadTechie Posted March 27, 2008 Share Posted March 27, 2008 check the database name.. please note some Host will prefix your database name IE the database "Members" could be "gameyin_Members" EDIT: infact some also do the same with the username Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502175 Share on other sites More sharing options...
TimUSA Posted March 27, 2008 Share Posted March 27, 2008 also remember its case sensitive. Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502176 Share on other sites More sharing options...
GameYin Posted March 27, 2008 Author Share Posted March 27, 2008 Ok will do... Report: name is correct. Using..THIS information, I still have no way to connect.. <?php $host="localhost"; // Host name $username="gameyinc"; // Mysql username $password="......"; // Mysql password $db_name="members"; // Database name $tbl_name="users"; // Table name $user=$_POST['username']; $link = mysql_connect("$host", "$username", "$password")or die(mysql_error()); mysql_select_db("$db_name")or die("cannot select DB") or die ("Could not connect to mysql because ".mysql_error()); $check = "select id from $tbl_name where username = '".$_POST['username']."';"; $qry = mysql_query($check) or die ("Could not match data because ".mysql_error()); $num_rows = mysql_num_rows($qry); if ($num_rows != 0) { echo "Sorry, there the username $user is already taken.<br>"; echo "<a href=register.html>Try again</a>"; exit; } else { // insert the data $insert = mysql_query("insert into $tbl_name values ('NULL', '".$_POST['username']."', '".$_POST['password']."')") or die("Could not insert data because ".mysql_error()); // print a success message echo "Your user account has been created!<br>"; echo "Now you can <a href=login.html>log in</a>"; } ?> EDIT: I checked case-sensitive. Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502178 Share on other sites More sharing options...
MadTechie Posted March 27, 2008 Share Posted March 27, 2008 Your username format will be: webusername_dbusername Your DB format will be: webusername_dbname Your hostaddress will be: localhost Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502184 Share on other sites More sharing options...
GameYin Posted March 27, 2008 Author Share Posted March 27, 2008 I know how to connect, it isn't my first time, with my code I provided someone should be able to provide why my method isn't working, or at least suggest an alternative. Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502185 Share on other sites More sharing options...
MadTechie Posted March 27, 2008 Share Posted March 27, 2008 have you tried? $db_name="webusername_members"; // Database name replacing webusername with your username ? as you have no underscore on the name! EDIT: if your getting "cannot select DB" then the DB name is wrong Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502186 Share on other sites More sharing options...
GameYin Posted March 27, 2008 Author Share Posted March 27, 2008 I got it working www.gameyin.com/register.html I had to change $db_name="members"; // Database name to $db_name="gameyinc_members"; // Database name Then I just rewrote the code where needed. Thanks. Topic Solved. Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502192 Share on other sites More sharing options...
MadTechie Posted March 27, 2008 Share Posted March 27, 2008 i did say that 3 times! Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502194 Share on other sites More sharing options...
GameYin Posted March 27, 2008 Author Share Posted March 27, 2008 Only saw you the last time, ok but I got another problem...I have cookie set and if its not set, display the register/login link, and if they are signed in, display logout/usercp link. www.gameyin.com You will see what I mean... Here is the piece of code I've been working with. <div class="tmenu"> <b><a class="add" href="index.php">Home</a></b> <a href="store/index.html">Store</a> <?php if (!isset($_COOKIE['loggedin'])) echo "<a href=\"register.html\">Register</a> <a href=\"login.html\">Login</a>"; $mysite_username = $HTTP_COOKIE_VARS["mysite_username"]; echo "<a href=\"logout.php\">Logout</a> <a href=\"usercp/index.html\">UserCP</a>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502209 Share on other sites More sharing options...
GameYin Posted March 27, 2008 Author Share Posted March 27, 2008 Maybe an else statement? I'll try that...You will need help with setting it up..tried..failed Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502212 Share on other sites More sharing options...
wildteen88 Posted March 27, 2008 Share Posted March 27, 2008 Maybe an else statement? I'll try that... correct! Also when retrieving cookies use $_COOKIE not $HTTP_COOKIE_VARS (this variable is depreciated). Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502215 Share on other sites More sharing options...
GameYin Posted March 27, 2008 Author Share Posted March 27, 2008 I'm not good at PHP, can you give me my code to replace this with? <div class="tmenu"> <b><a class="add" href="index.php">Home</a></b> <a href="store/index.html">Store</a> <?php if (!isset($_COOKIE['loggedin'])) echo "<a href=\"register.html\">Register</a> <a href=\"login.html\">Login</a>"; $mysite_username = $HTTP_COOKIE_VARS["mysite_username"]; echo "<a href=\"logout.php\">Logout</a> <a href=\"usercp/index.html\">UserCP</a>"; ?> Tried and failed with else...dunno how to do it with this.. Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502216 Share on other sites More sharing options...
MadTechie Posted March 27, 2008 Share Posted March 27, 2008 try (had a quick look) <div class="tmenu"> <b><a class="add" href="index.php">Home</a></b> <a href="store/index.html">Store</a> <?php if (!isset($_COOKIE['loggedin'])) { echo "<a href=\"register.html\">Register</a> <a href=\"login.html\">Login</a>"; }else{ $mysite_username = $_COOKIE["mysite_username"]; echo "<a href=\"logout.php\">Logout</a> <a href=\"usercp/index.html\">UserCP</a>"; } ?> edit: Opps moved a line down! i assume that how its got to be Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502219 Share on other sites More sharing options...
GameYin Posted March 27, 2008 Author Share Posted March 27, 2008 All is good, except when I click login, enter details I have to refresh the page to get the other stuff to come up that's supposed to...should I look at a meta refresh or soemthing ??? ??? EDIT: Also when I signout I must refresh it too.... Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502223 Share on other sites More sharing options...
wildteen88 Posted March 27, 2008 Share Posted March 27, 2008 An else can only be after an if statement so common sense will tell you it needs to be this: Reformated your code: $mysite_username = $_COOKIE['mysite_username']; if (!isset($_COOKIE['loggedin'])) { echo "<a href=\"register.html\">Register</a> <a href=\"login.html\">Login</a>"; } else { echo "<a href=\"logout.php\">Logout</a> <a href=\"usercp/index.html\">UserCP</a>"; } Also I highly suggest that use sessions rather than cookies, because at the moment I can easily by pass the need to log in, by simply setting the loggedin cookie. If you use sessions then it will prevent the user from tampering with the cookies. Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502226 Share on other sites More sharing options...
GameYin Posted March 27, 2008 Author Share Posted March 27, 2008 Well How would I Make the switch to sessions? Also thanks for the revised code..I'll go try it out Result: Still need refresh problem fixed. EDIT: ALSO, MadTechi, I would go to your profile and change the link, it says darkmindz on it except link is to phpfreaks..just thought I'd point that out. Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502230 Share on other sites More sharing options...
wildteen88 Posted March 27, 2008 Share Posted March 27, 2008 Well How would I Make the switch to sessions? Also thanks for the revised code..I'll go try it out Have a read of this tutorial on sessions. Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502238 Share on other sites More sharing options...
GameYin Posted March 27, 2008 Author Share Posted March 27, 2008 Do you think I can make the switch from cookies to sessions quickly? Also will this fix my refresh prob? Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502242 Share on other sites More sharing options...
MadTechie Posted March 27, 2008 Share Posted March 27, 2008 Shouldn't be a major problem switching, just need session_start(); at the start and change $_COOKIE AND $HTTP_COOKIE_VARS to $_SESSION Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502248 Share on other sites More sharing options...
MadTechie Posted March 27, 2008 Share Posted March 27, 2008 it will not fix the refesh problem, i am going to guess here but i think your checking the cookie (are they logged in) but only later in the code checking they login details.. ie with the code below if you load login.php?login your get the message your not logged in but if you refeash it will change to you are logged in <?php //Section 1 if($_COOKIE['loggedin']) { echo "you are logged in"; }else{ echo "your not logged in"; } //section2 if(isset($_GET['logout'])) { $_COOKIE['loggedin'] = false; } if(isset($_GET['login'])) { $_COOKIE['loggedin'] = true; } ?> But if you move section 1 under section 2 it will work ie <?php //section2 if(isset($_GET['logout'])) { $_COOKIE['loggedin'] = false; } if(isset($_GET['login'])) { $_COOKIE['loggedin'] = true; } //Section 1 if($_COOKIE['loggedin']) { echo "you are logged in"; }else{ echo "your not logged in"; } ?> so i think you need to find the part where to check the login and move it up.. Quote Link to comment https://forums.phpfreaks.com/topic/98159-connecting-to-database/#findComment-502257 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.