Notice: Undefined index
Results 1 to 2 of 2

Thread: Notice: Undefined index

  1. #1
    Member
    Join Date
    Dec 2005
    Posts
    51

    Notice: Undefined index

    Hi guys,
    I was wondering if anyone had any idea why im getting two errors in my code? i've been trying to do this in pdo and am not very good at it any help would be great thanks.(sorry for the messy code)
    ERRORS:
    Code:
    Notice: Undefined index: username in C:\xampp\htdocs\test.php on line 22
    line 22>>(':username' => $_POST['username'])
    Notice: Undefined variable: delete in C:\xampp\htdocs\test.php on line 68
    line 68>>(if($delete){)
    PHP Code:
    <?php
    require("common.php");
    if(empty(
    $_SESSION['user']))
    {
    header("Location: login.php");
    die(
    "Redirecting to login.php");
    }
    $query "
    SELECT
    id,
    username,
    invoicenumber,
    package,
    purchasedate,
    duedate,
    invoiceamount
    FROM users
    WHERE
    username = :username
    "
    ;
    $query_params = array(
    ':username' => $_POST['username']
    );
    try
    {
    $stmt $db->prepare($query);
    $result $stmt->execute($query_params);
    }
    catch(
    PDOException $ex)
    {
    die(
    "Failed to run query: " $ex->getMessage());
    }
    $row $stmt->fetch();
    ?>
    <table width="938" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <td><form name="form1" method="post" action="">
    <table width="938" border="0" cellpadding="3" cellspacing="1" bgcolor="#000000">
    <tr>
    <td align="left" bgcolor="#3576B4" width="26"><font color="#FFFFFF" size="2"><strong></strong></td>
    <td align="left" bgcolor="#3576B4" width="95"><font color="#FFFFFF" size="2"><strong>Invoice:</strong></font></td>
    <td align="left" bgcolor="#3576B4" width="102"><font color="#FFFFFF" size="2"><strong>Package:</strong></font></td>
    <td align="left" bgcolor="#3576B4" width="271"><font color="#FFFFFF" size="2"><strong>Purchase Date:</strong></font></td>
    <td align="left" bgcolor="#3576B4" width="79"><font color="#FFFFFF" size="2"><strong>Due Date:</strong></font></td>
    <td align="left" bgcolor="#3576B4" width="79"><font color="#FFFFFF" size="2"><strong>Invoice Amount:</strong></font></td>
    </tr>
    <?php
    while($rows=$stmt->fetch($result)){
    ?>
    <tr>
    <td align="center" bgcolor="#f8f8f8" width="26">
    <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo htmlentities($rows['id']); ?>" style="float: left"></td>
    <td bgcolor="#f8f8f8" width="95"><font size="2"><?php echo htmlentities($rows['invoicenumber'], ENT_QUOTES'UTF-8'); ?></font></td>
    <td bgcolor="#f8f8f8" width="102"><font size="2"><?php echo htmlentities($rows['package'], ENT_QUOTES'UTF-8'); ?></font></td>
    <td bgcolor="#f8f8f8" width="271"><font size="2"><?php echo htmlentities($rows['purchasedate'], ENT_QUOTES'UTF-8'); ?></font></td>
    <td bgcolor="#f8f8f8" width="240"><font size="2"><?php echo htmlentities($rows['duedate'], ENT_QUOTES'UTF-8'); ?></font></td>
    <td bgcolor="#f8f8f8" width="240"><font size="2"><?php echo htmlentities($rows['invoiceamount'], ENT_QUOTES'UTF-8'); ?></font></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="6" align="center" bgcolor="#f8f8f8">
    <input name="delete" type="submit" id="delete" value="Delete" style="float: left"></td>
    </tr><?php


    if($delete){
    for(
    $i=0;$i<$count;$i++){
    $del_id $checkbox[$i];
    $query "
    DELETE FROM
    users
    WHERE
    id = :
    $del_id And
    username = :username
    "
    ;
    $query_params = array(
    ':checkbox' => $_POST['checkbox'],
    ':delete'   => $_POST['delete'],
    ':username' => $_POST['username']
    );
    try
    {
    $stmt $db->prepare($query);
    $result $stmt->execute($query_params);
    }
       catch(
    PDOException $ex)
       {
       die(
    "Failed to run query: " $ex->getMessage());
      }
     }
    }

    ?></table>
    </form>
    </td>
    </tr>
    </table>
    </div>
    Last edited by fullyloaded; 05-03-2013 at 11:24 PM.

  2. #2
    PHP Witch laserlight's Avatar
    Join Date
    Apr 2003
    Location
    Singapore
    Posts
    13,537
    Is the user coming to this page by a form submission made via the POST method? Does this form have a field named "username"? If the answer to either of these questions is "no", then that explains why you get the first notice.

    The second notice should be self-explanatory: you used $delete before assigning anything to it.
    Use Bazaar for your version control system
    Read the PHP Spellbook
    Learn How To Ask Questions The Smart Way

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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