【问题标题】:How to fix 'php skipping my if statement' [duplicate]如何修复'php跳过我的if语句'[重复]
【发布时间】:2019-04-10 16:21:58
【问题描述】:

我有一个 html/php 页面,您只能从复选框值中选择 2 或以下。我使用 javascript 限制了选择。但我担心的是,如果有人试图使用开发人员工具,他们可以简单地禁用我的脚本并从复选框中选择更多值。所以我认为我需要计算从复选框中获得了多少 POST 数据。

我尝试在 php 中使用 count() 来计算帖子并尝试回显计数,我得到了正确的数字。然后我尝试使用 if 语句,如果所选内容超过 2,它将回显。它有效,但是当我使用 header('Location:') 时,它会跳过它。

$countpres = count($_POST['pres']);
if($countpres>=3){
   header('Location:../index.php');
}
for($i=0;$i<$countpres;$i++)
{
$sql1 = "UPDATE candidate_list SET vote_count= vote_count + 1 WHERE candidate_number = '". $_POST['pres'][$i]. "' ";
mysqli_query($conn, $sql1);
}   

如果用户尝试发送超过 2 个选项,我需要转到 index.php。

【问题讨论】:

  • 他们可以选择 2 个值。对不起
  • 是的,我试过了。但是为什么我不去 index.php
  • 是的。我所有的复选框都命名为 pres[]
  • 您需要在header() 之后添加一个exit() 否则它将继续 - stackoverflow.com/questions/3553698/…
  • 当心!您的代码容易受到 SQL 注入的攻击。更多信息请查看What is SQL injectionHow to prevent

标签: javascript php jquery html


【解决方案1】:

对于 php7

header("location: index.php",  true,  301 );  exit;

对于 php5

header("Location: index.php");
exit();

如果仍然无法正常工作并且无法重定向,请使用:

echo '<script>windows.location = "index.php";</script>'; exit();

或者

echo "<script type='text/javascript'>window.top.location='index.php';</script>"; exit();

将 index.php 替换为您的文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-26
    • 2019-02-26
    • 1970-01-01
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 2018-01-11
    • 1970-01-01
    相关资源
    最近更新 更多