Forum Moderators: coopster

Message Too Old, No Replies

Need help with code to delete database entry

         

epsd

11:46 am on Oct 21, 2008 (gmt 0)

10+ Year Member



I've got a database entry that looks like so:

 testeee

I can select this from the database and put it into a variable (say $deletethis). However, when I then go to delete this entry from the database using php:
mysql_query("DELETE FROM calendar WHERE entry = '$deletethis'"); it doesn't work... The variable is there, but it won't delete! Any ideas?

Pico_Train

2:02 pm on Oct 21, 2008 (gmt 0)

10+ Year Member



try

mysql_query("DELETE FROM calendar WHERE entry = '.$deletethis.'");

Also make sure you are actually running the query.

I do this:

$sql= 'DELETE FROM calendar WHERE entry = "'.$deletethis.'"';

$result = mysql_query($sql);

You have to pay attention to whether your variable is a string or integer when doing statements. It affects the brackets and the . and still confuses me after 3 years!

[edited by: Pico_Train at 2:02 pm (utc) on Oct. 21, 2008]

epsd

2:48 pm on Oct 21, 2008 (gmt 0)

10+ Year Member



No go. Still doesn't delete the entry. I know the delete statement works because entries without &nbsp can be deleted...

Anyango

3:11 pm on Oct 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$deletethis=' testeee';
$sql="delete from calendar where entry='$deletethis'";
mysql_query($sql) or die(mysql_error());

This works fine for me and deletes the entry.

phranque

3:14 pm on Oct 21, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i would put the sql statement in a string variable first and print it out to make sure it is what you think it should be.

Anyango

3:16 pm on Oct 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




It affects the brackets and the . and still confuses me after 3 years!

mysql_query("DELETE FROM calendar WHERE entry = '.$deletethis.'");

Hey, i dont think those . are needed at all, are they ? infact it appears invalid syntax to me ( i might be wrong i know)

i would use it as

mysql_query("DELETE FROM calendar WHERE entry = '$deletethis'");

and in the scenario where we decide to use them or they might be needed for any reasons i would use

mysql_query("DELETE FROM calendar WHERE entry = '". $deletethis. "'");

[edited by: Anyango at 3:17 pm (utc) on Oct. 21, 2008]

epsd

4:14 pm on Oct 21, 2008 (gmt 0)

10+ Year Member



mysql_query("DELETE FROM calendar WHERE entry = '$deletethis'"); is what I've been using. It works fine when &nbsp is not in an entry. Going forward, I've stripped all entries of &nbsp before adding to the DB. However, I still can't delete any old entries with &nbsp in them. When I print the string, &nbsp is there. Pretty frustrating!

phranque

10:01 pm on Oct 21, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



does the printed query work when entered in the command line of the mysql client?

Anyango

3:31 am on Oct 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



what MySQL version are you Using, and does your printed query (as phranque mentioned) work via phpMyAdmin or any other admin you use for mysql ? becase your exact query worked fine for me in MySQL 5.0.

If you want to delete all such entries from the table, which contain   in them then try this also

DELETE FROM calendar where entry like '% %'

See if that works