Jump to content

variable using wrong value


etsted

Recommended Posts

 $sql = "SELECT vote FROM rate WHERE username = '$username' && file_name = '$file_name'";                $query = mysqli_query($con,$sql) or die(mysqli_error());                                $row = mysqli_fetch_array($sql);                $DB_vote = $row['vote'];                //var_dump($DB_vote);                                //check to see if the user has voted on this file before                if($rate_type === "good" && $DB_vote === 1)                {                    $vote = 1;                } else if($rate_type === "good" && ($DB_vote === 0 OR $DB_vote == NULL) )                {                    $vote = 1;                } else if($rate_type === "good" && $DB_vote === -1)                {                    $vote = 0;                } else if($rate_type === "bad" && $DB_vote === 1)                {                    $vote = 0;                } else if($rate_type === "bad" && ($DB_vote === 0 OR $DB_vote == NULL) )                {                    $vote = -1;                } else if($rate_type === "bad" && $DB_vote === -1){                    $vote = -1;                }                                  $numrows = mysqli_num_rows($query);                if($numrows === 0){                    $sql = "INSERT INTO rate VALUES('','$file_name','$username','$vote')";                    $query = mysqli_query($con,$sql) or die(mysqli_error());                } else if($numrows === 1){                    $sql = "UPDATE rate SET vote = '$vote' ";                    $query = mysqli_query($con,$sql) or die(mysqli_error());                }

I am trying to make a rate system that rate a file according to what he rated before. So when i press "bad" i get a value of -1 as expected, but then when i press "good" i get a value of 1 (stored in the DB), But it should have the value 0 as you can see in my code.

Link to comment
Share on other sites

that might be a string containing the text NULL, anyway you should probably be checking the row count before fetching the row in case the row doesn;t exist. something like:

 

 

if(mysql_num_rows() == 0){$DB_vote = NULL;}else{$row = mysqli_fetch_array($sql);$DB_vote = $row['vote'];}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...