etsted Posted September 10, 2014 Share Posted September 10, 2014 $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 More sharing options...
justsomeguy Posted September 10, 2014 Share Posted September 10, 2014 What does that var_dump line print? Link to comment Share on other sites More sharing options...
etsted Posted September 10, 2014 Author Share Posted September 10, 2014 NULL Link to comment Share on other sites More sharing options...
etsted Posted September 10, 2014 Author Share Posted September 10, 2014 that is the first time you vote on a content Link to comment Share on other sites More sharing options...
JamesB Posted September 10, 2014 Share Posted September 10, 2014 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 More sharing options...
justsomeguy Posted September 10, 2014 Share Posted September 10, 2014 that is the first time you vote on a contentAnd you're trying to solve a problem with the first time someone votes? Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now