Jump to content

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

I'd propably create a link with ?id=$row['id'](or your unique row) and just then in that link delete it.. or just redirect back with a get param to the same file and if isset..

 

EDIT.

or.. you could use js + ajax to send data to an independent file to delete it :)

Edited by _EmilsM
Link to comment
https://forums.phpfreaks.com/topic/279892-delete-row-in-php/#findComment-1439527
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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.