0

I wrote this code called delete.php, i have another script called view.php which displays all the users in the database and displays a link to delete that particular record using the get method.

In the delete.php file it retrieves the id through get method from the view.php file and deletes accordingly. code of my files and the error is below, please help.

delete.php

<?php
include("header.html");
echo "<h1>Delete User</h1>";
$id="";
if((isset($_GET['id']))&&(is_numeric($_GET['id'])))
{
	$id=$_GET['id'];
}
  elseif(((isset($_POST['id']))&&is_numeric($_POST['id'])))
  {
	  $id=$_GET['id'];
  }
     else
	 {
		 echo '<p>This page has been accessed in error</p>';
		 include("footer.html");
		 exit();
	 }
require("connect.php");
if(isset($_POST['submitted']))
{
	if($_POST['sure']=="Yes")
	{
		$q = "DELETE FROM users WHERE user_id=$id LIMIT 1";
		$r=mysql_query($q,$dbc);
		if(mysql_affected_rows($dbc)==1)
         {
	        echo "<p>The user has been deleted</p>";
			include("footer.html");
         }
           else
             {
	            echo "<p>The user could not be deleted due to a system error</p>";
	            echo "<p>".mysql_error($dbc)."<br />Query : ".$q."</p>";
                include("footer.html");
			 }
			 
	}
	else
	{
		echo "<p>The user has not been deleted</p>";
		include("footer.html");
	}
}
else
{
	$q="SELECT CONCAT(first_name, ' ',last_name) FROM users WHERE user_id=$id";
	$r=mysql_query($q,$dbc);
	if(mysql_num_rows($r)==1)
	{
		$row=mysql_fetch_array($r,MYSQL_NUM);
		echo '<form action="delete.php" method="post">'.
		'<h3>Name : '.$row[0].'</h3><br />'.'<p>Are you sure you want to delete this user'.
		'<input type="radio" name="sure" value="Yes" />Yes'.'<input type="radio" name="sure" value="no" checked="checked" />No</p><br /><br />'.
		'<input type="submit" name="submit" value="Submit" />'.
		'<input type="hidden" name="submitted" value="TRUE" /><input type="hidden" name="id" value="'.$id.'" />';
		echo '</form>';
	}
	  else
	  {
		  echo "<p>This page has been accessed in error</p>";
	  }
	mysql_close($dbc);
	include("footer.html"); 
}
?>

view.php

<?php
$page_title="View Users";
include("header.html");
include("nav.html");
?>
<div id="content">
<?php
$num=
require_once("connect.php");
$q="SELECT user_id, first_name, last_name FROM users";
$r=mysql_query($q,$dbc);
$num=mysql_num_rows($r);
echo "<center>Total no. of users registered : ".$num."<br /><br /></center>";
if($r)
{
	echo '<table align="center" border="2" cellspacing="3" cellpadding="3" width="100%">
	     <tr><td align="left"><b>Edit</b></td>
		 <td align="left"><b>Delete</b></td>
		 <td align="left"><b>ID</b></td>
		 <td align="left"><b>First Name</b></td>
		 <td align="Left"><b>Last Name</b></td>
		 </tr>';
		 while($row=mysql_fetch_array($r,MYSQL_ASSOC))
		 {
			 echo '<tr> 
			 <td align="left"><a href="edit_user.php?id=' . $row['user_id'] . '">Edit</a></td>
			 <td align="left"><a href="delete_user.php?id=' . $row['user_id'] . '">Delete</a></td>'.
			 '<td align="center">'.$row['user_id'].'</td>'.
			 '<td align="left">'.$row['first_name'].'</td>'.
			 '<td align="left">'.$row['last_name'].'</td>'.
			 '</tr>';
		 }
	echo '</table>';
}
mysql_free_result($r);			 
?>
</div>
<?php
include("footer.html");
?>

error i am getting :-

Notice: Undefined index: id in C:\xampp\htdocs\delete.php on line 11

The user could not be deleted due to a system error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1
Query : DELETE FROM users WHERE user_id= LIMIT 1
2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by dhruv_arora
0

Change line 11. from

$id = $_GET['id'];

to

$id = $_POST['id'];

aah, my bad, silly me.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.