Link to home
Create AccountLog in
Avatar of fcruz5
fcruz5Flag for United States of America

asked on

Clear table data in PHP & MySQL?

Hi,

Is it possible to clear table data from a MySQL database by clicking on a link? For example, lets say I have a table called:

TABLE_ONE

with the following fields:

field1
field2
field3
field4
field5

I want to have a link like this on a PHP page:

"Clear data on Table One"

Once the link is clicked it will just clear all the data in all the fields for Table One. How is something like this done?
Avatar of Cornelia Yoder
Cornelia Yoder
Flag of United States of America image

The link would go to a php script that would connect to the database and then issue the mysql query

DELETE FROM TableName
Avatar of fcruz5

ASKER

Okay, how would I make the link work with the mysql query?

Would the query be something like below? Can you provide an example?

SELECT field1, field2, field3, field4, field5
FROM TABLE_ONE
DELETE FROM TABLE_ONE

Open in new window

depending on the size of table_one you might want to go for truncate table_one instead of delete

http://dev.mysql.com/doc/refman/5.0/en/truncate.html
ASKER CERTIFIED SOLUTION
Avatar of Cornelia Yoder
Cornelia Yoder
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of fcruz5

ASKER

What if I just want to clear out data on a couple of fields and not all of them?

Lets say I just want to clear out data for field1 & field 2

Would I do it like this?

<?
mysql_connect(...my database info...);
mysql_query("DELETE field1, field2 FROM TABLE_ONE");
mysql_close();
exit();
?>
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account