可能重複:
PHP: 「Notice: Undefined variable」 and 「Notice: Undefined index」公告:未定義的索引,PHP
我創建了一個HTML表單創建發票,但我發現了以下通知時我提交我的表單: 未定義的索引:在第38行添加/ public_html/Bicycle Store/create_invoice.php
我在綁定參數中缺少了一個參數並修復了它,但我提交時仍然收到通知。我檢查了php.net,但我沒有看到我的代碼違反了語法。以下是處理輸入數據的代碼的php部分:
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();
}
在此先感謝!
你的表單是否包含帶有'name ='add''的輸入?測試它是否設置。 'if(isset($ _ POST ['add']))' –
我也許會爲我們中的一些人突出顯示第38行,這裏有相當多的精神天賦缺乏:( – Rawkode
如果其中任何一個字段是複選框,如果複選框被選中,只能在$ _POST中,但不管怎樣,你都不應該假設這些字段是存在的,並且總是首先進行isset()測試。 –