undefined offset error in php
I don't know why but the code below is working when I have a different query:
$result = mysql_query("SELECT * FROM student WHERE IDNO='".$_GET['id']."'")
?>
<?php while ( $row = mysql_fetch_array($result) ) {
?>
<?p开发者_如何学编程hp list($year,$month,$day)=explode("-", $row['BIRTHDAY']); ?>
<tr>
<td width="30" height="35"><font size="2">Month:</td>
<td width="30"><input name="mm" type="text" id="mm" onkeypress="return handleEnter(this, event)" value="<?php echo $month;?>">
<td width="30" height="35"><font size="2">Day:</td>
<td width="30"><input name="dd" type="text" id="dd" maxlength="25" onkeypress="return handleEnter(this, event)" value="<?php echo $day;?>">
<td width="30" height="35"><font size="2">Year:</td>
<td width="30"><input name="yyyy" type="text" id="yyyy" maxlength="25" onkeypress="return handleEnter(this, event)" value="<?php echo $year;?>">
And it works when this is my query:
$idnum = mysql_real_escape_string($_POST['idnum']);
mysql_select_db("school", $con);
$result = mysql_query("SELECT * FROM student WHERE IDNO='$idnum'");
Please help, why do I get the undefined offset error when I use this query:
$result = mysql_query("SELECT * FROM student WHERE IDNO='".$_GET['id']."'")
I assume that the query is the problem because its the only thing that's different between the two.
You are missing a semi-colon ;
there:
$result = mysql_query("SELECT * FROM student WHERE IDNO='".$_GET['id']."'") ;?>
Also make sure that you are getting the id:
var_dump($_GET);
And it is named really id
or something else.
You might go like:
if ($_GET['id'])
{
// your further code.......
}
.
Update Based On Comment
Make sure that the date is coming up fine, try this:
print_r($row['BIRTHDAY']);
and see if everything is coming up or it is empty.
Are you showing all errors?
Check that there is an index called 'id' within your $_GET array.
Is $_GET['id']
defined? In your first query which supposedly works, you're using $_POST['idnum']
which is a different request method and different key. Undefined offset is a non-fatal error/warning telling you that the key doesn't exist in the array. An easy way to fix this is to change the PHP error reporting, although in thise case you need to figure out which variable you actually need to use.
精彩评论