Help needed to delete the entry form the table format

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
raj86
Forum Commoner
Posts: 25
Joined: Tue Sep 29, 2009 12:28 am

Help needed to delete the entry form the table format

Post by raj86 »

Hello friends
i am showing data in table format in my application. i have used while loop to show that table. for every request i am showing a DELETE button so that once that request is over i can delete that particular entry from the database. peroblem is , i am not able to delete the entry ::::::: below is the code i m using

Code: Select all

$query = "SELECT * FROM `software`";
$result=mysql_query($query) or die("Query failed!");
if(mysql_num_rows($result))
{
	while($data=mysql_fetch_array($result,MYSQL_ASSOC)) //$line=mysql_fetch_array($paymajor,MYSQL_ASSOC)
	{
		$sno=$data['SNO'];
		$name=$data['NAME'];
		$id=$data['ID'];
		$hostel=$data['HOSTEL'];
		$software=$data['SOFTWARE'];
		$url=$data['URL'];
		$size=$data['SIZE'];
	if($count==1)
	{
?>
		<table width="300" border="2" align="center" cellpadding="5" style="border: solid 2px black;" >
			<tr bgcolor="#CCCCCC"><td colspan="10"><strong><center>BITS - PILANI, K.K. Birla GOA CAMPUS</center></strong></td></tr>
			<tr bgcolor="#CCCCCC"><td colspan="10"><strong><center>Details of Software Request</center></strong></td></tr>
			<tr bgcolor="#CCCCCC">
				<td><center><strong>SNO</strong></center></td>
				<td><center><strong>NAME</strong></center></td>
				<td><center><strong>ID</strong></center></td>
				<td><center><strong>HOSTEL</strong></center></td>
				<td><center><strong>SOFTWARE</strong></center></td>
				<td><center><strong>URL</strong></center></td>
				<td><center><strong>SIZE</strong></center></td>
				<td><center></center></td>
		    </tr>
<?php
	}
		if($sno>0)
		{
			echo '<tr><td><center>'.$sno.'</td></center>
			<td><center>'.$name.'</center></td>
			<td><center>'.$id.'</center></td>
			<td><center>'.$hostel.'</center></td>
			<td><center>'.$software.'</center></td>
			<td><center><a href="'.$url.'" target="_blank" >'.$url.'</a></center></td>
			<td><center>'.$size.'</center></td>
			<td><input type="submit" name="delete" value="Delete" ></td></tr>';
		}
		$count++;
	}
}
<?php
echo "here $sno";
if(isset($_POST['delete'])) 
{
	global $sno;
	$query="DELETE FROM software WHERE SNO='$sno'";
	mysql_query($query) or die ("Error in deleting table");
	echo"<center><br><br>software table record deleted successfully<center>";
}
?>
amargharat
Forum Commoner
Posts: 82
Joined: Wed Sep 16, 2009 2:43 am
Location: Mumbai, India
Contact:

Re: Help needed to delete the entry form the table format

Post by amargharat »

Instead of delete button use delete text as a link as below,

Code: Select all

<a href='same_page.php?action=delete&sno=2'>Delete</a>

php codes below

<?php
$sno = $_REQUEST["sno"];
$action = $_REQUEST["action"];
if(isset($id) && isset($action))
{
        $query="DELETE FROM software WHERE SNO='" . $sno . "'";
        mysql_query($query) or die ("Error in deleting table");
        echo"<center><br><br>software table record deleted successfully<center>";
}
?>
User avatar
iijb
Forum Commoner
Posts: 43
Joined: Wed Nov 26, 2008 11:34 pm

Re: Help needed to delete the entry form the table format

Post by iijb »

Hi
Check whether you are passing the $sno. I think you have to pass the $sno value. For this you can use a hidden value.
I usually do by this. Im not sure is it the better way but I get the result.

Code: Select all

echo '<form id="form1" method="post" action="">
    		<p>
    		<label>
      		<input type="submit" name="delete" value="Delete" >
    		</label>
    		</p>
     		<input type="hidden"  name="sno"value=" '.$sno.' " />
 </form>';

if(isset($_POST['delete'])) 
	{
              $query="DELETE FROM software WHERE SNO='.$_POST['sno'].' ";
               mysql_query($query) or die ("Error in deleting table");
              echo"<center><br><br>software table record deleted successfully<center>";
		
	}
Try this.
Regards
iijb
raj86
Forum Commoner
Posts: 25
Joined: Tue Sep 29, 2009 12:28 am

Re: Help needed to delete the entry form the table format

Post by raj86 »

iijb........i tried your solution but still i am getting the last vale of SNO and the very last record is deleted.
i do`nt know how to retain the value of $sno when while is used. any suggestions are welcome
amargharat
Forum Commoner
Posts: 82
Joined: Wed Sep 16, 2009 2:43 am
Location: Mumbai, India
Contact:

Re: Help needed to delete the entry form the table format

Post by amargharat »

You are getting last sno. as you must be using only one form.
instead use separate forms for each hidden field and delete button.

what happening current scenario is, when you submit delete button, all the hidden values get submitted with same name, so that php stores last value .
raj86
Forum Commoner
Posts: 25
Joined: Tue Sep 29, 2009 12:28 am

Re: Help needed to delete the entry form the table format

Post by raj86 »

thank you amargharat and iijb................i forgot to add <form> tag ........it is working now
Post Reply