Deleting rows from a DB

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
crazycaddy
Forum Commoner
Posts: 43
Joined: Fri Aug 05, 2005 6:59 am
Location: England, UK

Deleting rows from a DB

Post by crazycaddy »

Ok, ive made a simple script which adds data to a mysql database, ive also made it display. Now, I wrote this piece of code to delete the row but it isnt working and I'm not sure why.

Code: Select all

<?php
if(isset($_GET['action']) && $_GET['action'] === 'del') {
$key = $_GET['id'];
$db = mysql_connect('localhost','****','****') or die("could not connect");
mysql_select_db("util", $db) or die("could not get database");
$query = "DELETE FROM util WHERE id = $key";
mysql_query($query);
}
?>
Anyone have any ideas :?:
crazycaddy
Forum Commoner
Posts: 43
Joined: Fri Aug 05, 2005 6:59 am
Location: England, UK

Post by crazycaddy »

Sorted that problem, but theres more....

Code: Select all

<?php
while($myrow = mysql_fetch_row($result)) {
$idno = $field[0];
echo "<tr>";
foreach ($myrow as $field){
echo "<td>$field</td><td><a href=\"util.php?action=del&id=$idno\">delete</a></td>";
}
echo "</tr>";
}
echo '</table>';
mysql_close($db);
?>
When the table is displayed it shows multiple delete links when I only want it to show one.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

crazycaddy wrote: The url i was typing was index.php?Action=del&id=1
when it should of been index.php?action=del&id=1
Not to rag on you but that's a rather dangerous url string no? For example, if you were a banking institution an each id represented a charge against a users account, then a savy user could have those charges (purchases) deleted from his system! 8O

You may want to consider POST'ing this information.

Cheers,
BDKR
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

~ Whoops - Wrong Forum ~
Last edited by Dale on Fri Aug 05, 2005 7:35 am, edited 1 time in total.
crazycaddy
Forum Commoner
Posts: 43
Joined: Fri Aug 05, 2005 6:59 am
Location: England, UK

Post by crazycaddy »

ok thanks, oh and btw its for a intranet with about 10 employees, but i may as well use the post method to make sure its safer.

In your 'SELECT' query add Code:
LIMIT 1
at the end. Maybe that helps?
Wheres my select query? Or where you referring to my first post?
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

crazycaddy wrote:Sorted that problem, but theres more....

Code: Select all

<?php
while($myrow = mysql_fetch_row($result)) {
$idno = $field[0];
echo "<tr>";
foreach ($myrow as $field){
echo "<td>$field</td><td><a href="util.php?action=del&id=$idno">delete</a></td>";
}
echo "</tr>";
}
echo '</table>';
mysql_close($db);
?>
When the table is displayed it shows multiple delete links when I only want it to show one.
Show us the query you are using to get the information from the db.
Post Reply