Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7691495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:38:16+00:00 2026-05-31T20:38:16+00:00

What am I doing wrong here? It says Database Count Failed (on the $count

  • 0

What am I doing wrong here? It says Database Count Failed (on the $count line in Process.php) without giving reason. When I take our the “or die” part of that line, it always displays “user account created” no matter which button I pressed and it doesn’t actually create an account…

Form.php

<html>
<head>
<title>Forms</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.css" />
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.js"></script>
</head>
<body>
    <form action="process.php" method="post">
        <label for="username">Email: </label>
        <input type="email" name="username" value="" id="username"/>
        <br/>
        <label for="password">Password: </label>
        <input type="password" name="password" value="" id="password"/>
        <br/>
        <input type="submit" name="submit" value="Sign in"/>
        <input type="submit" name="submit" value="Sign up"/>
    </form>
</body>
</html>

Process.php

<?php
    //1. Create a database connection
    $connection = mysql_connect("localhost","web","1234") or die("Database connection failed: " . mysql_error());

    //2. Select a database to use
    $db_select = mysql_select_db("tongue", $connection) or  die("Database selection failed: " .mysql_error());
?>
<html>
<head>
<title>Form processing</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.css" />
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.js"></script>
</head>
<body>
    <?php
        $username = $_POST['username'];
        $password = trim($_POST['password']);
        $hashed_password = sha1($password);

        $action = $_POST['submit'];

    //3. Select email, password from database
        $query = "
                SELECT email, password FROM user 
                WHERE email='$username' AND password='$hashed_password'";
        $user = mysql_query($query, $connection)
            or die ("Database query failed: ".mysql_error());

        $count = mysql_num_rows($user) or die ("Database count failed: ".mysql_error());

    //4. Authenticate user
        if ($count == 1) {
            if($action="Sign up"){
                echo "User already exists";
            } else if ($action="Sign in"){
                echo "User signed in";
            };
        } else if ($count == 0){
            if($action="Sign up"){
            $query = "
                INSERT INTO users (email, password)
                VALUES ('$username', '$hashed_password')";
            $signup = mysql_query($query, $connection);
            echo "User account created";
            } else if ($action ="Sign in"){
                echo "Username and/or password incorrect";
            };
        };


    ?>
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:38:17+00:00Added an answer on May 31, 2026 at 8:38 pm

    Try running this example:

    $a = 0 OR die('foobar');
    var_dump($a);
    

    You will see, that the OR die() will be executed any time the value is considered false.

    That said .. your code is quite bad.

    • The script is open to an SQL injection
    • You are using mysql_* API, which is more then 10 years old, and should not be used in any newly written code. Instead you should learn to use either PDO or MySQLi.
    • It’s pointless to select by both username and password, if you already have a unique username column
    • This is not how you should hash your password
    • Do not create new accounts without telling people beforehand. What if i make a mistake in my email address ?

    This is something like how i would do it:

    $connection = new PDO( 'mysql:host=hostname;dbname=tongue', 'web', '1234');
    $connection->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
    
    $statement = $connection->prepare('
         SELECT 
             email, 
             hash, -- instead of password, because thats what it is
             salt
         FROM user 
         WHERE email = :username
    ');
    $statement->bindParam( ':username', $_POST['username'], PDO::PARAM_STR, 127);
    if ( $statement->execute() )
    {
        $data = $statement->fetch( PDO::FETCH_OBJ );
    }
    
    if ( $data )
    {
        $hash = crypt( $_POST['password'], '$2a$07$' . $data->salt . '$' );
        if ( $hash === $data->hash )
        {
            // user OK
        }
        else
        {
            // login failed 
        }
    }
    else
    {
        //login failed
    }
    

    This is obviously a bit simplified version, because, if you read manual entry for ‘crypt()`, you will notice that the resulting hash already contains the original salt. You can extract if from it. But the intention was to make a point, not to add too much detail.

    Also the if condition for login could have been wrapped into single if, because, when you do if( $condition_1 && $condition_2 && $condition_3 ), PHP will stop checking at the first failed condition.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What am I doing wrong here folks? <?php include 'header.php'; /** * Display a
What am I doing wrong here? My use of the INTO clause seems to
What am I doing wrong here? I'm trying to get started with jQuery UI
What am I doing wrong here: class Helo { // main: generate some simple
What am I doing wrong here? I'm serializing a value, storing it in a
What am I doing wrong here? private void SendMail(string from, string body) { string
What is it I am doing wrong here: When I try to use the
I don't understand what I'm doing wrong here: public void Draw(GameTime gameTime) // in
I can't seem to figure out what I am doing wrong here. I publish
Someone has to be able to explain what I'm doing wrong here! I'm trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.