简体   繁体   English

php-使用以下命令向MySQL添加外键<option><select>

[英]php - Adding foreign key to MySQL using <option><select>

This is a follow up question to my previous question in here. 这是我在此之前的问题的跟进问题。 PHP - Dropdown menu(select) not working properly to while loop PHP-下拉菜单(选择)无法正常运行至while循环

I follow the code given to me and I think it is working correctly. 我遵循提供给我的代码,我认为它可以正常工作。 But my only concern is that, the query in my code is actually for the shelf table, and not for the book table which have the foreign key for the shelf. 但是我唯一关心的是,我的代码中的查询实际上是针对shelf表的,而不是针对具有书架外键的book表。 Here is my code for the textfield. 这是我的文本字段代码。 It is in a form which have a button that redirects to check_book.php to add it 它的形式是带有一个按钮,该按钮重定向到check_book.php以添加它

<div class="form-group">

      <label for="inputEmail" class="col-lg-2 control-label">Shelf</label>
      <div class="col-lg-10">
    <?php   
    $sel_admin = "select * from shelf";
    $rs_admin = mysql_query($sel_admin);
    echo '<select class="form-control">';
    while($row = mysql_fetch_array($rs_admin))
                {

                echo"<option value='" .$row['shelf_id']."'>" . $row['shelf_description'] ."</option>";
                }
                echo'</select>';
                ?>
      </div>
    </div>

It's giving me this error Notice: Undefined index: shelf_id in C:\\xampp\\htdocs\\OCatalog\\admin\\check_book.php on line 8 but the other non-foreign key textfield is just fine. 这给了我这个错误Notice: Undefined index: shelf_id in C:\\xampp\\htdocs\\OCatalog\\admin\\check_book.php on line 8但另一个非外键文本字段也很好。

And here is my check_book.php 这是我的check_book.php

$book_id = $_POST['book_id'];
    $book_title = $_POST['book_title'];
    $author_id = $_POST['author_id'];
    $book_quantity = $_POST['book_quantity'];
    $shelf_id = $_POST['shelf_id'];

How can I view all the shelf then adding it as a foreign key? 如何查看所有shelf然后将其添加为外键?

PS. PS。 The select is also in the form which when i click the submit button it will go to check_book.php 选择也采用以下form当我单击提交按钮时,它将转到check_book.php

您忘记在选择中添加名称属性

echo '<select class="form-control" name="shelf_id">';

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

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