Page 1 of 1

Deleting rows from a DB

Posted: Fri Aug 05, 2005 7:02 am
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 :?:

Posted: Fri Aug 05, 2005 7:20 am
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.

Posted: Fri Aug 05, 2005 7:31 am
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

Posted: Fri Aug 05, 2005 7:32 am
by Dale
~ Whoops - Wrong Forum ~

Posted: Fri Aug 05, 2005 7:34 am
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?

Posted: Fri Aug 05, 2005 8:24 am
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.