dougong1031 2016-10-04 15:12
浏览 74
已采纳

Mysqli_num_rows无法正常工作

I have a POST php script that I'm wanting to check if the POST data already exists in the database and if it does, throw error message, if it doesn't add it to database and give success message.

It adds successfully but if I try and add a name that already exists, it gives error:

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

and adds the new data anyways.

Code:

<?php  // _POST Add Major Category
    if(isset($_POST['AddMajorCat']))
    {
        // Declare _POST Variables
        $POSTNewMajorCatName = $_POST['MajorCatName'];

        // Check if name exists in database
            include_once('connection.php'); // db connection

            $query = "SELECT *
                                FROM DowntimeCategoriesMain
                                WHERE DowntimeCategoriesMain.maincategory = '$POSTNewMajorCatName'";

            $response = mysqli_query($db, $query); // Send Statement
            $NumRows = mysqli_num_rows($query); // Count number of rows

                if ($NumRows != 0) {
                    $messageSendTitle = "Uh-oh!";
                    $messageSend = "Major Downtime Category " . $POSTNewMajorCatName ." already exists. Unable to add Category.";
                    $messageSendType = "error";
                }
                else { // If name doesn't exist in database, add
                                    include_once('connection.php'); // db connection

                                    $query = "INSERT INTO DowntimeCategoriesMain (maincategory)
                                                        VALUES ('$POSTNewMajorCatName')";

                                                        $response = mysqli_query($db, $query); // Send Statement

                                                                if ($response) { // Successful
                                                                    $messageSendTitle = "Success!";
                                                                    $messageSend = "Major Category " . $POSTMajorCatName . " was added. ";
                                                                    $messageSendType = "success";
                                                                }
                                                                else { // Unsuccessful
                                                                    $messageSendTitle = "Uh-oh!";
                                                                    $messageSend = "There seems to be a problem. MySQL Error: " . mysqli_error($db);
                                                                    $messageSendType = "error";
                                                                }
                    }   // else statement

        } // If _POST is set statement
?>

I have tried exchanging mysqli_num_rows($query); for $query->num_rows; and it no longer shows error, but still adds the data.

  • 写回答

3条回答 默认 最新

  • donglu7998 2016-10-04 15:16
    关注

    Did you actually read the error message? It tells you exactly what is wrong. $query is a string, and it wants a mysqli_result, which is $response in your case:

    $response = mysqli_query($db, $query);
    $NumRows = mysqli_num_rows($response);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型