我在数据库中有一个表,其中包含ID,问题,日期等列。和一个php页面(1.php),它从db表输出问题和日期,并在textarea中获取答案。当点击提交时,它会重定向到另一个php页面(2.php),我应该将键入的答案存储到db中的另一个表中。
在这里,我的问题是我如何将db_ad中的第二个表中的question_ID添加到答案中。
1.PHP脚本如下::
<div class="name">
<?php
$sql = "SELECT * FROM input";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$index = 0;
while($row = $result->fetch_assoc()) {
$index++;
?>
<div id="q">
<?php echo $row["question"]; ?> </B>
<?php
echo '<button class="add" id="add_'.$index.'"><B>Add Answer</B></button>';
echo '<form style="display:none;" name="answer_'.$index.'" method="post" action="output.php">'; // I dont think openning form from row to row would be nice!
echo '<textarea type="text" class="addtext" name="addtext" required id="addtext_'.$index.'" placeholder="Please type your answer here.." ></textarea>';
echo '<button onClick="addsubmit('.$index.');" type="submit" id="addsubmit_'.$index.'" class="addsubmit"><B>Submit</B></button>';
echo '</form>';
?>
<small><p><?php echo $row["date"]; ?></p></small>
2.PHP脚本如下::
<?php include('1.php'); ?>
<?php
$servername = "localhost";
$dbusername = "root";
$dbpassword = "******";
$dbname = "the_database";
$addtext = $_POST['addtext'];
$date = date_default_timezone_set('Asia/Kolkata');
$date = date('d/m/Y H:i:s');
$conn = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO output (answer, date)
VALUES ('$addtext', '$date')";
if ($conn->query($sql) === TRUE) {
echo '<script language="javascript">';
echo 'alert("Your Answer has been Succesfully posted")';
echo '</script>';
echo '<a href="answer.php"></a>';
}
else {
echo "ERROR" . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
成功保存在db表中的答案和日期,但在获取question_ID ...
时遇到困难任何帮助都会得到极大的赞赏。
答案 0 :(得分:1)
添加隐藏字段:
on 1.php
echo '<form style="display:none;" name="answer_'.$index.'" method="post" action="output.php">'; // I dont think openning form from row to row would be nice!
echo '<input type="hidden" name="questionid" value="<?php echo $row[ID]?>"/>'
echo '<textarea type="text" class="addtext" name="addtext" required id="addtext_'.$index.'" placeholder="Please type your answer here.." ></textarea>';
echo '<button onClick="addsubmit('.$index.');" type="submit" id="addsubmit_'.$index.'" class="addsubmit"><B>Submit</B></button>';
echo '</form>';
第2.pp页
$qid = $_POST['questionid']; //get the question id and insert in the table