简体   繁体   English

我的代码有什么问题??? 更新查询不起作用

[英]What's wrong in my code??? Update query not working

I'm posting this question after debugging it several times.我在调试了几次后发布了这个问题。 Please have a look.请看一看。

<?php
if(isset($_POST["g"]) && isset($_POST["c"]))
{
    $g = preg_replace('#[^a-z]#i', '', $_POST['g']);
    $c = preg_replace('#[^a-z ]#i', '', $_POST['c']);
    //echo $g.$c.$log_username;
    if($g == "" || $c == "")
    {
        echo "The form submission is missing values.";
        exit();  
    }
    else
    {
        $sql = "UPDATE users SET gender='$g',country='$c' WHERE username='$log_username'";
        $query = mysqli_query($db_conx,$sql);
        //echo $query;
        $numrows = mysqli_num_rows($query);
        //echo $numrows;
        if($numrows > 0)
        {
            echo "success";
            exit();
        }
        else
        {
            echo "failed";
            exit();
        }
        exit();
    }
}
?>

The error i'm getting is Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\\xampp\\htdocs\\sns2\\root\\account_config.php on line 36我得到的错误是Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\\xampp\\htdocs\\sns2\\root\\account_config.php on line 36

I'm perfectly getting the values of $g,$c,$log_username (tested it using echo).我完美地获得了$g,$c,$log_username (使用 echo 对其进行了测试)。 I'm being connected to the database.我正在连接到数据库。 I don't think there is something wrong with the query syntax because I have manually executed the same query using the values of $g,$c,$log_username .我认为查询语法没有问题,因为我已经使用$g,$c,$log_username的值手动执行了相同的查询。 The query was executed.查询已执行。 So could be wrong in this??所以这可能是错误的? Please help!!请帮忙!!

Citing the php documentation for mysqli_query :引用mysqli_query的 php 文档:

Returns FALSE on failure.失败时返回 FALSE。 For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object.对于成功的SELECT、SHOW、DESCRIBE 或 EXPLAIN查询,mysqli_query() 将返回一个 mysqli_result 对象。 For other successful queries mysqli_query() will return TRUE.对于其他成功的查询, mysqli_query() 将返回 TRUE。

So you're probably getting a TRUE return value and should use http://de3.php.net/manual/en/mysqli.affected-rows.php instead所以你可能得到一个TRUE返回值,应该使用http://de3.php.net/manual/en/mysqli.affected-rows.php代替

在 UPDATE 查询的情况下使用 mysqli_affected_rows。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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