简体   繁体   English

将ID从一个表发布到另一个表

[英]POSTING an ID from one table to another

I have two tables in db, input table nd output table. 我在数据库中有两个表,输入表和输出表。 input table got two columns $ID (autoincreament), $question . 输入表有两列$ID (autoincreament), $question table 2 got two columns $Q_ID, Answer . 表2有两列$Q_ID, Answer

i made a php page (1) where i printed out the table 1 content (QUESTION) and i added a text area to get answers from visitors, along with it i also added a echo "hidden" field for ID from table one. 我制作了一个php页面(1),在其中打印了表1的内容(问题),并添加了一个文本区域以获取访问者的答案,同时我还为表1的ID添加了回显“隐藏”字段。

Now this php page 1 redirects to php page 2, where i POST the text area input into the answer column in table 2. But unfortunately, the ID is not able to get posted in q_id column of table 2... I tried a lot but no hope.. 现在,此php页面1重定向到php页面2,在此我将输入到表2中的答案列的文本区域发布到表2。但是不幸的是,该ID无法发布在表2的q_id列中...我尝试了很多但没有希望..

My PHP script #1 as follows :: 我的PHP脚本#1如下 ::

<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++; // stuff inside foreach goes here 
            ?>

            <div id="q">
                <B><big><font color= #ba4a00> Q:</font></big> <?php echo $row["question"]; ?> 

                <?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">'; 
                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>';
                ?>
            </div>
        <?php     
        }
    } else {
        echo "0 results";
    }
    $conn->close();
    ?>
</div>

My PHP script #2 as follows :: 我的PHP脚本#2如下 ::

<?php include('1.php'); ?>
<?php
    $servername  = "localhost";
    $dbusername = "root";
    $dbpassword = "*******";
    $dbname = "the_database";
    $addtext = $_POST['addtext'];
    $questionid = $_POST['questionid'];
    $date = date_default_timezone_set('Asia/Kolkata');
    $date = date('M-d,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 (question_id, answer, date)
    VALUES ('$questionid', '$addtext', '$date')";
    if ($conn->query($sql) === TRUE) {
    echo '<script language="javascript">';
    echo 'alert("Your Answer has been Succesfully posted")';
    echo '</script>';
    echo '<a href="1.php"></a>';
    }
    else {
    echo "ERROR" . $sql . "<br>" . $conn->error;
    }
    $conn->close();
 ?>

i want the respective question id to be passed to table 2 along with its answer. 我希望将相应的问题ID及其答案传递给表2。
Any help is greatly appreciated.. 任何帮助是极大的赞赏..

try enclosing id in double quotes in php 1: 尝试将ID括在php 1中的双引号中:

echo '<input type="hidden" name="questionid" value="'.$row['id'].'"/>';

yip... looking a this a bit more closely you've got a bit of a problem with mixing single and double quotes in PHP #1. 是的...仔细看一下,您在PHP#1中混合使用单引号和双引号时遇到了一些问题。

for example, on this line the echo command ends after the second single quote after add_ 例如,在这一行上,echo命令在add_之后的第二个单引号之后结束

echo '<button class="add" id="add_'.$index.'"><B>Add Answer</B></button>';

You need to use either single or double quotes consistently. 您需要始终使用单引号或双引号。

Give single-quote in $row['id'] . $row['id']给出单引号。 Change this <?php echo $row[id] >? 更改此<?php echo $row[id] >? to <?php echo $row['id'] >? <?php echo $row['id'] >?

echo '<input type="hidden" name="questionid" value="'. $row['id'].'"/>';

Already you are echoing the form using the ECHO Attribute in the PHP but after that also you have echoed it and that is wrong. 您已经在PHP中使用ECHO属性来回显表单,但是之后您也已经回显了它,这是错误的。

You have to make the code like this. 您必须像这样编写代码。

echo '<input type="hidden" name="questionid" value="'. $row['id'].'"/>';

Try this and share the thought about the code.. 试试这个,并分享有关代码的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM