/    Sign up×
Community /Pin to ProfileBookmark

How to delete a record? PHP code help

HI all,
I want to delete a record based on a field input.

Here is my setup:
There are two files.

First file: enterdelete.php
<html>
<body>
<form action=delete.php method=GET>
Delete:
Item #: <input type=text name=p value=”11″ >
<p>
<input type=submit>
</form>
</body>
</html>

Second file:
<html>
<body>
<?php
mysql_connect (xxx, cccccccc, tttttttt);
mysql_select_db (order1);
if ($p == “”)
{$p = ‘%’;}
$result = mysql_query (“DELETE * FROM frames
WHERE p = ‘$p%’
“);
print “Test”;
?>
</body>
</html>

I ran these files, but records wasn’t deleted.

Please help me to do this task.

Thanks a lot.

SS

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@aussie_girlJul 04.2006 — The correct syntax is

DELETE FROM tablename

WHERE column name = something

So is p in your SELECT query a column name?
Copy linkTweet thisAlerts:
@DaiWelshJul 04.2006 — p = '$p%'

the % operator is usually used with LIKE not with =, so either try

p = '$p'

or

p LIKE '$p%'

Caveat though, don't use browser input directly like that in a real scenario or it could go horribly wrong....
Copy linkTweet thisAlerts:
@NogDogJul 04.2006 — 
Caveat though, don't use browser input directly like that in a real scenario or it could go horribly wrong....[/QUOTE]

Yeah, this code...
[code=php]
if ($p == "")
{$p = '%';}
[/code]

...looks really dangerous: accidentally hitting the Enter key in the form before entering a value would result in every record being deleted. :eek:
Copy linkTweet thisAlerts:
@smallstoneauthorJul 04.2006 — Thanks all for your helps.

Yes Assie Girl. P is column name.

DaiWelsh. I tried without the % and it didn't work.

Sigh.

Thanks,
Copy linkTweet thisAlerts:
@sitehatcheryJul 04.2006 — Even if you delete the record from the database, the file itself will remain on your server. You might think about deleting the file, or moving it to an archive folder if the record was successfully deleted from the database.

Also, I think it's a bit scary being able to delete any record you want directly from the query string. Something to think about.
Copy linkTweet thisAlerts:
@SheldonJul 04.2006 — This may not work right off the bat, but here we go


[code=php]
<?php
//connect to the mysql database
mysql_connect (xxx, cccccccc, tttttttt);
mysql_select_db (order1);

//make sure the hte var is set and thast it is number/nermic value only
if((isset($_GET['p']) and is_numeric($_GET['p'])){
$p = $_GET['p']; //set as an easy to use var

//this part hopefully checks to see if the string contains a '%' and addss one if there isnt one
if(!strstr($p, "%")){
preg_replace($p, $p."%", $p);
}

//all going well we delete the entry from the database
$delete = "DELETE FROM frames WHERE p = ".$p." LIMIT 1";
$result = mysql_query ($delete) or die (mysql_error()); //if there is an error with that then will stop the code from running further


//this is here if there is a file to be deleted
$path ="/path/to/file/"; //set the path to the file from this script
if(file_exists($path.$p)){
unlink($path.$p);
$mes = " and deleted from your server";
}

//success!
echo($p . " was successfuly deleted from your database".$mes.".");

}else{
//the file was not set or not a number

die ("There was not file set, of it was not only numbers");

}

//done!
?>[/code]
Copy linkTweet thisAlerts:
@bokehJul 04.2006 — LIMIT is for SELECT queries.
×

Success!

Help @smallstone spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...