8 Replies - 1806 Views - Last Post: 30 July 2009 - 01:48 AM Rate Topic: -----

#1 DCMark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 17-July 09

Delete row from table, should be simple :(

Post icon  Posted 29 July 2009 - 09:01 AM

Hi all,

I'm writing some simple code to add and delete property info to a DB. The adding part works fine, I just can't seem to delete rows from my table.

My DB user account has read and write permissions, so I guess its the code.

I'm basically telling the page to connect to the DB, read whats there and add a link to delete that row (from the variable ID).

It seems to work fine, it is just not deleting the row when the link is clicked.

<?php
include 'connect.php';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die					  ('Error connecting to mysql');
mysql_select_db($dbname);
$data = mysql_query("SELECT * FROM Property")
or die(mysql_error());
Print "<table border=\"0\" cellpadding=\"3\">";
while($info = mysql_fetch_array( $data ))
{
// Display contents of DB
Print "<tr>";
Print "<th>Property Name:</th> <td>".$info['PropertyName']."</td> ";
Print "<th>Detail 1:</th> <td>".$info['Detail1']."</td> ";
Print "<th>Detail 2:</th> <td>".$info['Detail2']."</td> ";
Print "<th>Detail 3:</th> <td>".$info['Detail3']."</td> ";
Print "<th>Address:</th> <td>".$info['Address']."</td> ";
Print "<th>Map:</th> <td>".$info['Map']."</td> ";
Print "<th>Image 1:</th> <td><img src=\"".$info['Img1']."\" width=\"50\" height=\"50\"></td> ";
Print "<th>Image 2:</th> <td><img src=\"".$info['Img2']."\" width=\"50\" height=\"50\"></td> ";
Print "<th>Image 3:</th> <td><img src=\"".$info['Img3']."\" width=\"50\" height=\"50\"></td> ";
Print "<th>Image 4:</th> <td><img src=\"".$info['Img4']."\" width=\"50\" height=\"50\"></td> ";
echo "<th>Remove:</th> <td><a href='addremoveproperty.php?cmd=delete&ID=".$info['ID']."'>Delete</a></td></tr>";
}
Print "</table>";
?> 
<?
if($cmd=="delete")
{
	$data = mysql_query("DELETE FROM Property WHERE ID=$ID")
	or die(mysql_error());
	print "<strong>Row deleted!</strong>";
}
?>


Any ideas? :)

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Delete row from table, should be simple :(

#2 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: Delete row from table, should be simple :(

Posted 29 July 2009 - 09:03 AM

Quote

"DELETE FROM Property WHERE ID=$ID"


should be:

"DELETE FROM Property WHERE ID='$ID'"



Notice the single quotes. Because currently you are looking for any Property that has an id of "$ID" literally.

--

Greg
Was This Post Helpful? 0
  • +
  • -

#3 DCMark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 17-July 09

Re: Delete row from table, should be simple :(

Posted 29 July 2009 - 09:14 AM

Hi Greg! Thanks for the quick reply!

I tried replacing the code, but it still won't delete the row... any ideas?

<?php
include 'connect.php';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die					  ('Error connecting to mysql');
mysql_select_db($dbname);
$data = mysql_query("SELECT * FROM Property")
or die(mysql_error());
Print "<table border=\"0\" cellpadding=\"3\">";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Property Name:</th> <td>".$info['PropertyName']."</td> ";
Print "<th>Detail 1:</th> <td>".$info['Detail1']."</td> ";
Print "<th>Detail 2:</th> <td>".$info['Detail2']."</td> ";
Print "<th>Detail 3:</th> <td>".$info['Detail3']."</td> ";
Print "<th>Address:</th> <td>".$info['Address']."</td> ";
Print "<th>Map:</th> <td>".$info['Map']."</td> ";
Print "<th>Image 1:</th> <td><img src=\"".$info['Img1']."\" width=\"50\" height=\"50\"></td> ";
Print "<th>Image 2:</th> <td><img src=\"".$info['Img2']."\" width=\"50\" height=\"50\"></td> ";
Print "<th>Image 3:</th> <td><img src=\"".$info['Img3']."\" width=\"50\" height=\"50\"></td> ";
Print "<th>Image 4:</th> <td><img src=\"".$info['Img4']."\" width=\"50\" height=\"50\"></td> ";
//Print "<th>Remove:</th> <td><a href=\"delete.php?ID=".$info['ID']."\">Delete Property</a></td></tr>";
echo "<th>Remove:</th> <td><a href='addremoveproperty.php?cmd=delete&ID=".$info['ID']."'>Delete</a></td></tr>";
}
Print "</table>";
?> 
<?
if($cmd=="delete")
{
	$data = mysql_query("DELETE FROM Property WHERE ID='$ID'")
	or die(mysql_error());
	print "<strong>Row deleted!</strong>";
}
?>

Was This Post Helpful? 0
  • +
  • -

#4 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Delete row from table, should be simple :(

Posted 29 July 2009 - 09:46 AM

you never set $ID ... need to add a line defining it or change the query ...

$ID = $_GET['ID'];
$data = mysql_query("DELETE FROM Property WHERE ID='$ID'")



or

$data = mysql_query("DELETE FROM Property WHERE ID='" . $_GET['ID'] . "'")


Was This Post Helpful? 0
  • +
  • -

#5 DCMark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 17-July 09

Re: Delete row from table, should be simple :(

Posted 29 July 2009 - 10:05 AM

Sorry man, neither of those are working :(

Its getting the ID from the database as it displays the ID when it writes out the delete url correctly ie: /addremoveproperty.php?cmd=delete&ID=21

<?php
include 'connect.php';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die					  ('Error connecting to mysql');
mysql_select_db($dbname);
$data = mysql_query("SELECT * FROM Property")
or die(mysql_error());
Print "<table border=\"0\" cellpadding=\"3\">";
while($info = mysql_fetch_array( $data ))
{
$ID = $_GET['ID'];
Print "<tr>";
Print "<th>Property Name:</th> <td>".$info['PropertyName']."</td> ";
Print "<th>Detail 1:</th> <td>".$info['Detail1']."</td> ";
Print "<th>Detail 2:</th> <td>".$info['Detail2']."</td> ";
Print "<th>Detail 3:</th> <td>".$info['Detail3']."</td> ";
Print "<th>Address:</th> <td>".$info['Address']."</td> ";
Print "<th>Map:</th> <td>".$info['Map']."</td> ";
Print "<th>Image 1:</th> <td><img src=\"".$info['Img1']."\" width=\"50\" height=\"50\"></td> ";
Print "<th>Image 2:</th> <td><img src=\"".$info['Img2']."\" width=\"50\" height=\"50\"></td> ";
Print "<th>Image 3:</th> <td><img src=\"".$info['Img3']."\" width=\"50\" height=\"50\"></td> ";
Print "<th>Image 4:</th> <td><img src=\"".$info['Img4']."\" width=\"50\" height=\"50\"></td> ";
//Print "<th>Remove:</th> <td><a href=\"delete.php?ID=".$info['ID']."\">Delete Property</a></td></tr>";
echo "<th>Remove:</th> <td><a href='addremoveproperty.php?cmd=delete&ID=".$info['ID']."'>Delete</a></td></tr>";
}
Print "</table>";
?> 
<?
if($cmd=="delete")
{
	//$data = mysql_query("DELETE FROM Property WHERE ID='$ID'")
	$data = mysql_query("DELETE FROM Property WHERE ID='" . $_GET['ID'] . "'")
	or die(mysql_error());
	print "<strong>Row deleted!</strong>";
}
?>


Any other ideas? :(
Was This Post Helpful? 0
  • +
  • -

#6 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Delete row from table, should be simple :(

Posted 29 July 2009 - 10:12 AM

i just realized you didn't define $cmd as well ... im blind today ...

try changeing
if($cmd=="delete")



to
if(isset($_GET['cmd']) && $_GET['cmd'] == "delete")


This post has been edited by RPGonzo: 29 July 2009 - 10:47 AM

Was This Post Helpful? 1
  • +
  • -

#7 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: Delete row from table, should be simple :(

Posted 29 July 2009 - 10:48 AM

Quote

LOL i just realized you didn't define $cmd as well ... im blind today ...


Or make it a variable: $cmd = $_GET['cmd'];
Was This Post Helpful? 1
  • +
  • -

#8 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Delete row from table, should be simple :(

Posted 29 July 2009 - 11:16 AM

yep yep ... but if your host has E_NOTICES on .. you will get a slew of notices every time the page is uses about the index "cmd" not being defined ... if you don't check if its set first anyways .. so :)
Was This Post Helpful? 1
  • +
  • -

#9 DCMark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 17-July 09

Re: Delete row from table, should be simple :(

Posted 30 July 2009 - 01:48 AM

You guys rock so much!

Thanks for this! :) It was driving me mad! :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1