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

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程