【问题标题】:Need help redirecting through PHP需要帮助通过 PHP 重定向
【发布时间】:2019-04-14 21:20:19
【问题描述】:

我目前正在开展一项调查。到目前为止,我已经取得了相当大的进步。如果参与者完成并提交调查,它会成功进入我的数据库并将参与者返回到我网站的主页,但如果参与者跳过了一个问题,当他们尝试提交调查时,它会返回空白显示并且没有数据输入我的数据库。

我想创建一个调查,让参与者能够跳过问题,并在提交调查后将它们返回到主页。

我知道这对某些人来说可能很容易,但我对 PHP 还很陌生,所以我还在学习,请参阅下面的代码。

<?php
$radioVal1 = $_POST["q1"];
$radioVal2 = $_POST["q2"];
$radioVal3 = $_POST["q3"];
$radioVal4 = $_POST["q4"];
$radioVal5 = $_POST["q5"];
$checkboxVal6 = $_POST["q6"];
$textAreaVal = $_POST["comment"];
session_start();
require_once('DBConnection.php');
if(isset($_POST["submit_questionnaire"])){
    if(isset($_POST["q1"]) && isset($_POST["q2"]) && isset($_POST["q3"]) && isset($_POST["q4"]) && isset($_POST["q5"]) && isset($_POST["q6"]) && $textAreaVal!=null){
        echo "blyat";
        $username = $_SESSION['username'];
        $db = new DBConnection();
        $conn= $db::getInstance()->dbConnect();
        $query_code = "SELECT id_code FROM users WHERE name = '$username'";
        $id_codeRow = $db::getInstance()->selectDB($query_code)->fetch_row();
        $id_code = (string)array_values($id_codeRow)[0];
        $query = "INSERT INTO questionnaire VALUES (default, '$id_code', '$radioVal1', '$radioVal2', '$radioVal3','$radioVal4', '$radioVal5', '$checkboxVal6', '$textAreaVal')";
        $insert_value = $db::getInstance()->selectDB($query);
        session_destroy();
        header("Location: index.php");
        exit();
    }

}

【问题讨论】:

  • 这是因为 insert 进入 db 并且如果很长的 if 语句不正确,则不会发生重定向,因此您需要执行例如重定向到 index.php 无论如何如果 if 语句是真是假
  • 警告:您对SQL Injections 持开放态度,应该真正使用参数化的prepared statements,而不是手动构建查询。它们由PDOMySQLi 提供。永远不要相信任何类型的输入,尤其是来自客户端的输入。即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your data

标签: php html redirect survey


【解决方案1】:

// 您可以将“&&”替换为“OR”,这样用户可以跳过任何问题,但用户必须至少完成 1 个问题。

<?php
$radioVal1 = $_POST["q1"];
$radioVal2 = $_POST["q2"];
$radioVal3 = $_POST["q3"];
$radioVal4 = $_POST["q4"];
$radioVal5 = $_POST["q5"];
$checkboxVal6 = $_POST["q6"];
$textAreaVal = $_POST["comment"];
session_start();
require_once('DBConnection.php');
if(isset($_POST["submit_questionnaire"])){
    if(isset($_POST["q1"]) OR isset($_POST["q2"]) OR isset($_POST["q3"]) OR isset($_POST["q4"]) OR isset($_POST["q5"]) OR isset($_POST["q6"])){
        echo "blyat";
        $username = $_SESSION['username'];
        $db = new DBConnection();
        $conn= $db::getInstance()->dbConnect();
        $query_code = "SELECT id_code FROM users WHERE name = '$username'";
        $id_codeRow = $db::getInstance()->selectDB($query_code)->fetch_row();
        $id_code = (string)array_values($id_codeRow)[0];
        $query = "INSERT INTO questionnaire VALUES (default, '$id_code', '$radioVal1', '$radioVal2', '$radioVal3','$radioVal4', '$radioVal5', '$checkboxVal6', '$textAreaVal')";
        $insert_value = $db::getInstance()->selectDB($query);
        session_destroy();
        header("Location: index.php");
        exit();
    }

}

【讨论】:

    猜你喜欢
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 1970-01-01
    相关资源
    最近更新 更多