drdl18946 2015-03-26 16:25
浏览 25
已采纳

从一个表中访问ID并将其插入另一个表 - 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. This form is in a file called "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. At the moment everything is posted into the database other than the "courseID" which takes the value of "0" when posted in the database.

The form action file "item_add.php" contains the code to submit the information:

<?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.

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. I have been struggling to access the variable from one file and inserting it into another basically.

Any help?

  • 写回答

1条回答 默认 最新

  • doudilin1225 2015-03-26 17:27
    关注

    Apologies, I realised I was missing a "$_POST" variable in "item_add.php" that retrieved the courseID from the dropdown.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择