$row['id'] – undefined index
Question by kdot
I am new to PHP. I am practicing how to delete a row in mysql using PHP.
I have this code:
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("usersdb", $con);
echo "<table border='1'>
<tr>
<th>USERNAME</th>
<th>PASSWORD</th>
<th>ACTION</th>
</tr>";
$result = mysql_query("SELECT * FROM users");
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>";
echo '<a href="index.php?delete='.$row['id'].'">Delete</a></html>';
echo "</td>";
echo "</tr>";
}
echo "</table>";
if(isset($_GET['delete']) and is_numeric($_GET['delete']))
{
mysql_query("DELETE FROM Persons WHERE `id` = '".$_GET['delete']."'");
}
?>
When I tested it on the browser, it gives this error:
Notice: Undefined index: id in C:wampwwwindex.php on line 22
The error is pointing at the $row[‘id’]. I tried to defined it as $id = $row[‘id’] and replace $id on the line where $row[‘id’] is located. But in the end, I still get the same error. How can I solve this kind of error?
Answer by Starx
It means your data row, does not have a column named id
or it can’t find it for some reason.
Verify this, by doing a print_r($row)
inside the loop like this.
$result = mysql_query("SELECT * FROM users");
while($row = mysql_fetch_array($result))
{
print_r($row);
}