我有兩個php問件
其中
add.php:
action.php:
switch($_GET['action']){ case "add": $name = $_POST['name']; $sex = $_POST['sex']; $arge = $_POST['arge']; $classId = $_POST['classId']; $sql = "insert into students VALUES (NULL ,'{$name}','{$sex}','{$arge}','{$classId}')"; $rw = $pdo->exec($sql); if($rw>0){ echo ""; }else{ echo ""; } break;}
提交資料時為什麼會報如下錯誤:
Notice: Undefined index: name in /Applications/XAMPP/xamppfiles/htdocs/studentsManager/action.php on line 16Notice: Undefined index: sex in /Applications/XAMPP/xamppfiles/htdocs/studentsManager/action.php on line 17Notice: Undefined index: arge in /Applications/XAMPP/xamppfiles/htdocs/studentsManager/action.php on line 18Notice: Undefined index: classId in /Applications/XAMPP/xamppfiles/htdocs/studentsManager/action.php on line 19
name sex arge classId 不是都有麼 為何還時 未定義??
回複內容:
我有兩個php問件
其中
add.php:
action.php:
switch($_GET['action']){ case "add": $name = $_POST['name']; $sex = $_POST['sex']; $arge = $_POST['arge']; $classId = $_POST['classId']; $sql = "insert into students VALUES (NULL ,'{$name}','{$sex}','{$arge}','{$classId}')"; $rw = $pdo->exec($sql); if($rw>0){ echo ""; }else{ echo ""; } break;}
提交資料時為什麼會報如下錯誤:
Notice: Undefined index: name in /Applications/XAMPP/xamppfiles/htdocs/studentsManager/action.php on line 16Notice: Undefined index: sex in /Applications/XAMPP/xamppfiles/htdocs/studentsManager/action.php on line 17Notice: Undefined index: arge in /Applications/XAMPP/xamppfiles/htdocs/studentsManager/action.php on line 18Notice: Undefined index: classId in /Applications/XAMPP/xamppfiles/htdocs/studentsManager/action.php on line 19
name sex arge classId 不是都有麼 為何還時 未定義??
你後面的錯誤是你 單獨 在地址欄中開啟 你的 action.php?action=add
的時候顯示的吧?
當你單獨在瀏覽器中直接開啟你的action.php?action=add
的時候, 它是以GET的方式請求的, 而且不會有POST
的資料過去, 所以你這個時候, 去取$_POST['xxx']
PHP 會有一個 Notice
的提示(在允許顯示的情況下).
正常從你的 add.php
點按鈕(有填寫頁資料的情況下), 是沒有報你說的那個問題的.
form 沒有設定encoding 屬性。
encoding='application/x-www-form-urlencoded'
這樣吧。你先var_dump($_POST);