0

I am trying to make a simple php and sql login form, but it is not working

Can anyone help me to fix my code?

<form  method="post" action="form.php">
    Username <input type="text" name="username"><br>
    Password <input type="password" name="password">
    <br>
    <input type="submit" name="submit" value="submit">
</form>


<?php
if (isset($_POST['submit'])) {

    $username = $_POST['username'];
    $password = $_POST['password'];
    $con = mysql_connect("localhost", "root", "");
    mysql_select_db($con, "formcolumn");
    $sql = mysql_query("select * from data1_table where username='$username' and password='$password' ");
    $row = mysql_fetch_array($sql);
    $uname = $row['username'];
    $pass = $row['password'];
    if ($username == $uname && $password == $pass) {
        header("Location: main.php");
    } else {
        echo "invalid username and password ";
    }
}
?>
Mr Lister
  • 42,557
  • 14
  • 95
  • 136

2 Answers2

1

Replace mysql_select_db($con, "formcolumn"); with mysql_select_db("formcolumn",$con); where formcolumn is your Database name

Popnoodles
  • 27,674
  • 1
  • 40
  • 53
  • thanks bro still getting 1 warning Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\php\zz\m\form.php on line 19 – Mohit gupta Dec 27 '13 at 13:13
0

Interchange the positions of your parameters in your mysql_select_db function bass the name of your database first then pass the connection. You must remember this is a predefined function and it is defined to accept parameters in a certain order therefore it expects that the first parameter passed to the function is going to be the database name.

You should have

mysql_select_db("formcolumn",$con);

also why not try something along the lines of this:

$sql = "select * from data1_table where username='$username' and password='$password'";
$result = mysql_query($sql,$con);
$row= mysql_fetch_assoc($result);