-1

I am posting this in order to see if someone could help me to solve this little problem. Right now I have this code:

visible $sqlUpdate = "UPDATE user.user SET level='1' WHERE id='".$_GET['char']."' AND level >249 AND power>0 LIMIT 1";

What I want to do is that if the query actually runs, print Correct, otherwise, Error.

What I tried:

visible       $sqlUpdate = "UPDATE user.user SET level='1' WHERE id='".$_GET['char']."' AND level >249 AND power>0 LIMIT 1";
              $updatePos = mysql_query($sqlUpdate);
              if($sqlUpdate==1) {

              }
              else { echo'<p class="as">Correct.</p>'; }

            }
            else {
              echo'<p class="as">Error .</p>';
            }

But that does not work.

Andriy M
  • 76,112
  • 17
  • 94
  • 154

6 Answers6

1

use mysql_affected_rows() which returns the number of affected rows on success, and -1 if the last query failed.

$chrVal = $_GET['char'];
$sqlUpdate = "UPDATE user SET level = '1' WHERE id = '$chrVal' AND level > 249 AND power > 0 LIMIT 1";
$updatePos = mysql_query($sqlUpdate);
if(mysql_affected_rows() >= 1) 
{ 
    echo'<p class="as">Correct.</p>';
}
else
{
    echo'<p class="as">Error .</p>';
}

As a sidenote, the query is vulnerable with SQL Injection if the value(s) of the variables came from the outside. Please take a look at the article below to learn how to prevent from it. By using PreparedStatements you can get rid of using single quotes around values.

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492
1

if you want to know whether row updated or not. try mysql_affected_rows()

mysql_affected_rows()

$char = mysql_escape_string($_GET['char']);
$sqlUpdate = "UPDATE user.user SET level='1' WHERE id='" . $char . "' AND level >249 AND power>0 LIMIT 1";
$updatePos = mysql_query($sqlUpdate);
$affRows = mysql_affected_rows();
if ($affRows) {
  echo'<p class="as">Correct.</p>';
} else {
  echo'<p class="as">Error .</p>';
}

also use mysql_escape_string to avoid sql injections :)

Dino Babu
  • 5,814
  • 3
  • 24
  • 33
1

If condition should check the $updatePos. If it comes out to be true then it is correct else it will give you error.

you can check an example from here: http://php.net/manual/en/function.mysql-query.php

gusainhimanshu
  • 157
  • 1
  • 11
0

change your variable name $sqlUpdate to $updatePos

$sqlUpdate = "UPDATE user.user SET level='1' WHERE id='".$_GET['char']."' AND level >249 AND power>0 LIMIT 1";
                  $updatePos = mysql_query($sqlUpdate);
                  if($updatePos==1) {

                  }
                  else { echo'<p class="as">Correct.</p>'; }

                }
                else {
                  echo'<p class="as">Error .</p>';
                }
Raj Adroit
  • 3,828
  • 5
  • 31
  • 44
0

I use the following.

 if( $updatePos = mysql_query($sqlUpdate))
 {
 echo'<p class="as">Correct.</p>';     
 }
 else
 {
 echo'<p class="as">Error.</p>';     
 }

if the query is wrong, it will return error.

Eng. M
  • 48
  • 5
0

try this:

$sqlUpdate = "UPDATE user.user SET level='1' WHERE id='".$_GET['char']."' AND level >249 AND power>0 LIMIT 1";
              $updatePos = mysql_query($sqlUpdate);
              if($updatePos==1) {

              }
              else { echo'<p class="as">Correct.</p>'; }

            }
            else {
              echo'<p class="as">Error .</p>';
            }

You can also check it mysql_num_rows();

Like this

Kichu
  • 3,284
  • 15
  • 69
  • 135