简体   繁体   中英

PHP/MySQL Database Issues

PHP/MySQL newbie question.

I have a database I've imported into my local phpmyadmin. However it seems I can't access it from my a php application. The connection string seems right and when I try to authenticate user credentials to access database information, no problems.

However authenticate everyone and knows when I put in fake credentials. Still it won't pull any other information from the database.

For instance, once a users login they should see something like, "Hello username", that kind of thing. At this point I see "Hello" without the username. Any ideas what i might be missing?

ok sorry for all the back and forth guys. here's the issue. I've got a php app and mysql database connected (or at least i hope so...). there is a form in the header of my page for users to login. i CAN login but i can't seem to pull any information from the database. If i try to log in using bogus credentials i'm given an "incorrect login" message. However when i do login it can't seem to pull anything else from the database other than those credentials.

ok here's the code...

DATABASE CONNECTION:

<?php
session_start();

# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_test = "localhost";
$database_test = "girlpower";
$username_test = "root";
$password_test = "password";
$test = mysql_pconnect($hostname_test, $username_test, $password_test) or trigger_error(mysql_error(),E_USER_ERROR);

mysql_select_db($database_test, $test);
?>

HERE'S THE LOGIN CODE:

<?php
require_once("includes/db.php");

$userEmail = trim($_POST['userEmail']);
$password = trim($_POST['password']);
$userlogin = trim($_POST['userlogin']);

//print_r($_POST);

if ($userlogin != ""  &&  $userEmail != "" && password != ""  )
{
    $sql = sprintf("Select * from girlpower where email = '%s' and pwd = '%s'", $userEmail, $password );

    //echo $sql;

    $res = mysql_query($sql);

    if( mysql_num_rows( $res ) == 0 )
    {
        //TODO:
        //redirect..

        header("Location: " . $_SERVER['HTTP_REFERER']  . "?fail=1" );

    }
    else
    {
        $row = mysql_fetch_assoc( $res );
        $_SESSION['recordId'] = $row['recordId'];
        $_SESSION['firstName'] = $row['firstName'];

        //echo "success...";
        header("Location: " . $_SERVER['HTTP_REFERER'] );

        //print_r($_SERVER);

    }

    //print($_SESSION);

}
else
{
        header("Location: " . $_SERVER['HTTP_REFERER']  . "?fail=1" );

}

HERE'S WHERE HEADER CODE (THIS IS WHERE THE FORM LIVES) :

<?php

    $fail = false;
    if( $_GET['fail']  != "")
    {
        $fail = true;
    }


        if( $_SESSION['recordId'] != "" )
    {
        //get the 1st name
        $firstName = $_SESSION['firstName'];
    }

       ?>

<div id="header">
< SHOULD BE LINK "index.php"></a>

<div id="ulogin">
  <fieldset id="userlogin">
        <?php if( $firstName == ""){ ?>
        <form name="loginForm" action="dologin.php" method="post" >
    <label for="logemail">Members Login: &nbsp;Email</label> <input type="text"
         name="userEmail" id="logemail" size="15" />
        <label for="logpwd">Password</label> <input type="password" name="password"
                id="logpwd" size="15" />
                <input type="submit" name="userlogin" id="login" value="Login" />

                <?php if ($fail == true ) {?>
               <span class="error">Incorrect Login</span>
                <?php }?>

          </form>
   </fieldset>

           <?php
        }
    else{
    ?>


<div id="welcome">Welcome <?= htmlentities( $firstName ) ?> | <SHOULD BE LINK ="seemsgs.php?receiver_id="<?=  $_SESSION["recordId"]?> > See Messages</> |<SHOULD BE LINK ="member.php">Update Profile</> |  <SHOULD BE LINK ="dologout.php">Logout</a> </div><?php }?>
</div>

I noticed you are using the root user (though I'm not sure if this was added merely for posting purposes here.) Depending on what hosting environment you are using this may or may not be a problem - some shared hosts force you to assign a user for databases in MySQL.

From the looks of things your query should be executing and returning a number of rows at the least. Have your tried print_r on the results array? If so, can you post the output?

If you are successfully getting results from the database, I don't see anywhere in your posted code a conditional that echos out a success message. You may want to check isset() against the $_SESSION superglobal keys you assign ( recordID and firstName) and if true echo out a success message if you have not already done so.

Just a thought as well - I noticed you are using sprintf to format out your query - it may be a bit too robust for what you're trying to accomplish, but using PDO to get parameterized sql queries is a nice way to get that job done where available. Introduction to PHP 5 PDO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM