Re: Deleting a Record
| From: | Julio Nobrega Trabalhando | Date: | Wed, 06 Mar 2002 13:21:46 +0000 |
| Subject: | Re: Deleting a Record | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-87454@lists.php.net to get a copy of this message | ||
First of all, make a copy of your database to test things. Never test code
on production enviroment.
Then, the delete syntax most of the time is:
DELETE FROM table_name WHERE column = 'value';
Let's see:
<?php
$table = 'table_name';
$value = '15';
$sql = "DELETE FROM $table WHERE field_1 = '$value'";
mysql_query($sql) or exit(mysql_error());
?>
There isn't a space between field and 1, at least I don't think SQL
compliant databases let you put spaces on table and column names. $value
probaly should be between single quotes, Mysql requires it for strings or
values that are compared to a string type-column (varchar, text, etc...).
Even sometimes for numeric columns, so, doesn't hurt to leave it unless it
doesn't work ;-)
If $table is not recognized, try:
$sql = 'DELETE FROM ' . $table . " WHERE field_1 = '$value'";
If you just want to delete one record, it's a good idea to put a LIMIT 1
at the end.
Hope it helps!
--
Julio Nobrega.
Um dia eu chego lá:
http://sourceforge.net/projects/toca
Ajudei? Salvei? Que tal um presentinho?
http://www.submarino.com.br/wishlistclient.asp?wlid=664176742884
"Chuck "Pup" Payne" <cepayne@magidesign.com> wrote in message
news:B8AB79D2.C382%cepayne@magidesign.com...
> Hi,
>
> I a seting up a php page that will let me delete a record from my mysql
> database, but I want it be able to match to fields before it will let a
user
> delete that record. I know the basic sql command is....
>
> DELETE FROM $table WHERE field 1 = "$value"
>
> But I don't know how to write the state for a second field. Can some one
> tell, but one field seem to give too much choose and would make it to easy
> to delete the wrong record.
>
>
> ----------------------------
> | Chuck Payne |
> | Magi Design and Support |
> | cepayne@magidesign.com |
> ----------------------------
>
> BeOS, Macintosh 68K, Classic, and OS X, Linux Support.
> Web Design you can afford.
>
> "Never be bullied into silence. Never allow yourself to be made a victim.
> Accept no one's definition of your life; define yourself."- Harvey
Fierstein
>
>