1

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

I've created a form in html to create an invoice, but I'm getting the following notice when I submit my form: Undefined index: add in /public_html/Bicycle Store/create_invoice.php on line 38

I had a missing parameter in my binding parameters and fixed it, but I'm still getting the notice when I submit. I checked php.net, but I'm not seeing where my code violates the syntax. Here's the php section of code that processes the inputted data:

if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

if( $_POST["add"] ) {

if (!($stmt = $mysqli->prepare("INSERT INTO INVOICE VALUES (?,?,?,?,?,?,?,?,?)"))) {
        echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

if (!$stmt->bind_param("sssissddd", $_POST['fname'], $_POST['lname'], $_POST['phone'], $_POST['date'], $_POST['invoice_num'], $_POST['item_num'], $_POST['price'], $_POST['discount'], $_POST['total'])) {
    echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}

if (!$stmt->execute()) {
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}

else {
    printf("%d row inserted.<br/>", $stmt->affected_rows);
}

$stmt->close();
}

Thanks in advance!

Community
  • 1
  • 1
user1852050
  • 655
  • 2
  • 13
  • 17

1 Answers1

3

Replace

if( $_POST["add"] ) {

with

if( isset($_POST["add"]) /* and other condition on $_POSt["add"] if necessary */) {

Problem is that you are accessing "add" key in your post array and it is not there.

jakub.petr
  • 2,951
  • 2
  • 23
  • 34