如果达到最大值,则语句不起作用

时间:2015-02-06 15:05:23

标签: php

我正在设置设置代码以设置您的帐户我有一个IF语句如果total_not< = 40它必须什么也不做,因为您已达到最大通知。但这不起作用我如何解决这个问题?

代码:

<?php

        // Set the database access information (add your own database credentials below)
        xxxxx

        // Connect to the database
        $dbc = @mysqli_connect ($db_host, $db_user, $db_password, $db_name) OR die ('Could not connect to MySQL: ' . mysqli_connect_error());

        // Check for shout removal


        // Check for shout submission
        if(isset($_POST['submit']))
        {
            // Empty error array
            $error = array();

            // Check for a shout
            if(empty($_POST['email']))
            {
                $error[] = "Oeps! Er is iets fout gegaan!";
            }

            // If there are no errors, insert shout into the database.
            // Otherwise, display errors.
            if(sizeof($error) == 0)
            {
                // Clean data
                $mijn_les = $_POST['mijn_les'];
                $email = mysqli_real_escape_string($dbc, $_POST['email']);


                // Insert shout
                $query = "UPDATE users SET user_email = '$email', user_tid = '$mijn_les' WHERE user_name = '{$_SESSION['user_name']}'";
                $result = @mysqli_query($dbc, $query) or die('Query failed: ' . mysqli_error($dbc));

                // Display confirmation

            echo "<div class='alert alert-success'>

            <strong>Gelukt!</strong> Je account is ingesteld! <a href='../signin.php?logout' class='alert-link'>Klik hier</a> om opnieuw in te loggen!
          </div>";

            $hide = "style='display: none;'";

    $num_rows = $result->num_rows;
    if($num_rows <= 40)
    {
        $send_not = "INSERT INTO notif (send_to, grav_url, send_by, notif_text) VALUES ('{$_SESSION['user_name']}', 'http://www.gravatar.com/avatar/a72776fb2702ba915e2327618414bb15', 'Admin', 'Leraar Setup compleet! U kunt nu onze website gebruiken!')";
                $not_add = @mysqli_query($dbc, $send_not) or die('Query failed: ' . mysqli_error($dbc));    
    }
            } else {

                // Display error message
                foreach($error as $value)
                {
                echo "<div class='alert alert-danger'>

            <strong>Oeps!</strong> Er is iets mis gegaan! Probeer het opnieuw!
          </div>";   
                }

            }
        }



    ?>  

2 个答案:

答案 0 :(得分:0)

更改您的代码:

 if($num_rows <= 40) {...}

通过

if (mysqli_affected_rows($dbc) <= 40) {...}

答案 1 :(得分:0)

num_rows:获取结果集中的行数,这意味着更新结果永远不会是>= 40,并且每次都会执行insert查询。

因此,您应该使用SELECT COUNT(id_notif) as total_notif FROM notif where user_email = '$email'total_notif代替$num_rows