how to delete records in my database in mysql fo php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zhed
Forum Newbie
Posts: 2
Joined: Tue Aug 30, 2011 8:16 am

how to delete records in my database in mysql fo php

Post by zhed »

if (isset($_POST['submitted'])){
{
$nam = $_POST['search'];
mysql_connect("localhost","root","");
mysql_select_db("sample");
$qry = "delete from student where sNumber='$nam'";
$res = mysql_affected_rows(mysql_query); // If user name exists, this will return number of rows fetched.
if ($res = 0) // If 0, then username doesnot exist
{
echo " No id found";
}
else
{
$qry = "delete from student where sNumber='$nam'";
$res = mysql_query($qry);
if (@mysql_query($qry))
{ echo("<br/><br/>Record Removed!</p>"); }
else
{ echo("<br/><br/>Error deleteing: " . mysql_error() . "</p>"); }
}

}





here is my code....there is a problem here...can someone help me..plz...thanx for advnce...
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: how to delete records in my database in mysql fo php

Post by phphelpme »

zhed wrote:

Code: Select all

$qry = "delete from student where sNumber='$nam'"; 

echo " No id found";

{ echo("<br/><br/>Record Removed!</p>"); }
else 
{ echo("<br/><br/>Error deleteing: " . mysql_error() . "</p>");
You are using a delete query when you connect to your database when checking a username etc

You echo without brackets in your first echo no id found but use brackets in your next two echos.

You need to tell us what errors are being displayed for you before we can truly fix the issue.

Best wishes
daveshoope
Forum Newbie
Posts: 8
Joined: Thu Aug 06, 2009 10:28 am

Re: how to delete records in my database in mysql fo php

Post by daveshoope »

zhed wrote: $qry = "delete from student where sNumber='$nam'";
$res = mysql_affected_rows(mysql_query); // If user name exists, this will return number of rows fetched.
if ($res = 0) // If 0, then username doesnot exist
try this out
$res = mysql_affected_rows(mysql_query($qry));

the query was not passed as a parameter to the mysql_query() function.

If thats the only problem fine, but if there are more, still speak :D
zhed
Forum Newbie
Posts: 2
Joined: Tue Aug 30, 2011 8:16 am

Re: how to delete records in my database in mysql fo php

Post by zhed »

sir daveshoope,....
i try your code and i met this error....what is the problem sir?;

Warning: mysql_affected_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\sample\delete.php on line 28

Record Removed!


sir...i want to delete record in my database in mysql and it will tells the user if the id is not in database and should not be deleting if that is the situation.....how can i do that with that code......
Post Reply