-5

Please help me I want my program to choose a site if it has not yet username then it will proceed it to ch_uname.php. Then if the login credentials have already username then it will be preceded to index_profile.php. Thank you in advance.

if(mysql_num_rows($runcreds)> 0 ) //checking log in forms
{
    if(mysql_num_rows($run_uname)>=1 ) //if username has already avalaible(proceed)
    {
        $_SESSION['Email_add']=$email;
        echo "<script>window.open('modules/index_profile.php','_self')</script>";
    }
    if(mysql_num_rows($run_uname)<1)//choouse username if has not yet username
    {
        $_SESSION['Email_add']=$email;
        echo "<script>window.open('forms/ch_uname.php','_self')</script>";
        //modules/index_profile.php
    }
}
else
{
    echo "<script>alert('Admin details are incorrect!')</script>";
}
}
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
RumRum
  • 59
  • 10
  • So what is the problem? – Sougata Bose Jan 30 '15 at 07:21
  • It cant direct to the next page , I think Sgt. I got the right logical condition but it has done nothing. For example if the **mark_casel@gmail.com** has already a **username** named **mark10** then it will direct to the homepage of the profile site. But if **mark_casel@gmail.com** has not yet inputted **username** then that is the time for that _email_ to choose its **username.** – RumRum Jan 30 '15 at 07:32
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 03 '16 at 14:17
  • ah! yah yah! I already converted that into PDO. Since this was posted year ago. I prefer PDO. :)) – RumRum May 04 '16 at 03:27

1 Answers1

1

Here is a basic demonstration (using a PDO connection) of what I think you are looking for? I am assuming some stuff here because you don't give enough info before your code snippet:

session_start();
// I will use PDO because I cannot bring myself to use mysql_ in this demonstration
// Initiate connection (assigning credentials assumed)
$con =  new PDO("mysql:host=$mysqlDB;dbname=$mysqlTable", $mysqlUser, $mysqlPass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT));

if(isset($_POST['login'])) {
    $username = trim($_POST['username']);
    // Stop if empty
    if(empty($username)) {
            // You can echo or assign to a variable to echo down the page
            echo 'Username cannot be empty';
            return;
        }
    // Set up prepared statement
    $query = $con->prepare("select Email_add,password from `users` where username = :username");
    $query->execute(array(":username"=>$username));
    // Loop through returned
    while($row = $query->fetch(PDO::FETCH_ASSOC)) {
            $result[] = $row;
        }
    // If the loop comes up blank, assign false (0)        
    $result = (isset($result) && !empty($result))? $result:0;
    // If username exists
    if($result != 0) {
            // I am assuming you have some form of super secure hash method for passwords...
            $password = bcrypt($_POST['password']);
            // If passwords match, create session
            if($result[0]['password'] == $password) {
                    $_SESSION['Email_add'] = $result[0]['Email_add'];
                    // You probably don't need javascript to redirect
                    header('Location: modules/index_profile.php');
                    exit;
                }
            else {
                    // Password doesn't match
                    // You can echo or assign to a variable to echo down the page
                    echo 'Invalid Username/Password';
                }

        }
    // This would mean the username doesn't exist
    else {
            header('Location: forms/ch_uname.php');
            exit;
        }
}
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
  • Though that's not the exact thing I am looking for, its covered up how will I initiate my code for if else statement. Thanks. :) – RumRum Feb 02 '15 at 08:46