Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2012
    Posts
    4

    Unanswered: Notice: undefined index

    Hi everyone, I'm new here. I have some experience with php/mysql/relational databases. I'm working on a hobby project that is not work or school related and have encountered a notice I haven't had before. I'm getting a notice of an undefined index, that is confusing me because I use the same code for my other databases and I don't have the problem. I've been working on this problem for several hours and I'm wondering if it's a database problem. But first I want to make sure I'm not missing anything simple in my php code. The webpage is formatted for html 5, but I doubt that's the problem.

    Notice: Undefined index: amount in /home2/htmlprac/public_html/php/traffic_light/traffic_light_script.php on line 30

    $amount at the very bottom of the script should show 33.

    The code below needs to access the database to see how many vehicles are at an intersection. I wrote the code and setup the database and have checked the spelling of the table and column names multiple times.

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    <title>Traffic Light Script</title>
    <link rel="stylesheet" href="style.css" />
    </head>
    <body>
    <?
    ini_set('error_reporting', E_ALL);
    ini_set('display_errors', 'On');
    echo "<h1>Traffic light sequence program</h1>";
    $database_address = 'localhost';
    $username = '*********';
    $password = '*********';
    $database_name = 'htmlprac_traffic_light';
    $connect = mysql_connect($database_address, $username, $password) or die ("Cannot connect to server: ".mysql_error());
    mysql_select_db($database_name) or die("Could not select database: ".mysql_error());
    /* Access database to check number of cars at intersection */
    $sql = "SELECT vehicles.vehicle_quantity FROM vehicles WHERE vehicles.vehicle_id = 1";
    $result = mysql_query($sql, $connect) or die(mysql_error()); // Did we get a result?
    $record = mysql_fetch_array($result);
    $amount = htmlentities($record['amount']);
    echo "$amount";
    ?>
    </body>
    </html>
    When I run the above select statement directly in phpmyadmin it works fine. It doesn't work in a webpage.
    Code:
    <?
    $database_address = 'localhost';
    $username = '********';
    $password = '********';
    $database_name = 'htmlprac_traffic_light';
    $connect = mysql_connect($database_address, $username, $password) or die ("Cannot connect to server: ".mysql_error());
    mysql_select_db($database_name) or die("Could not select database: ".mysql_error());
    ini_set('error_reporting', E_ALL);
    ini_set('display_errors', 'On');
    //echo $connect; // Used to test connection
    $sql = "SELECT SUM(vehicle_quantity) AS vehicle_total FROM vehicles"; // Need AS purchase_total in the select statment
    $result = mysql_query($sql, $connect) or die(mysql_error()); // Did we get a result?
    $record = mysql_fetch_array($result);
    $vehicle_total = htmlentities($record['vehicle_total']);
    echo "$vehicle_total";
    ?>
    This code works fine and the total amount of cars is 33, which is what I get. It works in a webpage.

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    is the column 'amount' in your select statement
    if not thats your issue
    PHP returns an associative array indexed on column name
    if the column or alias isn't in the select then SQL (and i can't see it there) then the error message is telling you that the associative array doenst' have an entry for the key index 'amount'
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Sep 2012
    Posts
    4
    Quote Originally Posted by healdem View Post
    is the column 'amount' in your select statement
    Dam it! I can't believe I did that. I must have had a brain fart when I was writing this code. Amount was suppose to be vehicle_quantity

    It works now, thanks a ton for the help.
    Last edited by 2161; 09-23-12 at 20:08.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •