mysql> SELECT * FROM main_table;
+--------+----------+--------+
| id_book | id_author |description |
+--------+----------+--------+
| 1 | 101 | I love cat |
+--------+----------+--------+
mysql> SELECT * FROM author;
+---------+-----------+
| id_author | name_author |
+---------+-----------+
| 101 | Dr Sent |
+---------+-----------+
mysql> SELECT * FROM book;
+--------+---------+
| id_book | name_book |
+--------+---------+
| 1 | cat |
+--------+---------+
你好,我现在仍然是PHP的新手,现在有点混淆了PHP和MySQL。我有一个drop drown列表,它与我在数据库中的一个表有关。
首先我的系统显示一个列表作者姓名的页面,用户可以选择一个用户喜欢的。
JK ROWLING
DR SEUSS <-- author_name
ROAD DAHL
接下来,它将转到一个新页面,并有一个选择(列表/菜单),显示一个BOOK NAME列表。 我可以从数据库中检索下拉列表,但我的问题是当我在下拉列表中选择一个BOOK NAME时,它显示所有书籍描述并且无法选择正确的描述。
这是我的编码,以使其更清晰
$currentPage = $_SERVER["PHP_SELF"];
mysql_select_db($database_config, $config);
$query_Recordset1 = "SELECT * FROM book ORDER BY name_book ASC";
$Recordset1 = mysql_query($query_Recordset1, $config) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
$maxRows_Recordset2 = 1;
$pageNum_Recordset2 = 0;
if (isset($_GET['pageNum_Recordset2'])) {
$pageNum_Recordset2 = $_GET['pageNum_Recordset2'];
}
$startRow_Recordset2 = $pageNum_Recordset2 * $maxRows_Recordset2;
$colname_Recordset2 = "-1";
if (isset($_GET['id_book'])) {
$colname_Recordset2 = $_GET['id_book'];
}
mysql_select_db($database_config, $config);
$query_Recordset2 = sprintf("SELECT * FROM main_table WHERE id_book = %s ORDER BY description ASC", GetSQLValueString($colname_Recordset2, "int"));
$query_limit_Recordset2 = sprintf("%s LIMIT %d, %d", $query_Recordset2, $startRow_Recordset2, $maxRows_Recordset2);
$Recordset2 = mysql_query($query_limit_Recordset2, $config) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
if (isset($_GET['totalRows_Recordset2'])) {
$totalRows_Recordset2 = $_GET['totalRows_Recordset2'];
} else {
$all_Recordset2 = mysql_query($query_Recordset2);
$totalRows_Recordset2 = mysql_num_rows($all_Recordset2);
}
$totalPages_Recordset2 = ceil($totalRows_Recordset2/$maxRows_Recordset2)-1;
$queryString_Recordset2 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
$params = explode("&", $_SERVER['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_Recordset2") == false &&
stristr($param, "totalRows_Recordset2") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_Recordset2 = "&" . htmlentities(implode("&", $newParams));
}
}
$queryString_Recordset2 = sprintf("&totalRows_Recordset2=%d%s", $totalRows_Recordset2, $queryString_Recordset2);
答案 0 :(得分:0)
试试这个.........
SELECT
a.*, b.*, c.*
FROM main_table a
inner join author b on a.id_author = b.id_author
inner join book c on a.id_book = c.id_book
答案 1 :(得分:0)
首先,我建议使用AJAX并在选择项目时停留在同一页面上。通过这种方式,您可以分离不同输入框的代码,并且在一天结束时看起来会更好。
要回答你的问题,这里是mySQL的SQL语句,你应该运行以获得你要求的内容:
SELECT MAT.*,AUT.name_author, BOO.name_book
FROM main_table AS MAT, author AS AUT, book AS BOO
WHERE (MAT.id_book=BOO.id_book AND MAT.id_author=AUT.id_author) AND MAT.id_author=101
ORDER BY MAT.id_author, MAT.id_book;
你可以找到上面那个人here的SQL小提琴代码。 您可以更新WHERE
子句中的“过滤”,以按另一个表的作者ID或书籍ID进行过滤