简体   繁体   English

从一个表访问ID并将其插入到另一个表-PHP

[英]Accessing the ID from one table and inserting it into another table - PHP

Currently I have a form which has dropdown list that contains values that have been accessed from the "Category" table, which has two fields, courseID and cName. 当前,我有一个具有下拉列表的表单,该列表包含从“类别”表访问的值,该表具有两个字段CourseID和cName。 This form is in a file called "add_item.php". 该表格位于名为“ add_item.php”的文件中。

<form action="../adminscripts/item_add.php" method="post" class="addItem">
    <label>2) Select Course</label>
                        <br>
                    <select class="dropdown" name="category">
                    <?php 
                    include "..database/connect.php";
                    $query = mysqli_query($conn, "SELECT * FROM courses");
                    while ($row = mysqli_fetch_array($query)){
                    echo "<option value='". $row['cName'] ."'>" . $row['cName'] . " </option>";

                    }
                        ?>

 </select>
 <input class="send" type="submit" value="Add Menu Item" name="itemSubmit">
         </form>

What I am trying to achieve is that when the user selects one of these values, and clicks the "itemSubmit" button, I want the courseID to be posted in the table "menu" along with all the other information on the form. 我想要实现的是,当用户选择这些值之一,然后单击“ itemSubmit”按钮时,我希望将courseID与表格上的所有其他信息一起发布在表“菜单”中。 At the moment everything is posted into the database other than the "courseID" which takes the value of "0" when posted in the database. 目前,除“ courseID”外,所有内容都已发布到数据库中,“ courseID”在数据库中发布时取值为“ 0”。

The form action file "item_add.php" contains the code to submit the information: 表单操作文件“ item_add.php”包含提交信息的代码:

<?php
session_start();
    require "../database/connect.php";

    if(isset($_POST['itemSubmit']))
    {
$query2 = mysqli_query($conn, "INSERT INTO menu (mName, courseID, description, price) VALUES ('$itemname', '$courseID', '$item_description', '$price')" ) or die (mysqli_error($conn));

            header("Location:../admin/add_item.php");


            exit;
        }

I have tried to play around with global variables with no luck, hence me having "$courseID" in the above form. 我尝试使用全局变量没有任何运气,因此我在上面的表单中有“ $ courseID”。

I have not included the file "item_add.php" in the "add_item.php" file because I only need it to fulfil the form action. 我没有在“ add_item.php”文件中包含文件“ item_add.php”,因为我只需要它即可完成表单操作。 I have been struggling to access the variable from one file and inserting it into another basically. 我一直在努力从一个文件访问变量,然后基本上将其插入另一个文件。

Any help? 有什么帮助吗?

Apologies, I realised I was missing a "$_POST" variable in "item_add.php" that retrieved the courseID from the dropdown. 抱歉,我意识到我在“ item_add.php”中丢失了一个“ $ _POST”变量,该变量从下拉列表中检索了courseID。

This being the variable I was missing 这是我所缺少的变量

$courseID = $_POST['category'];

I also changed this line 我也改变了这一行

echo "<option value='". $row['cName'] ."'>" . $row['cName'] . " </option>";

to this 对此

echo "<option value='". $row['courseID'] ."'>" . $row['cName'] . " </option>";

so it could retrieve the courseID. 因此它可以检索courseID。

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

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