Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/98159-connecting-to-database/
Share on other sites

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.

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>";
?>

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

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

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

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.

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

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

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.