0

What is the problem in my code ? every time it echo This image used as cover image , but delete query work properly.how can i fix it?

    <?php
     session_start();
     if(!empty($_SESSION['userId']) && !empty($_SESSION['name'])){
     include ('connect.php');
     dbConnect();
    if (isset($_GET['ximgid'])) {

  $del=mysql_query("Delete FROM project_image WHERE ximgid='".$_GET['ximgid']."' And coverflag !='1'");
  $delete=mysql_num_rows($del);
  if($delete==1){
    echo "<script type='text/javascript'>alert('Sucessfully Delete !!!')</script>";
    echo "<script>javascript:window.location = 'projectImage.php'</script>";
  }else{
    echo "<script type='text/javascript'>alert('This Image Used As Cover Image')</script>";
    echo "<script >window.location.href = 'projectImage.php'</script>";
  }
}else{
  echo "<script>javascript:window.location = 'index.php'</script>";
   }
 }
?>
6
  • 3
    Consider using prepared statements to avoid SQL injection attacks. Commented Jan 26, 2015 at 6:52
  • What does the data look like in your database? are you sure the 'ximgid' you're using matches with one of the rows? Commented Jan 26, 2015 at 6:53
  • 3
    For delete query, use mysql_affected_rows() instead of mysql_num_rows() Commented Jan 26, 2015 at 6:53
  • try if($delete!=0) instead of if($delete==1) Commented Jan 26, 2015 at 6:53
  • This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. Commented Jan 26, 2015 at 6:56

4 Answers 4

4

mysql_num_rowsonly works with selectstatement.

For delete statements you have to use mysql_affected_rows

Here the part of the documentation of mysql_affected_rows:

Retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set. To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows().

Sign up to request clarification or add additional context in comments.

3 Comments

@Khandaker Are you sure that records are deleted?
@Khandaker if you echoing $delete what is the value of that?
Nothing after echoing $delete , only blank page . @ Jens
0

mysql_affected_rows() is the way to go

$delete=mysql_affected_rows($del);
if($delete>0){
echo "<script type='text/javascript'>alert('Sucessfully Delete !!!')    </script>";
echo "<script>javascript:window.location = 'projectImage.php'</script>";
}else if($delete==0){
echo "<script type='text/javascript'>alert('This Image Used As Cover Image')</script>";
echo "<script >window.location.href = 'projectImage.php'</script>";
}else{
echo "<script type='text/javascript'>alert('mysql error')</script>";
}

Comments

0

I had to solve my problem in an alternative way which is given below:

      <?php
       session_start();
       if(!empty($_SESSION['userId']) && !empty($_SESSION['name'])){
        include ('connect.php');
        dbConnect();

         if(isset($_GET['ximgid'])){
         $selQuery = "SELECT coverflag FROM project_image WHERE ximgid ='".trim($_GET['ximgid'])."' AND  zid = '1000' AND xpid = '".trim($_GET['pid'])."'";
         $flagtResult=mysql_query($selQuery);
         $flagRow=mysql_fetch_array($flagtResult);
         mysql_free_result($flagRow);
         $cover=$flagRow[0];
         if($cover == 1){
         echo "<script type='text/javascript'>alert('Used as cover picture')</script>";
         echo "<script>javascript:window.location = 'projectImage.php'</script>";
        }else{
        $sql = "DELETE FROM project_image WHERE ximgid = '".trim($_GET['ximgid'])."' AND coverflag != '1' AND zid = '1000' AND xpid = '".trim($_GET['pid'])."'";
        $result = mysql_query($sql) or die(mysql_error());

        if($result > 0){
            echo "<script type='text/javascript'>alert('Sucessfully Detele !!!')</script>";
            echo "<script>javascript:window.location = 'projectImage.php'</script>";
        }
      }


    }
 }else{
    echo "<script>javascript:window.location = 'index.php'</script>";
  }
?>

Now I can get my proper output.. Thanks Everyone.

Comments

-1

first of all it appears that you are escaping ximgid and coverflag with ' which would throw a syntax error in your sql if those were not varchar fields.

$del=mysql_query("Delete FROM project_image WHERE ximgid=".$_GET['ximgid']." And coverflag !=1;");

Second issue to address would be that you are taking a $_GET parameter and putting it directly into your SQL query which is just trouble looking to happen so I would suggest next changing it to:

$del=mysql_query("Delete FROM project_image WHERE ximgid=".mysql_real_escape_string($_GET['ximgid'])." And coverflag !=1;");

if you need extra help debugging your SQL you can always temporarily put a call to

echo mysql_error();

after your call to mysql_query in order to show the error message from the server.

2 Comments

Please read question again, he said: delete query work properly
oops, that is my bad, I missed it primarily because the English used in the post was not exactly clear so I jumped straight into looking at a fault in the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.