gpt4 book ai didi

php - 需要将选择 ID 作为表单的一部分提交,我该怎么做?

转载 作者:行者123 更新时间:2023-11-29 17:33:08 25 4
gpt4 key购买 nike

我需要将下面表单中的 Choice_ID 提交到我的数据库,因为它在另一个函数中使用。我如何将其传递给处理用户注册的 PHP 代码?我收到的错误消息是:

注意:未定义索引:/Users/philip/sites/feedmefit/Assets/register.php 第 16 行中的 Choice_ID

注意:未定义索引:/Users/philip/sites/feedmefit/Assets/register.php 第 17 行中的 Choice_ID

HTML 代码:

<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse1">Personal Details</a></h4>
</div>
<div id="collapse1" class="panel-collapse collapse in">
<div class="panel-body">
<form action="Assets/register.php" method="post">
<label>Forename:</label><br><input type="text" id="f" name="Forename" placeholder="Forename" required><br/>
<label>Surname:</label><br><input type="text" id="s" name="Surname" placeholder="Surname" required><br/>
<label>House Number:</label><br><input type="text" id="h" name="House_No" placeholder="House Number" required><br/>
<label>Street Name:</label><br><input type="text" id="sn" name="Street_Name" placeholder="Street Name" required><br/>
<label>City:</label><br><input type="text" id="c" name="City" placeholder="Town/City" required><br/>
<label>Postcode:</label><br><input type="text" id="p" name="Postcode" placeholder="Postcode" required><br/>
<label>Username:</label><br><input type="text" id="u" name="Username" placeholder="Username" required><br/>
<label>Password:</label><br><input type="text" id="pd" name="Password" placeholder="Password" required><br/>
<label>Email:</label><br><input type="text" id="e" name="Email" placeholder="Email" required><br/>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse2">Questionnaire</a>
</h4>
</div>
<div id="collapse2" class="panel-collapse collapse">
<div class="panel-body">
<?php
$sql = "SELECT Question_ID, Question FROM Questions;";

$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);

if($resultCheck > 0):
while($row = mysqli_fetch_assoc($result)):
$questionid = (int)$row['Question_ID'];
echo '<label>'.$row['Question'].'</label><input type="hidden" value="'.$row['Question_ID'].'">';

$query = "SELECT Choice_ID, Choice FROM Choices WHERE Question_ID = '$questionid';";
$results = mysqli_query($conn, $query);
$resultsCheck = mysqli_num_rows($results);
if($resultsCheck > 0):
$string = '<br><select id="choice">';
while($rows = mysqli_fetch_assoc($results)):
$string .= '<option value="'.$rows['Choice_ID'].'">'.$rows['Choice'].'</option>';
endwhile;
$string .= '</select><br>';
echo $string;
endif;
endwhile;
endif;
?>
<br>
<input type="submit" value="Register"/>
</form>
</div>
</div>
</div>
</div>

这是处理注册的 PHP:

<?php
session_start();
//Connect to DB
include 'DBConnection.php';

$Forename = mysqli_real_escape_string($conn, $_POST['Forename']);
$Surname = mysqli_real_escape_string($conn, $_POST['Surname']);
$House_No = mysqli_real_escape_string($conn, $_POST['House_No']);
$Street_Name = mysqli_real_escape_string($conn, $_POST['Street_Name']);
$City = mysqli_real_escape_string($conn, $_POST['City']);
$Postcode = mysqli_real_escape_string($conn, $_POST['Postcode']);
$Username = mysqli_real_escape_string($conn, $_POST['Username']);
$Password = mysqli_real_escape_string($conn, $_POST['Password']);
$Email = mysqli_real_escape_string($conn, $_POST['Email']);

$choice = $_POST['Choice_ID'];
print_r($_POST['Choice_ID']);
exit();

$sql = "INSERT INTO Customers (Forename, Surname, House_No, Street_Name, City, Postcode, Username, Password, Email)
VALUES ('$Forename', '$Surname', '$House_No', '$Street_Name', '$City', '$Postcode', '$Username', '$Password', '$Email')";
$query = "INSERT INTO Results (Result_ID, Cust_ID, Choice_ID)
VALUES(,,'$choice')";
$result = $conn->query($sql);
$results = $conn->query($query);

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

最佳答案

您没有给自己起名字<select>输入。你必须改变<select id="choice"><select id="choice" name="Choice_ID'.$i.'">

现在您必须更改添加以下代码以增加 $i对于每个问题。

$sql = "SELECT Question_ID, Question FROM Questions;";

$i=0;
$sql = "SELECT Question_ID, Question FROM Questions;";

                endif;
endwhile;
endif;

                endif;
$i++;
endwhile;
endif;

现在在您的注册 php 中您必须阅读 $choice0 = $_POST['Choice_ID0']; $choice1 = $_POST['Choice_ID10'];等等,并对它们每个进行不同的插入查询。

此外,您的代码很容易受到 SQL 注入(inject)攻击。 mysqli_real_escape_string并不能完全保护您,您应该使用准备好的语句。

关于php - 需要将选择 ID 作为表单的一部分提交,我该怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50488074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com