Jump to content

[SOLVED] Link to delete mysql row


jgmaddle

Recommended Posts

I am writing a simple script to insert data into mysql via an HTML form and then display it in an HTML table. That part of the script is working perfectly. What I want to do now is add a link or button next to each row in the HTML table that will let me delete that specific row.

 

I know that to accomplish I need to pass the id of the row to be deleted to a varible that can be used in a query string (this is not a something I will be putting online, so I'm not worried about the security of using a query string). The problem is that I don't have a clue how to actually implement this in my code. I have tried a few different ways, and so far nothing has worked.

 

Here is the code of the page I'm trying to implement this on:

 

<?php

mysql_connect('localhost', 'username', 'password');
mysql_select_db('network') or die ('Unable to connect to database: ' . mysql_error());
$query="SELECT * FROM ipaddrs";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

?>

<table border="0" cellspacing="5" cellpadding="5">
<tr>
<th><font face="Arial, Helvetica, sans-serif">ID</font></th>
<th><font face="Arial, Helvetica, sans-serif">IP Address</font></th>
<th><font face="Arial, Helvetica, sans-serif">Port</font></th>
<th><font face="Arial, Helvetica, sans-serif">Router</font></th>
<th><font face="Arial, Helvetica, sans-serif">FW Version</font></th>
<th><font face="Arial, Helvetica, sans-serif">Username</font></th>
<th><font face="Arial, Helvetica, sans-serif">Password</font></th>
<th><font face="Arial, Helvetica, sans-serif">Notes</font></th>
<th><font face="Arial, Helvetica, sans-serif">Delete</font></th>
</tr>

<?php

$i=0;
while ($i < $num) {

$id=mysql_result($result,$i,"id");
$ip=mysql_result($result,$i,"ip");
$port=mysql_result($result,$i,"port");
$router=mysql_result($result,$i,"router");
$fwversion=mysql_result($result,$i,"fwversion");
$user=mysql_result($result,$i,"user");
$pass=mysql_result($result,$i,"pass");
$notes=mysql_result($result,$i,"notes");

?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $id; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $ip; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $port; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $router; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $fwversion; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $user; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $pass; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $notes; ?></font></td>

<?php
$i++;
}

echo "</table>";

?>

 

Any help that anyone can offer would be greatly appreciated. I've been stuck on this one for a couple of days now.

 

-Jason

Link to comment
https://forums.phpfreaks.com/topic/137712-solved-link-to-delete-mysql-row/
Share on other sites

You could modify this link:

 

<td><font face="Arial, Helvetica, sans-serif"><?php echo $id; ?>< a href="delete.php?id='<?php echo $id; ?>'">Delete</a></font></td>

 

That would take you to a delete.php page with the id in the url. Then on the delete page you would use a get method to get the id and then use the id to delete the record using a query and redirect you to a page that says the record was deleted. Hopefully that will help.

That is a floored code, unless your the only one got access to delete the database entrys..

 

 

You need to use a session for the users id to delete entry or your get a clever person

come along and delete other users database rows...

 

 

what happens when he start loosing money, When his database get deleted entry's,

 

you no as well as me your suggestion is only a suggestion, and other users like my self like to point out

possibility that could happen.

 

good advice is always welcomed,

 

also as been told my self thousand times, a problem never solved until the

post owner says so.

 

dosent matter if a user is logged in, the point is it deleting a database entry via  a url id,

what can be changed via any user looged in knowing any bodys id...

 

 

It can't be deleted if it is in an admin section. sure it is passed through the url but one would assume that on his delete.php page he would have something like this:

 

if($user == "admin")
{
delete the record
}

 

And more than likely he is just learning the how to do something like that and is testing.

Redarrow, thank you for wanting to let me know about a possible issue with the code, but as I stated in my first post I am not planning to make this script avalible to anyone besides myself, so security isn't much of a concern. I was simply focused on getting the delete function to work at all in a way that I could actually understand how it was happening.

 

Also, I am new to these forums and only just noticed that some of the posts are marked as "solved". I do consider my question answered and the problem solved, I simply don't know how to mark it as such, or even if I can do that myself.

 

Again, ngreenwood, thank you for the response, you were a big help. ;D

 

-Jason

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.