2

I'm trying to check if the leaguename name already exists if it does then check if it is higher than the existing score. if its higher then replace the quizscore else insert the leaguename and quizscore into the table league_quiz.

The code seem to insert the values in my table, but it do not seem update if the name is equal to previous and the score is higher?

I get this error:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, string given in

My code:

<?php

$name = (string)$_POST['name'];
$score = (string) $_POST['score'];

$link = mysqli_connect("mysql12.gigahost.dk","username","password","dirts_mysql");

$query = "SELECT leaguename, quizscore FROM league_quiz WHERE  leaguename = '$name' AND quizscore < '$score'";

if(mysqli_num_rows($query)) {
    mysqli_query($link,"UPDATE league_quiz SET quizscore = '$score'");
} else {
    mysqli_query($link,"INSERT INTO league_quiz (leaguename, quizscore) VALUES ('$name', '$score')") or die(mysqli_error($link));
}

?>
tadman
  • 208,517
  • 23
  • 234
  • 262
user3140296
  • 169
  • 1
  • 9
  • 1
    Use prepared statements. Your code is vulnerable to SQL injections – Jompper Dec 27 '13 at 18:50
  • Also you should run the query $query = mysqli_query($link, "select here"); – Jompper Dec 27 '13 at 18:52
  • Why are you casting strings to strings? Why aren't you escaping your values using [`bind_param`](http://www.php.net/manual/en/mysqli-stmt.bind-param.php)? I really hope this isn't live on the internet because it could be cracked in seconds. – tadman Dec 27 '13 at 19:56
  • i'll take a look on the bind_param and it is not live. – user3140296 Dec 27 '13 at 20:12
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – John Conde Jan 02 '14 at 00:29

3 Answers3

0

Between those two:

$query = "SELECT leaguename, quizscore FROM league_quiz WHERE  leaguename = '$name' AND      quizscore < '$score'";

if(mysqli_num_rows($query)) {

Have you tried to write this code?

$result = mysqli_query($query);

and then use mysqli_num_rows on the result of the query?

Goikiu
  • 574
  • 3
  • 12
0

mysqli_num_rows expects the result of a query and not the string of the query itself. Firstly you must execute your query :

$result = mysqli_query($link,$query);

and then you can count the number of rows

$numRows = mysqli_num_rows($result);
Olivier
  • 324
  • 3
  • 12
  • first who knows that mysqli needs link for connection for query. – Lkopo Dec 27 '13 at 19:02
  • Please, remove that link to w3schools for the sake of all humanity. [The lessons there are severely damaging](http://w3fools.com/). – tadman Dec 27 '13 at 19:55
0

Try the followowing:

$result = mysqli_query($query);
if (mysqli_num_rows($result)) {
...
Ahmed Siouani
  • 13,701
  • 12
  • 61
  • 72
abe
  • 624
  • 8
  • 14