Raj
Raj

Reputation: 993

How to execute a MySql query?

I'm executing a simple delete query in mysql using php. I don't know wrong with this query:

  $delid=$_REQUEST['did'];
if(($del=mysql_query("delete from achieva_trainee_mail_tble where id='".$delid."'"))==true)
{
    header("location:viewtrainee.php?dl=1");
}
else
{
    echo("<script>alert('Failure please try again later...');</script>");
}

I can get the 'did' in the page using '$_REQUEST['did']. But it isn't redirecting to the viewtrainee.php page, it goes to the else part. I execute the same structure for other files it works fine. What is wrong with this.

Upvotes: 1

Views: 157

Answers (2)

A.N.M. Saiful Islam
A.N.M. Saiful Islam

Reputation: 2138

Use mysql_affected_rows function.

$delid=$_REQUEST['did'];

mysql_query("delete from achieva_trainee_mail_tble where id='".$delid."'"));

if(mysql_affected_rows())
{
    header("location:viewtrainee.php?dl=1");
}
else
{
    echo("<script>alert('Failure please try again later...');</script>");
}

Upvotes: 1

silly
silly

Reputation: 7887

mysql_query return the result and not the success state... use mysql_error or mysql_errno like this:

$didName = 'did';
$delId = null;
$failed = true;

if(array_key_exists($didName, $_REQUEST)) {
    $delId = $_REQUEST[$didName];
    mysql_query("delete from achieva_trainee_mail_tble where id='".mysql_real_escape_string($delId, $yourConnection)."'", $yourConnection);

    if(false == mysql_errno($yourConnection)) {
        $failed = true;
    }
}

if(false == $failed) {
       header("location:viewtrainee.php?dl=1");
}
else {
    echo("<script>alert('Failure please try again later...');</script>");
}

Upvotes: 0

Related Questions