【问题标题】:Showing error message on correct page in PHP在 PHP 的正确页面上显示错误消息
【发布时间】:2022-01-18 03:25:48
【问题描述】:

我的 PHP 项目中有 4 个表单。 Index.php 将存储用户的nameid 号码,然后他们可以单击下一步并将他们带到Form2.php。 Form2.php 将存储他们的一些随机答案,Form3.php 将执行与 Form2 相同的操作,Form4.php 将存储一些详细信息,然后用户可以单击提交,记录应保存在我的数据库中。我遇到的问题是我的ID 数字字段是一个唯一字段,如果ID 输入与数据库中的输入相同,我希望在用户单击下一步时在Index.php 上显示错误。目前,它在最后一个表单中单击提交按钮后显示。有没有办法做到这一点?

索引.php

<body>
    <center>
        <div class="div2">
            <h1>Welcome</h1>
            <form action="form2.php" method="post">
                <p>
                    <label for="firstName">Named:</label>
                    <input size="30" class="rounded-input" type="text" name="name" id="name" autocomplete="off" required>
                </p>
                <p>
                    <label for="lastName">S ID:</label>
                    <input size="30" class="rounded-input" type="text" name="Sid" id="Sid" autocomplete="off" required>
                </p>
                <input class="btn" type="submit" value="Next" style="float: right;">
            </form>
        </div>
    </center>
</body>

Form2.php:

<?php
session_start();
$_SESSION['name'] = $_POST['name'];
$_SESSION['Sid']  = $_POST['Sid'];
?>
<div class="div2">
    <h1>How disappointed would you be if this product ceased to exist?</h1>
    <form action="form3.php" method="post">
        <input type="radio" style="height:20px; width:20px;" required 
                name="product_exist_satisfaction" 
                <?php if (isset($product_exist_satisfaction) && $product_exist_satisfaction == "Very disappointed") echo "checked"; ?> 
                value="Very disappointed">
        <label style="font-size: 20px;"> Very disappointed</label><br />

        <input type="radio" style="height:20px; width:20px;" required 
                name="product_exist_satisfaction" 
                <?php if (isset($product_exist_satisfaction) && $product_exist_satisfaction == "Mildly disappointed") echo "checked"; ?> 
                value="Mildly disappointed">
        
        <label style="font-size: 20px;"> Mildly disappointed</label><br />

        <input type="radio" style="height:20px; width:20px;" required 
                name="product_exist_satisfaction" 
                <?php if (isset($product_exist_satisfaction) && $product_exist_satisfaction == "Not at all") echo "checked"; ?> 
                value="Not at all">
        <label style="font-size: 20px;"> Not at all</label><br />
        <input type="button" onclick="history.back()" 
                value="Previous" style="float: left;">
        <input type="submit" value="Next" style="float: right;">
    </form>
</div>

插入.php

<?php
session_start();
?>
<body>
    <div class="div2">
<?php
    $conn = mysqli_connect("localhost", "root", "", "survey");
    if ($conn === false) {
        die("ERROR: Could not connect. "
                . mysqli_connect_error());
    }
    $stmt = $conn->prepare('insert into `cus_survey` 
                    ( `fullname`, `Sid`, `product_exist_satisfaction`,
                      `system_battery_runout`, `rank_appliances` ) 
                values (?, ?, ?, ?, ?)');
    $stmt->bind_param('sssss', $_SESSION['fullname'], $_SESSION['Sid'], 
                               $_SESSION['product_exist_satisfaction'], 
                               $_SESSION['system_battery_runout'], 
                               $_POST['rank_sequence']);
    $stmt->execute();
    $msg = ($stmt->affected_rows == 1) 
                ? 'Your survey was captured successfully. Thank You!'!' 
                : 'Sorry, your S ID is used already, Please use another and resubmit.' . "<h3><a href='/index.php'>Click here to edit your S ID</a></h3>" . mysqli_connect_error();
    $stmt->close();
    $conn->close();
    printf('<h3>%s</h3>', $msg);
?>
    </div>
</body>

【问题讨论】:

  • 为什么不尽快检查该 ID 并打印错误消息?你试过什么了?你被困在哪里了?
  • @NicoHaase,我尝试使用一些 js 在单击下一个按钮时输出错误,但这没有用,所以我最终删除了它......我将如何检查在Index.php 表格中识别并打印?
  • “我如何检查 ID” - 通过编写 SELECT 查询,过滤该 ID,检查是否返回任何行?
  • @NicoHaase,我知道,但是,由于 ID 被存储为会话,它不会失败吗?
  • 您必须运行查询以查看 Sid 是否以您希望完成检查的任何形式存在,然后返回 Ok/Fail 并采取相应措施

标签: php html mysql


【解决方案1】:

我没有检查代码,但基本步骤是:

-在您的 index.php 中,您可以将索引发布给自己,方法是更改​​为 action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"

-然后您将为 Sid 运行选择查询

-如果 Sid 不在 sql 表中,那么您将重定向到 form2.php

-否则你已经有 Sid,然后设置错误消息,然后显示带有错误消息的索引表单。

<?php
// Start/resume sessions
if (session_status() !== PHP_SESSION_ACTIVE) {
  session_start();
}

//set your error message to empty string
$error_message = "";

// ensure that you actually have values to check
if ((isset($_POST['name'])) &&(isset($_POST['Sid'])))  {


// use select statement to verify that the Sid is not already in table

$sql_check = 'SELECT Sid FROM cus_survey...
//...
//... put your check on the result of select statement

if (Sid not already there) {

   // redirect to form 2
   header("Location: form2.php");   
}else{
    // If Sid already used found, populate the error message, which will get displayed in your body
    $error_message = "Sorry, your S ID is used already, Please use another and submit.";

}
}
?>

<body>
    <center>
        <div class="div2">
        <?php if ($error_message != ""){
            ?>          
            <h1>Oops: <?php echo "{$error_message}"; ?></h1>
            <?php
            }else{
            ?>
            <h1>Welcome</h1>
            <?php
            };
            ?>
            
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                <p>
                    <label for="firstName">Named:</label>
                    <input size="30" class="rounded-input" type="text" name="name" id="name" autocomplete="off" required>
                </p>
                <p>
                    <label for="lastName">S ID:</label>
                    <input size="30" class="rounded-input" type="text" name="Sid" id="Sid" autocomplete="off" required>
                </p>
                <input class="btn" type="submit" value="Next" style="float: right;">
            </form>
        </div>
    </center>
</body>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    相关资源
    最近更新 更多