gpt4 book ai didi

php - 更新和插入 mysqli 表

转载 作者:行者123 更新时间:2023-11-29 03:36:35 30 4
gpt4 key购买 nike

我正在尝试检查 leaguename 名称是否已经存在,如果存在,然后检查它是否高于现有分数。如果它更高,则替换 quizscore 否则将 leaguenamequizscore 插入表 league_quiz。

代码似乎在我的表中插入了值,但如果名称与之前的名称相同且分数更高,它似乎不会更新?

我收到这个错误:

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

我的代码:

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

?>

最佳答案

mysqli_num_rows 期望查询的结果而不是查询本身的字符串。首先你必须执行你的查询:

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

然后你可以数一数行数

$numRows = mysqli_num_rows($result);

关于php - 更新和插入 mysqli 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20806116/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com