Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 10 of 10
  1. #1
    Member
    Join Date
    Jun 2007
    Location
    Scotts Valley, CA
    Posts
    49
    Member #
    15405
    First off, I'm very much a rookie, and working on putting together my first database-driven site. Apologies if my question is a naive one-- I've tried to be specific.

    I'm trying to alert customers on my B2B site when their accounts are past due. Trouble is, I'd like to have this alert be:

    1) discrete and relatively unobtrusive and, more importantly:
    2) not there at all when the user's account is not past due

    For login, I'm using php sessions with the usernames and passwords validated through a mySQL table.

    I've also got a table showing financial information that's indexed to the different usernames.

    The login redirects the code below, but I can't seem to get users with a balance of 0 on their account to the main page. Everyone winds up on financePopUp.php.

    <?
    // Connect to database
    $username = "admin";
    $password = " ";
    mysql_connect(localhost,$username,$password);

    // Ask it for the balance due on the user who's logged in
    mysql_db_query ("myDatabase","SELECT BalanceDue FROM Table WHERE Username='$_SESSION[user]'");
    $j = $row["BalanceDue"];

    // Send the user to the main page if the balance due is zero
    if ($j = 0)
    {
    header ('Location: main.php');
    exit;
    }

    // Or send the user to a page with the pop-up if the balance is not zero
    else
    {
    header ('Location: financePopUp.php');
    }
    ?>

  2.  

  3. #2
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Er... You don't seem to be grabbing the actual row.
    Code:
    $res = mysql_query(...);
    $row = mysql_fetch_assoc($res);

  4. #3
    Member
    Join Date
    Jun 2007
    Location
    Scotts Valley, CA
    Posts
    49
    Member #
    15405
    Thanks for the advice, Shadowfiend. I inserted the recommended code after line 9, and am still getting the same result. Regardless of the value of the balance, I wind up being redirected to financePopUp.php after login. How do I get the user to go straight to main.php when they don't owe anything?

    Cheers,

    K.

  5. #4
    ljm
    ljm is offline
    Senior Member ljm's Avatar
    Join Date
    Aug 2006
    Location
    Manchester, England
    Posts
    284
    Member #
    13684
    Liked
    1 times
    Your code is a bit wrong at this point:
    PHP Code:
    <?
         mysql_db_query 
    ("myDatabase","SELECT BalanceDue FROM Table WHERE Username='$_SESSION[user]'");
         
    $j $row["BalanceDue"];
    ?>
    Firstly, [phpfunction]mysql_db_query[/phpfunction] isn't stored in a variable, so it's returning the result of the query, but it's not being used by anything. Secondly, you're trying to get a row from the database based on your SQL, but the results haven't been returned as an array. You need something like this:

    PHP Code:
    <?
    // This is the query itself. You should validate the 'user' session variable.
    $query mysql_query("SELECT BalanceDue FROM Table WHERE Username='$_SESSION['user']' LIMIT 0,1");

    // This takes the results of the query, and turns it into an array.
    $array mysql_fetch_array($query);

    // This gets the data you want from the array
    $j $array['BalanceDue'];

    ?>

  6. #5
    Member
    Join Date
    Jun 2007
    Location
    Scotts Valley, CA
    Posts
    49
    Member #
    15405
    Thanks, LJM. I messed around with the code a bit more, though, and I'm still having trouble with the redirect. On financePopUp.php, this is the section of code that generates the pop-up:
    Code:
    <?
     session_start();
        $username = "foobar";
     $password = "foo";
      mysql_connect(localhost,$username,$password);
        $query = mysql_db_query ("session_distributors","SELECT BalanceDue FROM Finance WHERE Username='$_SESSION[user]' LIMIT 0,1");
     $array = mysql_fetch_array ($query);
     $j = $array['BalanceDue'];
      ?>
      
      <SCRIPT LANGUAGE="JavaScript1.2">
      alert(' Your Account Shows and Overdue Balance of <? echo $j ?> dollars');
      </SCRIPT>
    How can I get the redirect to work?

    Again, thanks to everyone for your help...

    K.

  7. #6
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    In your code:
    Quote Originally Posted by kalchas
    // Send the user to the main page if the balance due is zero
    if ($j = 0)
    {
    header ('Location: main.php');
    exit;
    }
    As you're trying to check the value of $j, you have to use double equal signs:
    PHP Code:
    // Send the user to the main page if the balance due is zero
    if ($j == 0)
    {
    header ('Location: main.php');
    exit;

    S. Rosland

  8. #7
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Ah, the old equality-vs-assignment bug. Those are always such delightful fun

  9. #8
    Member
    Join Date
    Jun 2007
    Location
    Scotts Valley, CA
    Posts
    49
    Member #
    15405
    Thanks Rosland! Equality vs. assignment was correct (bumping myself in the forehead with the heel of my hand). Now, though, I'm redirecting everybody to main.php, even the deadbeats who should have to deal with my financePopUp.php. It's like the "else" clause never kicks in... even when I pretend my guinea pig user owes me $1,000,000!

  10. #9
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Quote Originally Posted by kalchas
    Now, though, I'm redirecting everybody to main.php, even the deadbeats who should have to deal with my financePopUp.php...
    You should check the value of $j;
    make an echo statement after assigning a value to $j, to see what value it holds. By doing that, you will also check that your DB query works , and is returning valid data.
    You should also make an exception handler, in case the database itself holds invalid data in some of its rows.

    ##############

    Anytime I make any sort of coding functionality I'm uncertain of, I make a testfile like "testDelete.php" where I upload whatever part of code I want to test.

    Let's say you have a four stage process like this:
    1. First part of the code takes user inputs, cleans them up and sends them to the server for processing.
    2. Second part recieves the user data and makes a DB query based on it.
    3. Third part extracts the information from DB and processes it.
    4. Fourth part performs one of three different events, depending on the result of the processed DB data.


    If any of the above code sections were unfamiliar to me, and had never been tested before (by me), I would run them isolated through "testDelete.php" first. That makes it so much easier to check for bugs. An isolated test run will immediately expose:
    • spelling errors
    • syntax errors
    • if the code produces the result you intended.


    If, let's say, the fourth part of the code above would redirect or produce some other results based on a numeric value produced by processing the data from the DB query.

    The below example is very simple and would hardly need to be run seperately, but just to illustrate the point.
    You know, from testing, that the third part of the code example above produces either a negative number, a positive number or zero, that you assign to a variable (like $j in your code). To test if the following if/else section work as intended, you would upload something like this to "testDelete.php":
    PHP Code:
    for($i=-1$i<2$i++)
    {
        if(
    $i<0)
        {
            echo 
    "\$i is less than zero<br />";
        }
        elseif(
    $i==0)
        {
            echo 
    "\$i is zero<br />";
        }
        else
        {
            echo 
    "\$i is more than zero<br />";
        }

    If you do that, you will immediately identify where your code strays off and produces undesirable results.
    S. Rosland

  11. #10
    Member
    Join Date
    Jun 2007
    Location
    Scotts Valley, CA
    Posts
    49
    Member #
    15405
    Hi all,

    Thanks so much for your help. I wound up resolving the issue and got done what I needed to get done. Here's what I did, in case it's of any help to anyone else:

    On the page where I wanted the Finance Info to appear:

    PHP Code:
    <?
        
    include_once("PHP/mySQLConnect.php");
        
    connectToDB($username$password);
        include_once(
    "PHP/financeQuery.php");
        if (
    $j != 0)
            {
            include 
    "PHP/financePopUp.php";
            }
    ?>
    mySQLConnect.php just contains a function for connecting to the mySQL database.

    financeQuery contains this php:

    PHP Code:
    <?
        $query 
    mysql_db_query ("session_distributors","SELECT * FROM Finance WHERE Username='$_SESSION[user]' LIMIT 0,1");
        
    $array mysql_fetch_array ($query);
        
    $j $array['BalanceDue'];
        
    $i $array['LastPaymentDate'];
        
    $k $array['LCorWireDue'];
    ?>
    Then here's what's on financePopUp.php:

    HTML Code:
    <div id="finance" onclick="event.cancelBubble=true;">
    <a href = "wireInfo.php" onclick="hidecurrenttarget="_blank">
        <div id="financeHeader">
            Your Account is Currently Carrying A Balance
        </div>
        <p class="clear"> 
            Overdue Balance: $<? echo $j ?>    
        </p>
        <p class="clear">
            Last Payment: <? echo $i ?>    
        </p>
        <p class="clear">
            Payment Due By: <? echo $k ?>    
        </p>
        <p class="clear">
            For information on how to settle your account, click here.    
        </p> 
    </a>
    </div>
    Again, thanks for your help, and I'd welcome any comments on my solution, especially if you see anything that could be cleaned up.

    Cheers,

    K.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 02:10 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com