Reputation: 29
<?php
mysql_connect("localhost","root","");
mysql_select_db("sample") or die("database could not connect ");
?>
<html>
<head>
<meta name="description" content="Php Code for View, Search, Edit and Delete
Record" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>View Student Record</title>
</head>
<body>
<center><h1><u>Student Database</u></h1></center>
<?php
$roll=$_GET["roll"];
$query="select * from student where roll='$roll'";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result)) {
}
?>
<table style=" border:1px solid silver" cellpadding="5px" cellspacing="0px"
align="center" border="1">
<tr>
<td colspan="4" style="background:#0066FF; color:#FFFFFF; font-size:20px">VIEW
STUDENT DATABASE</td></tr>
<tr>
<td> Roll Number</td><td><? echo $row[0];?></td>
<td> Class</td><td><? echo $row[1];?></td>
</tr>
<tr>
<td> Name of Student</td><td><? echo $row[2];?></td>
</tr>
<tr>
<td>Sex</td><td><? echo $row[3];?></td>
<td>Address1</td><td><? echo $row[4];?></td></tr>
<tr>
<td>Address2</td><td><? echo $row[5];?></td>
<td>Address3</td><td><? echo $row[6];?></td></tr>
<tr>
<td>Remarks</td><td><? echo $row[7];?></td></tr>
<tr>
</table>
<p align="center"><a href="index.php">Go Back to Home</a></p>
</body>
</html>
that is my code
<?php
$roll=$_GET["roll"];
$query="select * from student where roll='$roll'";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result)) {
}
?>
Here's my problem. I don't know how to get rid of this error:
Notice: Undefined index: roll in C:\xampp\htdocs\sample\view.php on line 16
Line 16 is $roll=$_GET["roll"];
.
This code should display the information in each row, but as I run it, it shows nothing.
Upvotes: 1
Views: 1576
Reputation: 71918
Make sure you pass roll
on the querystring. Without that, none of your PHP code makes sense. You're getting data from MySql based on $roll
and trying to display it; if there's no value on that variable, there's no data. So add isset
as Phil suggested, and dot run any PHP if it's not set.
Also, get rid of the while
loop, and only execute $row = mysql_fetch_array($result)
once, and only if isset($_GET['roll'])
. Otherwise, you'll try to echo false
later on your code.
Upvotes: 1
Reputation: 345
Place this code below $roll=$_GET["roll"];
if (isset($_GET["roll"))
{
// IF TRUE, DO THE QUERY ECHO YOUR RESULT
}
?>
Upvotes: 0
Reputation: 164760
Your issue is an undefined $_GET
index. Use isset()
, eg
$roll = isset($_GET['roll']) ? $_GET['roll'] : null; // or a suitable default
Try something like this to fix your query loop
$result=mysql_query($query);
?>
<table style=" border:1px solid silver" cellpadding="5px" cellspacing="0px" align="center" border="1">
<tr>
<td colspan="4" style="background:#0066FF; color:#FFFFFF; font-size:20px">VIEW
STUDENT DATABASE</td></tr>
<?php while ($row = mysql_fetch_array($result)) : ?>
<!-- rows, cells and echos, etc -->
<?php endwhile ?>
</table>
And finally, the MySQL extension is now deprecated. I strongly recommend you switch to PDO. Using PDOStatement::bindParam()
would also solve your SQL injection vulnerability in mysql_query()
.
Upvotes: 1