Page 1 of 1
Undefined index error
Posted: Fri Nov 04, 2011 9:25 am
by Php Beginner
Hello there, I have a dynamic web page designed with php and mysql. I encountered some problem while moving my root file from (C:\wamp\www) to another PC. I am having the error while trying to preview my web pages locally. I have highlighted the line that causing the error.
My scripts as below:
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "frmaddlect1")) {
$insertSQL = sprintf("INSERT INTO lecturer (lect_id, lect_password, lect_name, lect_register, dept) VALUES (%s, %s, %s, %s, %s)",
GetSQLValueString($_POST['lect_id'], "text"),
GetSQLValueString($_POST['lect_password'], "text"),
GetSQLValueString($_POST['lect_name'], "text"),
GetSQLValueString($_POST['lect_register'], "text"),
GetSQLValueString($_POST['dept'], "text"));
mysql_select_db($database_connection, $connection);
$Result1 = mysql_query($insertSQL, $connection) or die(mysql_error());
}
mysql_select_db($database_connection, $connection);
$query_lecturer = "SELECT * FROM lecturer WHERE dept='$_REQUEST[dept]'";
$lecturer = mysql_query($query_lecturer, $connection) or die(mysql_error());
$row_lecturer = mysql_fetch_assoc($lecturer);
$totalRows_lecturer = mysql_num_rows($lecturer);
Re: Undefined index error
Posted: Fri Nov 04, 2011 9:38 am
by Celauran
First, it's a notice, not an error. It's telling you that you're trying to use a variable (in this case an array index) that may not have been set.
This will check if it has been set and, if not, set it to an empty string:
Code: Select all
$_REQUEST['dept'] = (isset($_REQUEST['dept'])) ? $_REQUEST['dept'] : '';
An important note: you're passing unvalidated, unescaped data directly into a query. This can result in all kinds of horrible things happening at the hands of a malicious user. Before passing it to the query, check that the variable contains a valid department name/id/whatever you're expecting it to contain. If it's meant to be an integer, cast is as such. If it's meant to be a string, escape it using mysql_real_escape_string at the very least.
Re: Undefined index error
Posted: Fri Nov 04, 2011 10:25 pm
by Php Beginner
Thanks, It solve my matter. There is another, I had fatal error for db_query on the line which I have highlighted. The error message is "Call to undefined function db_query() in C:\wamp\www\Mentor Mentee System\manage_dept.php"
<!-- Mentee List Title -->
<table width="818" border="0" align="center" cellpadding="0" cellspacing="0">
<tr align="center" bgcolor="#999999">
<td height="23"><strong style="font-size: 14px; color: #FFF;">Mentee List - <?php $lecturer_id=$_REQUEST['lect_id'];
$zzz = db_query("SELECT * FROM lecturer WHERE lect_id='$_REQUEST[lect_id]'");
if($zzz = mysql_fetch_assoc($zzz)){
echo $xxx=$zzz['lect_name'];}
?></strong></td>
</tr>
<tr>
<td height="32"></td>
</tr>
</table>
<!-- Mentee List Title -->
How should I deal with this Fatal Error?
Re: Undefined index error
Posted: Fri Nov 04, 2011 10:44 pm
by twinedev
Where is the function db_query() defined? Is it in another file that was supposed to be included?
Or perhaps you were following a tutorial that was written to work with different systems, and you are were supposed to replace db with the one you are using (mysql , sybase, mssql, etc).
-Greg
Re: Undefined index error
Posted: Sat Nov 05, 2011 12:22 am
by Php Beginner
Hi Greg, I manage to solve my problem with your advice. Many thanks and have a nice day!
Re: Undefined index error
Posted: Sat Nov 05, 2011 7:27 am
by Php Beginner
Hi there, I am having the problem with the "student_name" variable which I have defined. It gave me an undefined index notice as well when I try to preview my page locally. I have highlighted the line below.
Codes:
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "frmedituser")) {
$updateSQL = sprintf("UPDATE student SET student_name=%s, student_ic=%s, student_gender=%s, student_race=%s, student_religion=%s, student_address_c=%s, student_email=%s, `year`=%s, year_intake=%s, program=%s, student_contact=%s, student_address=%s, lect_id=%s, medical=%s, medical_name=%s, medical_relation=%s, medical_contact=%s, medical_address=%s, admin_id=%s, admin_update=%s WHERE student_id=%s",
GetSQLValueString($_POST['student_name'], "text"),
GetSQLValueString($_POST['student_ic'], "text"),
GetSQLValueString($_POST['student_gender'], "text")
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "frmstuconfirm")) {
$insertSQL = sprintf("INSERT INTO student (student_id, student_pass, student_update, student_name, `year`, year_intake, program, lect_id, admin_id, admin_update) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['student_id'], "int"),
GetSQLValueString($_POST['student_pass'], "text"),
GetSQLValueString($_POST['student_update'], "text"),
GetSQLValueString($_POST['student_name'], "text"),
GetSQLValueString($_POST['year'], "int"),
<tr bgcolor="#FFFFCC">
<td width="118" height="22"><strong> Name</strong>
<input name="id" type="hidden" id="id" value="student" />
<input name="student" type="hidden" id="student" value="confirm" /></td>
<td width="12" height="22"><strong>:</strong></td>
<td width="420" height="22">
<strong id="sprytextfield47">
<label>
<input style="color: #009; font-family: Verdana, Geneva, sans-serif; font-size: 11px;" name="student_name" type="text" id="student_name" value="<?php echo $_REQUEST['student_name']; ?>" size="35" maxlength="100" />
</label>
</strong>
<strong style="color: #F00; font-size: 10px;">*</strong><strong class="style27 style25" style="color: #999; font-size: 10px"> eg: Rahmah Binti Abu Bakar</strong></td>
</tr>
<tr bgcolor="#FFFFCC">
<td width="118" height="22"><strong> Name</strong></td>
<td width="12" height="22"><strong>:</strong></td>
<td width="420" height="22"><?php echo $student_name=$_REQUEST['student_name'];?></td>
Re: Undefined index error
Posted: Sat Nov 05, 2011 7:37 am
by Php Beginner
Hi there, I am having the problem with the "student_name" variable which I have defined. It gave me an undefined index notice as well when I try to preview my page locally. I have highlighted the line below.
Codes:
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "frmedituser")) {
$updateSQL = sprintf("UPDATE student SET student_name=%s, student_ic=%s, student_gender=%s, student_race=%s, student_religion=%s, student_address_c=%s, student_email=%s, `year`=%s, year_intake=%s, program=%s, student_contact=%s, student_address=%s, lect_id=%s, medical=%s, medical_name=%s, medical_relation=%s, medical_contact=%s, medical_address=%s, admin_id=%s, admin_update=%s WHERE student_id=%s",
GetSQLValueString($_POST['student_name'], "text"),
GetSQLValueString($_POST['student_ic'], "text"),
GetSQLValueString($_POST['student_gender'], "text")
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "frmstuconfirm")) {
$insertSQL = sprintf("INSERT INTO student (student_id, student_pass, student_update, student_name, `year`, year_intake, program, lect_id, admin_id, admin_update) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['student_id'], "int"),
GetSQLValueString($_POST['student_pass'], "text"),
GetSQLValueString($_POST['student_update'], "text"),
GetSQLValueString($_POST['student_name'], "text"),
GetSQLValueString($_POST['year'], "int"),
<tr bgcolor="#FFFFCC">
<td width="118" height="22"><strong> Name</strong>
<input name="id" type="hidden" id="id" value="student" />
<input name="student" type="hidden" id="student" value="confirm" /></td>
<td width="12" height="22"><strong>:</strong></td>
<td width="420" height="22">
<strong id="sprytextfield47">
<label>
<input style="color: #009; font-family: Verdana, Geneva, sans-serif; font-size: 11px;" name="student_name" type="text" id="student_name" value="<?php echo $_REQUEST['student_name']; ?>" size="35" maxlength="100" />
</label>
</strong>
<strong style="color: #F00; font-size: 10px;">*</strong><strong class="style27 style25" style="color: #999; font-size: 10px"> eg: Rahmah Binti Abu Bakar</strong></td>
</tr>
<tr bgcolor="#FFFFCC">
<td width="118" height="22"><strong> Name</strong></td>
<td width="12" height="22"><strong>:</strong></td>
<td width="420" height="22"><?php echo $student_name=$_REQUEST['student_name'];?></td>
Re: Undefined index error
Posted: Sat Nov 05, 2011 7:56 am
by internet-solution
Its the same issues as before. Please follow Celauran's advice and use isset to initialize variables. In this case it will be $_REQUEST['student_name'] instead of $_REQUEST['dept'].
Celauran wrote:First, it's a notice, not an error. It's telling you that you're trying to use a variable (in this case an array index) that may not have been set.
This will check if it has been set and, if not, set it to an empty string:
Code: Select all
$_REQUEST['dept'] = (isset($_REQUEST['dept'])) ? $_REQUEST['dept'] : '';
Re: Undefined index error
Posted: Sat Nov 05, 2011 8:24 pm
by Php Beginner
Many thanks for everyone who helped, I solved my problems for the moment.
