如何在PHP中创建下拉查找字段?

时间:2018-04-24 23:51:11

标签: php html mysql mysqli mysql-workbench

这里的PHP新手,

我在MySQL Workbench中有一个包含表格的库数据库:Student,Book和Checkin。我正在尝试使用HTML和PHP在网页上显示它,到目前为止它一直很好,直到尝试在签入表中显示s_id为" s_name"这是学生姓名,b_id到b_name也是一样。我已经在Workbench中创建了外键连接,但无法弄清楚如何创建一个下拉选项,该选项显示我已经在各自的表(而不是s_id和b_id)中创建的学生姓名和书名。

在MS Access中查找字段非常容易,但创建PHP版本已经证明作为初学者很难......现在就是它的样子。

This is what it looks like:

        <?php
                include('conn.php');
                $query=mysqli_query($conn,"select * from `checkin`");
                while($row=mysqli_fetch_array($query)){
                    ?>

                    <tr>
                        <td><?php echo $row['c_id']; ?></td>
                        <td><?php echo $row['s_id']; ?></td>
                        <td><?php echo $row['b_id']; ?></td>
                        <td><?php echo $row['c_date']; ?></td>
                        <td><?php echo $row['c_in']; ?></td>
                        <td>
                            <a href="edit_checkin.php?id=<?php echo $row['c_id']; ?>">Edit</a>
                            <a href="delete_checkin.php?id=<?php echo $row['c_id']; ?>">Delete</a>
                        </td>
                    </tr>
                    <?php
                }
            ?>
        </tbody>
    </table>
</div>

非常感谢任何帮助或提示!

1 个答案:

答案 0 :(得分:0)

现在是,现在准备好了查询:

lib4.php

<form action='add_borrowing1.php' method='post'>
          <br>
          <select name="student">
        <?php
          $link = mysqli_connect("localhost", "root", "", "library");
          $sql = "SELECT DISTINCT studentId, studentName
          FROM students";
          $result = mysqli_query($link, $sql);
          print "<option value='students' selected>Students</option>";
          While ($row = mysqli_fetch_assoc($result))
            {
            print "<option value=" . $row["studentId"] . ">"
          . $row["studentName"] . "</option>";
            }
        ?>
          </select>
                    <p>Book:</p>
          <br>
          <select name="book">
        <?php
          $link = mysqli_connect("localhost", "root", "", "library");
          $sql = "SELECT DISTINCT bookId, bookName
          FROM books";
          $result = mysqli_query($link, $sql);
          print "<option value='books' selected>Books</option>";
          While ($row = mysqli_fetch_assoc($result))
            {
            print "<option value=" . $row["bookId"] . ">"
          . $row["bookName"] . "</option>";
            }
        ?>
          </select>
          <input type="submit" />
          </form>

          <?php
          if (!isset($_GET["raw"])) {
            echo "";
          }
          else {
            echo "Borrowing has been successfully added!";
          }

?>

add_borrowing.php
======================
<?php
$book = $_POST["book"];
$student = $_POST["student"];
$link = mysqli_connect("localhost", "root", "", "library");
        $sql = "INSERT INTO borrowing (bookId, studentId) VALUES (?,?)";


$stmt = mysqli_prepare($link, $sql);

            /*
            Explanation:
            i Integers
            d Doubles
            b Blobs
            s Everything Else 
            */
            mysqli_stmt_bind_param($stmt, "ii", $book,
                                   $student);
            mysqli_stmt_execute($stmt);
                mysqli_stmt_close($stmt);
                mysqli_close($link);

header("Location:lib4.php?raw=1");
?>