【发布时间】:2022-01-18 03:25:48
【问题描述】:
我的 PHP 项目中有 4 个表单。 Index.php 将存储用户的name 和id 号码,然后他们可以单击下一步并将他们带到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 并采取相应措施