为什么我的ip检查方法不起作用?php/mysqli

k10s72fa  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(239)

为什么我试图通过ip地址检查双重投票,却不能正常工作?

if ($qtype === "sc"){
                // single choice has only one select item

                // check whether user has already answered this question
                $i=0;
                $res = mysqli_query($mysqli, "SELECT ip FROM votedanswers WHERE ip = \"".$ip."\" and qID = \"".$qID."\"");

                while($obj = mysqli_fetch_object($res))
                {
                  $i = $i+1;
                }                   
                if ($i > 0){
                    echo "your vote has already been counted";
                }
                else{
                    //insert data
                    $res = mysqli_query($mysqli, "INSERT INTO votedanswers (ip, qID, qQuestion, aID) VALUES ('$ip', '$qID', '$quest', '$aID')");
                    echo "your vote has been counted";
                }   

        } else {
            echo "nope!";
        }`


不久前测试过,一切正常。但现在我想投多少就投多少。

uqdfh47h

uqdfh47h1#

试试这个:

if ($qtype === "sc"){
            // single choice has only one select item

            // check whether user has already answered this question
            $i=0;
            $res = mysqli_query($mysqli, "SELECT ip FROM votedanswers WHERE ip = \"".$ip."\" and qID = \"".$qID."\"");

            while($obj = mysqli_fetch_assoc($res))
            {
              $i = $i+1;
            }                   
            if ($i > 0){
                echo "your vote has already been counted";
            }
            else{
                //insert data
                $res = mysqli_query($mysqli, "INSERT INTO votedanswers (ip, qID, qQuestion, aID) VALUES ('$ip', '$qID', '$quest', '$aID')");
                echo "your vote has been counted";
            }   

    } else {
        echo "nope!";
    }

另请参阅:如何防止php中的sql注入?

相关问题