简体   繁体   中英

update and insert mysqli table

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));
}

?>

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?

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);

Try the followowing:

$result = mysqli_query($query);
if (mysqli_num_rows($result)) {
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM