我收到未定义的索引错误 [英] I am getting an undefined index error
问题描述
我遇到这些错误:
注意:未定义的索引:第13行/Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php中的名字
Notice: Undefined index: first_name in /Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php on line 13
注意:未定义的索引:第14行/Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php中的姓氏
Notice: Undefined index: last_name in /Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php on line 14
注意:未定义的索引:第15行上的/Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php中的年龄
Notice: Undefined index: age in /Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php on line 15
通知:未定义的索引:在第16行的/Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php中移动
Notice: Undefined index: mobile in /Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php on line 16
通知:未定义的索引:第17行/Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php中的电子邮件
Notice: Undefined index: email in /Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php on line 17
通知:未定义的索引:第18行/Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php中的地址
Notice: Undefined index: address in /Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php on line 18
注意:未定义的索引:第19行/Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php中的club_role错误:键"PRIMARY"的条目"重复
Notice: Undefined index: club_role in /Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php on line 19 Error: Duplicate entry '' for key 'PRIMARY'
这是我的代码:`
<h1> Add a new student</h1>
<p>Add a new student to the system using the form below</p>
<?php
/*information posted from booking form*/
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$age=$_POST['age'];
$mobile=$_POST['mobile'];
$email=$_POST['email'];
$address=$_POST['address'];
$club_role=$_POST['club_role'];
/*connect to DBMS*/
$con=mysql_connect("localhost", "root", "");
if (!$con)
{
die ('Could not connect: ' .mysql_error());
}
/*connect to Database*/
mysql_select_db("91368_jurgen_glee",$con);
/*insert into records*/
$sql="INSERT INTO glee (`first_name`, `last_name`, `age`, `mobile`, `email`, `address`, `club_role`)
VALUES ('$first_name', '$last_name', '$age', '$mobile', '$email', '$address', '$club_role')";
mysql_Query($sql, $con) or die ('Error: '.mysql_error());
$result=mysql_query("SELECT *
WHERE `first_name` = '$first_name'
AND `last_name` = '$last_name'
AND `age` = '$age'
AND `mobile` = '$mobile'
AND `email` = '$email'
AND `address` = '$address'
AND `club_role` = $club_role'");
while ($row = mysql_fetch_array($reslut))
/*close DBMS Connection*/
mysql_close($con);
?>
`这是html代码:
<div id="body">
<h1> Add a new student</h1>
<p>Add a new student to the system using the form below</p>
<form name="add_student" action="add_student.php">
<table class="add_student">
<tr>
<td class="form">
<b>First Name</b></br>
<input type="text" name="first_name" size="30" value="">
</td>
<td class="form">
<b>Last Name</b></br>
<input type="text" name="last_name" size="30" value="">
</td>
</tr>
<tr>
<td class="form">
<b>Mobile</b></br>
<input type="text" size="10" name="mobile" min="1" max="10" value="">
</td>
<td class="form">
<b>Email</b></br>
<input type="text" name="email" size="15" value="">
</td>
<td class="form">
<b>Age</b></br>
<input type="number" name="age" size="2" min="1" max="30" value="">
</td>
</tr>
<td class="form">
<br>
</h1><b>Address</b></h1>
<select name="address">
<option value="01">Please select one</option>
<option value="02">Red Beach</option>
<option value="03">Orewa</option>
<option value="04">Silverdale</option>
<option value="05">Dairy Flat</option>
</select>
</td>
<br>
<td class="form">
<br>
</h1><b>Club Role</b></h1>
<select name="club-role">
<option value="01">Please select one</option>
<option value="02">Lead</option>
<option value="03">Backstage</option>
<option value="04">Chorus</option>
<option value="05">Orchestra</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" class="submit">
<br>
<input type="submit" value="SUBMIT">
</td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
`
推荐答案
您会收到此通知,因为数组 $ _ POST
不像其他数组那样具有 first_name
键.仅当您发出 POST请求
(例如提交表单或进行AJAX调用)时,它们才会出现.
You get this notice because array $_POST
does not have first_name
key like another ones. They are going to appeared only when you make POST request
like submit the form or make AJAX call.
在您的代码段中,我可以看到您有一个* .php文件用于 GET
方法和 POST
方法.为避免这种情况,您可以制作两个不同的文件: indexPost.php
和 index.php
并更改HTML代码:
In your snippet I can see you have one *.php file for GET
method and POST
method. To avoid this situation you can make two different files: indexPost.php
and index.php
and change HTML code:
<form action="indexPost.php" method="POST">
或为您的密钥设定条件:
or make conditions to your keys:
<?php
if (isset($_POST['first_name']) && !empty($_POST['first_name'])) {
// Your code
}
?>
每个键都需要自己的条件语句.
Each key needs own conditional statement.
如果我对一个* .php文件有误.
If I'm wrong about one *.php file.
使用条件语句(请参见上面的代码)删除所有列出的通知.您也可以对其进行改进以验证表单和mysql查询.
Using conditional statements (look on code above) remove all listed notices. Also you can improve it to validate form and mysql query.
您的一列具有主键,该主键不能在一个表中重复.假设 last_name
列具有主键.然后,在整个表中, last_name
列中只能有一行值为 LoremIpsum
的行.
One of your column has primary key which can't be duplicated in one table. Let's say the column last_name
has primary key. Then in whole table you can have only one row with value LoremIpsum
in last_name
column.
这篇关于我收到未定义的索引错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!