我对 php 和 sql 很陌生,并且在创建 Web 应用程序时正在学习,所以我想知道这将如何实现,如果有的话,或者对于这类任务是否有更好的做法。
举个例子:假设我有一个有 2 个表的数据库,一个是作者,另一本书
author table
-----------
id | author
1 | jones
2 | jane
book table
----------
id | title | author
1 | Learning php | 1 <- i want this to reference the author from author table
2 | Foobar | 1
3 | How to give a proper high five | 2
我有一个表单,它从查询中获取作者姓名作为选择标签,当我提交表单时,我想将 id 分配为 book 表中作者的值
$authorQuery = mysqli_query($con, "SELECT * FROM author");
<form id="bookForm" action="books.php" method="POST">
<label for="title">Book Title</label>
<input type="text" name="title">
<label for="author">Author</label>
<select id="author" name="author">
<?php
while($row = mysqli_fetch_array($authorQuery)) {
//display as an array in a drop down list
echo '<option>' . $row['author'] . '</option>';
}
?>
</form>
我该怎么办?
郎朗坤
精慕HU