嗨,我完全用PHP初學者。所以我正在嘗試這個爲我的學校項目。而我看到未定義的索引:在第49行C: xampp htdocs assignment.php中的n1
未定義指數:用C N1:\ XAMPP \ htdocs中\ assignment.php上線49
真的不知道問題出在哪裏。
<html>
<body style="color:Midnightblue;"bgcolor=#eee8aa>
<form action="index.php" method="post">
<h1>Mobile Shop Management</h1>
<font size='16'><i>Type your Name:</i></font> <input type="text" name="n1" size="10" style="background-color: lightblue;
display: inline-block;
margin-right: 20px;
width: 380px;
height: 50px;
border-radius: 0%;
margin-left: 72px;
text-align: center;
font-size: 2em;"><br><br>
<font size='16'><i>Type your Budget:</i></font> <input type="text" name="n2" size="10" style="background-color:lightblue;
display: inline-block;
margin-right: 20px;
width: 180px;
height: 50px;
border-radius: 0%;
margin-left: 50px;
text-align: center;
font-size: 2em;"><br><br>
<input type="submit" size="10" value="Show" style="background-color:lightblue;
box-shadow: 5px 5px 5px #888;
display: inline-block;
margin-right: 20px;
width: 280px;
height: 50px;
border-radius: 0%;
margin-left: 230px;
color: #CC0000;
text-align: center;
font-weight: bold;
font-size: 2em;
border: 4px solid #008800;">
</form>
</body>
</html>
<?php
if ($_POST['n1']<>""||$_POST['n2']<>"")
{
$x=$_POST['n1'];
$y=$_POST['n2'];
$c=mysql_connect("localhost","root","");
mysql_select_db('mis410');
$q=mysql_query("select Mobile_brand from mobile_shop where price<=$y");
echo "Hello mr./ms. $x <br> Available devices: $q";
mysql_close($c);
}
?>
'mysql_ *'函數在PHP 5.5棄用(和**在PHP 7完全刪除**),你應該[停止使用它們(HTTP:/ /stackoverflow.com/q/12859942)如果可以的話。 你應該選擇另一個允許你使用[準備語句](http://stackoverflow.com/q/60174/)的API(當處理變量時你應該*),比如'mysqli_ *'或者PDO - 請參閱[選擇API](http://php.net/manual/en/mysqlinfo.api.choosing.php)。 – Qirel
您應該檢查表單是否完全提交。第一次加載表單時,'$ _POST'中沒有任何值。 – Shadow
這個錯誤來自於'if($ _POST ['n1'] <>「」|| $ _POST ['n2'] <> 「」)' - 表單未提交時,這些索引不存在。你也可以使用本地的'empty()'函數進行比較,這也將在單次掃描中清除這個錯誤,使其成爲'if(!empty($ _ POST ['n1'])||!empty($ _POST ['n2'])){'(儘管我懷疑你可能想用AND'&&而不是OR'||'?) – Qirel