一些单选按钮不起作用,有些单独按钮

时间:2015-08-20 21:24:38

标签: php html mysql mysqli

我是PHP的新手,我收到错误。我在if statement内插入不同的单选按钮后,在条件被检查(在MySQLi查询上)后插入单选按钮。首先,当我插入8个单选按钮时当我插入20个单选按钮而不是前三个单选按钮不起作用时,其他工作正常! 我很困惑导致这个错误的原因!

$qry = mysqli_query($con,"select user_pin,user_pin_entry from `user_data` where user_pin='$sms_code'") or die(mysqli_error($con));
$row=mysqli_fetch_assoc($qry);
 if ($row['user_pin']==$sms_code && $row['user_pin_entry']<=10) {
  $slct_data = mysqli_query($con,"select * from poll") or die(mysqli_erro($con));
    while ($row2=mysqli_fetch_assoc($slct_data)) {

      echo "<div class='col-xs-12 col-md-12 col-sm-12 text-center back'>".$row2['poll_title']."</div>";

         if(!empty($row2["poll_1"]) && !empty($row2["poll_1_image"])){
        echo  "<div class='col-xs-12 col-md-4 col-sm-12 center'></br><img src='admin/uploads/".$row2["poll_1_image"]."'/><p>";
        echo $row2["poll_1"]."</p><input type='radio' name='poll[]' class='radio' value='male'/></div>";
    }

         if(!empty($row2["poll_2"]) && !empty($row2["poll_2_image"])){
        echo  "<div class='col-xs-12 col-md-4 col-sm-12 center'></br><img src='admin/uploads/".$row2["poll_2_image"]."'/><p>";
        echo $row2["poll_2"]."</p><input type='radio' name='poll[]' class='radio' value='male'/></div>";
    }

         if(!empty($row2["poll_3"]) && !empty($row2["poll_3_image"])){
        echo  "<div class='col-xs-12 col-md-4 col-sm-12 center'></br><img src='admin/uploads/".$row2["poll_3_image"]."'/><p>";
        echo $row2["poll_3"]."</p><input type='radio' name='poll[]' class='radio' value='male'/></div>";
    }

         if(!empty($row2["poll_4"]) && !empty($row2["poll_4_image"])){
        echo  "<div class='col-xs-12 col-md-4 col-sm-12 center'></br><img src='admin/uploads/".$row2["poll_4_image"]."'/><p>";
        echo $row2["poll_4"]."</p><input type='radio' name='poll[]' class='radio' value='male'/></div>";
    }

         if(!empty($row2["poll_5"]) && !empty($row2["poll_5_image"])){
        echo  "<div class='col-xs-12 col-md-4 col-sm-12 center'></br><img src='admin/uploads/".$row2["poll_5_image"]."'/><p>";
        echo $row2["poll_5"]."</p><input type='radio' name='poll[]' class='radio' value='male'/></div>";
    }

         if(!empty($row2["poll_6"]) && !empty($row2["poll_6_image"])){
          echo  "<div class='col-xs-12 col-md-4 col-sm-12 center'></br><img src='admin/uploads/".$row2["poll_6_image"]."'/><p>";
        echo $row2["poll_6"]."</p><input type='radio' name='poll[]' class='radio' value='male'/></div>";
    }

         if(!empty($row2["poll_7"]) && !empty($row2["poll_7_image"])){
        echo  "<div class='col-xs-12 col-md-4 col-sm-12 center'></br><img src='admin/uploads/".$row2["poll_7_image"]."'/><p>";
        echo $row2["poll_7"]."</p><input type='radio' name='poll[]' class='radio' value='male'/></div>";
    }

         if(!empty($row2["poll_8"]) && !empty($row2["poll_8_image"])){
        echo  "<div class='col-xs-12 col-md-4 col-sm-12 center'></br><img src='admin/uploads/".$row2["poll_8_image"]."'/><p>";
        echo $row2["poll_8"]."</p><input type='radio' name='poll[]' class='radio' value='male'/></div>";
    }

         if(!empty($row2["poll_9"]) && !empty($row2["poll_9_image"])){
        echo  "<div class='col-xs-12 col-md-4 col-sm-12 center'></br><img src='admin/uploads/".$row2["poll_9_image"]."'/><p>";
        echo $row2["poll_9"]."</p><input type='radio' name='poll[]' class='radio' value='male'/></div>";
    }

         if(!empty($row2["poll_10"]) && !empty($row2["poll_10_image"])){
        echo  "<div class='col-xs-12 col-md-4 col-sm-12 center'></br><img src='admin/uploads/".$row2["poll_10_image"]."'/><p>";
        echo $row2["poll_10"]."</p><input type='radio' name='poll[]' class='radio' value='male'/></div>";
    }

         if(!empty($row2["poll_11"]) && !empty($row2["poll_11_image"])){
        echo  "<div class='col-xs-12 col-md-4 col-sm-12 center'></br><img src='admin/uploads/".$row2["poll_11_image"]."'/><p>";
        echo $row2["poll_11"]."</p><input type='radio' name='poll[]' class='radio' value='male'/></div>";
    }

         if(!empty($row2["poll_12"]) && !empty($row2["poll_12_image"])){
        echo  "<div class='col-xs-12 col-md-4 col-sm-12 center'></br><img src='admin/uploads/".$row2["poll_12_image"]."'/><p>";
        echo $row2["poll_12"]."</p><input type='radio' name='poll[]' class='radio' value='male'/></div>";
    }

         if(!empty($row2["poll_13"]) && !empty($row2["poll_13_image"])){
        echo  "<div class='col-xs-12 col-md-4 col-sm-12 center'></br><img src='admin/uploads/".$row2["poll_13_image"]."'/><p>";
        echo $row2["poll_13"]."</p><input type='radio' name='poll[]' class='radio' value='male'/></div>";
    }

         if(!empty($row2["poll_14"]) && !empty($row2["poll_14_image"])){
        echo  "<div class='col-xs-12 col-md-4 col-sm-12 center'></br><img src='admin/uploads/".$row2["poll_14_image"]."'/><p>";
        echo $row2["poll_14"]."</p><input type='radio' name='poll[]' class='radio' value='male'/></div>";
    }

         if(!empty($row2["poll_15"]) && !empty($row2["poll_15_image"])){
        echo  "<div class='col-xs-12 col-md-4 col-sm-12 center'></br><img src='admin/uploads/".$row2["poll_15_image"]."'/><p>";
        echo $row2["poll_15"]."</p><input type='radio' name='poll[]' class='radio' value='male'/></div>";
    }

         if(!empty($row2["poll_16"]) && !empty($row2["poll_16_image"])){
        echo  "<div class='col-xs-12 col-md-4 col-sm-12 center'></br><img src='admin/uploads/".$row2["poll_16_image"]."'/><p>";
        echo $row2["poll_16"]."</p><input type='radio' name='poll[]' class='radio' value='male'/></div>";
    }

         if(!empty($row2["poll_17"]) && !empty($row2["poll_17_image"])){
        echo  "<div class='col-xs-12 col-md-4 col-sm-12 center'></br><img src='admin/uploads/".$row2["poll_17_image"]."'/><p>";
        echo $row2["poll_17"]."</p><input type='radio' name='poll[]' class='radio' value='male'/></div>";
    }

         if(!empty($row2["poll_18"]) && !empty($row2["poll_18_image"])){
        echo  "<div class='col-xs-12 col-md-4 col-sm-12 center'></br><img src='admin/uploads/".$row2["poll_18_image"]."'/><p>";
        echo $row2["poll_18"]."</p><input type='radio' name='poll[]' class='radio' value='male'/></div>";
    }

         if(!empty($row2["poll_19"]) && !empty($row2["poll_19_image"])){
        echo  "<div class='col-xs-12 col-md-4 col-sm-12 center'></br><img src='admin/uploads/".$row2["poll_19_image"]."'/><p>";
        echo $row2["poll_19"]."</p><input type='radio' name='poll[]' class='radio' value='male'/></div>";
    }

        if(!empty($row2["poll_20"]) && !empty($row2["poll_20_image"])){
    }
 }
}
 else {
    header("Location: wrong_pin.php");
 }
 }

 if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
    echo $ip;
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    echo $ip;
} else {
    $ip = $_SERVER['REMOTE_ADDR'];
    echo $ip;
}
?>
<input type="submit" name="submit" value="Vote Now"/>
</div>
</form>
  </div>

