Jump to content

delete row in php


phpgoal

Recommended Posts

Hello,

 

Below code is working fine except deleting rows.

 

1. My code simply search mysql to select data.

2. Once data is found, it displays row by row.

Working fine so far.

 

3. I am trying to add a delete button next to each row. When i click Delete only that row gets deleted.

 

Would you provide any help?

 

 

 

Thanks.

echo "<table 
border='1'> <tr> <th>Date</th> <th>Income</th> 
<th>Expense</th> <th>MS</th> 
<th>MK</th><th>Delete</th> </tr>";while($row 
= mysql_fetch_array($result)){$myear = $row['monthyear'];$income = 
$row['income'];$expense = $row['expense'];$ms = $row['ms'];$mk = 
$row['mk'];
echo "<tr>";   echo "<td>" . $row['monthyear'] 
. "</td>";   echo "<td>" . $row['income'] . 
"</td>";      echo "<td>" . 
$row['expense'] . "</td>";   echo "<td>" . $row['ms'] 
. "</td>";      echo "<td>" . 
$row['mk'] . "</td>";       echo 
"<td>" . "<form name=\"kk\" method=\"post\" 
action=\"\"> <input type=\"submit\" value=\"Delete\" 
/></form>". "</td>";      echo 
"</tr>";    }
echo "</table>";
Link to comment
https://forums.phpfreaks.com/topic/279892-delete-row-in-php/
Share on other sites

Use hidden form input to pass your unique row ID which relevant to your deleted item with form submitting. Something like this 

<input type="hidden" name="delete-item" value="your id" />

Here you need to select your unique id from MySql table along with other table columns.

 

Then your code should be something similar to this.

 

NOTE: I assumed your unique row id as item_id (It should be change with your real ID)

echo "<table border='1'> 
		<tr> 
			<th>Date</th> 
			<th>Income</th> 
			<th>Expense</th> 
			<th>MS</th> 
			<th>MK</th>
			<th>Delete</th> 
		</tr>";

while($row = mysql_fetch_array($result)){
	$myear 	 = $row['monthyear'];
	$income	 = $row['income'];
	$expense = $row['expense'];
	$ms 	 = $row['ms'];
	$mk 	 = $row['mk'];
	$item_id = $row['item_id']; 
	
echo "<tr>
		<td>" . $row['monthyear'] . "</td>
		<td>" . $row['income'] . "</td>
		<td>" . $row['expense'] . "</td>
		<td>" . $row['ms'] . "</td>
		<td>" . $row['mk'] . "</td>
		<td>
			<form name='kk' method='post' action=''> 
				<input type='submit' value='Delete' />
				<input type='hidden' value='" . $item_id . "' name='item_id' />
			</form>
		</td>
	  </tr>";    
}
echo "</table>"; 
Link to comment
https://forums.phpfreaks.com/topic/279892-delete-row-in-php/#findComment-1439547
Share on other sites

Don't use GET except to retrieve data.  Use POST to modify data.  You can wrap your row output in the form and use a checkbox array with the value of the row unique id:

<form method="post">
<?php
if(isset($_POST['submit']) && isset($_POST['ids'])) {
   $ids = implode(',', $_POST['ids']);
   //DELETE FROM table_name WHERE id IN ($ids)
}
while($row = mysql_fetch_array($result)){
   echo 'row stuff... <input type="checkbox" name="ids[]" value="' . $row['id'] . '"><br />';
}
?>
<input type="submit" name="submit" value="submit">
</form>

There needs to be some security / sanitization added, this is just an example.  Look at mysqli or PDO.

 

Link to comment
https://forums.phpfreaks.com/topic/279892-delete-row-in-php/#findComment-1439548
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.