Reputation: 174
<td>
<form action="del.php" method="POST">
<?php foreach($jk as $item) {
echo"<input type='hidden' name='id' value='".$item['emc_id']."'>";
}
?>
<input type="submit" name="downld" value="DELETE" >
</form>
</td>
As you can see that I am passing a value using hidden but in del.php. I am getting a 4. I have 4 data in my database but when I click rowid-1
rowid-2
and any other id I am always getting a id-4. I want whenever I click on DELETE button the respective id should be deleted.
Upvotes: 2
Views: 2602
Reputation: 174
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel">
<html>
<head>
WELCOME <?=$_SESSION['user']?></br>
<a href="logout.php" >log-out</a></br>
<title> emc promo 1</title>
<meta http-equiv="Content-type" content="text/html;charset=utf-8" />
</head>
<body>
<b>the total no rows are :</b><?=$g?>
<table cellpadding="5" border="1" style="width:450px;"><tr>
<?php for ($i=0; $i < count($colnames); $i++) { ?>
<td><?=$colnames[$i]?></td>
<?php }?>
<td>DELETE</td>
<td>EDIT</td>
</tr>
<?php for ($i=0; $i < count($jk); $i++) { ?>
<tr>
<?php for ($ii=0; $ii < count($colnames); $ii++) {?>
<td><?=$jk[$i][$colnames[$ii]]?></td>
<?php }?>
<td><form action="del.php" method="POST">
<input type='hidden' name='id' value= <?php echo $jk[$i];?>>
<input type="submit" name="downld" value="DELETE" >
</form></td>
<td><form action="edit.php" method="POST">
<input type="hidden" name="id1" value="<?=$jk['$i'];?>">
<input type="submit" name="downld" value="UPDATE " >
</form></td>
</tr>
<?php } ?>
</table>
</body>
</html>
I got my answer , Now i am passing all the column values so that i can check at the time of delete and edit
Upvotes: 2
Reputation: 2613
What exactly you want to achieve? You want to delete one row at a time or multiple rows? What you are doing right now is creating the hidden boxes dynamically however NAME of the hidden field is same, So when browser renders it will create four text boxes like:: --
<input type="hidden" name="id" value="1" />
<input type="hidden" name="id" value="2" />
<input type="hidden" name="id" value="3" />
<input type="hidden" name="id" value="4" />
Now, when you will post the form you will get the last value. so you will always get 4. If you are trying to delete single record then use the approach paw has suggested. If you want to delete multiple records use checkboxes infront of every row and makes sure you use array like notation for checkboxes name like name="foo[]"
Upvotes: 1
Reputation: 2057
you should attach an id in your row like this: Inside your foreach
echo "<a href="delete.php?id=". $row['id']>Delete</a>"
whenever the user clicks the link delete the id will be passed into a php script which there you will $_GET
the id
passed and have a delete query
using the id
I hope this helps.
Upvotes: 0