Jump to content

problem with MYSQL


mojtabaa.sln

Recommended Posts

hi every one...

i have a problem with mysql.

i try to create a login page.

and i wrote code below.

but this code has 2 errors:

<!doctype html><html><head><meta charset="utf-8"><title>Login page!</title> </head><body> <form action="login.php" method="post" >Username:<input name="user" type="text" >Password:<input type="text" name="password"><input type="submit" name="login_btn" value="Login!"></form>  <?phpif(isset($_POST['login_btn'])){    $user_name=$_POST["user"];    $password=$_POST['password'];    $conn=mysqli_connect("localhost","mojtaba",    "9addsfsf93gfh6","mojidb");    $query="SELECT password FROM users WHERE username=$user_name";         $result=mysqli_query($conn,$query);    $result2=mysqli_fetch_assoc($result);         if(mysqli_num_rows($result)==1)    {        //check password        if(md5($password)==$result2['password'])        {            //logined OK!            echo "logined successfully!";            exit();        }        else echo "login unsuccessful!";    }}?>  </body></html>

and these are errors:

1:Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:wampwwwlogin.php on line 22

2:Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:wampwwwlogin.php on line 24

 

i want to help me how can i solve these errors?

 

and another question!

i created a register page that users can become a member.

Even though i set column of ID with PRIMARY KEY but still Repetitious usernames will create and save in the database.

how can i do this problem??? that Repetitious usernames don't save...

 

and at last forgive me for my bad english!

Link to comment
Share on other sites

You have an error in your SQL and you're not checking for errors. This thread shows mysql functions, but it works the same with mysqli functions. The reason you're getting that error is because you are not using prepared statements, you need to look up and use prepared statements.http://w3schools.invisionzone.com/index.php?showtopic=44106Also, MD5 is not acceptable for passwords, use password_hash:http://php.net/manual/en/function.password-hash.php

Even though i set column of ID with PRIMARY KEY but still Repetitious usernames will create and save in the database.how can i do this problem??? that Repetitious usernames don't save...

Give the username column a unique index.
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...