3 个答案:

答案 0 :(得分:0)

@hassan gul考虑@ chris85评论。您目前有OR条件:

Uncaught TypeError: Cannot read property 'Autocomplete' of undefined

这意味着如果poll_1或poll_1_image实际上有值,您将显示if语句中的内容

if(!empty($row2["poll_1"]) || !empty($row2["poll_1_image"]))

仅显示以上两者都有值(例如:您可以从数据库中返回)。

如果您的目标是让所有人都能够工作并进行显示,那么您需要在所有行中读取数据库中的数据

答案 1 :(得分:0)

你搞砸了||&&||的情况下,如果满足任何条件,则执行if-statement(true)。另一方面,只有在使用&&时两个条件都为真时才执行它。 试试这个:

if(!empty($row2["poll_1"]) && !empty($row2["poll_1_image"])){
    echo  "<div class='col-xs-12 col-md-4 col-sm-12 center'></br><img src='admin/uploads/".$row2["poll_1_image"]."'/>";
    echo $row2["poll_1"]."</p><input type='radio' name='poll[]' class='radio' value='male'/></div>";
}

修改 您在打开/关闭<p>标记时出错,请使用正确的语法。

答案 2 :(得分:0)

我可能会建议制作与您的poll[]输入相关的具体数组键(例如,它们将是poll[poll_1]等。这可能会有所帮助,也可能没有帮助,这取决于您在做什么时做什么处理提交后的输入),然后使用for循环创建一个函数。如果你这样做或者不需要||&&,我就无法破译,但看起来你需要&&

<?php
    // Put this function declaration at the top of the page.
    // Better would be to put it on entirely different page and include the page at the top
    function makecheckbox($array = false, $key = false)
        {
            if(!empty($array[$key]) && !empty($array[$key."_image"])) { ?>
            <div class="col-xs-12 col-md-4 col-sm-12 center"><br />
                <img src="admin/uploads/<?php echo $array[$key."_image"]; ?>"/>
                <p><?php echo $array[$key]; ?></p>
                <input type="radio" name="poll[<?php echo $key; ?>]" class="radio" value="male"/>
            </div>
            <?php
                }
        }
?>

要在您的代码中使用

    while ($row2 = mysqli_fetch_assoc($slct_data)) {
        echo "<div class='col-xs-12 col-md-12 col-sm-12 text-center back'>".$row2['poll_title']."</div>";

        // Inside your while, use a for in place of all your "if" conditions
        for($i = 1; $i <= 20; $i++)
            makecheckbox($row2,'poll_'.$i);
    }

如果您创建了一个函数,几乎可以保证您的输出是相同的(您可能会复制并粘贴错误或更改一行而忘记更改其他等等。) 。它还可以清理页面并更容易发现语法错误。