gpt4 book ai didi

php - 显示内容的表格下拉列表

转载 作者:行者123 更新时间:2023-11-29 21:02:27 26 4
gpt4 key购买 nike

此脚本允许用户从下拉列表中选择一个表,然后显示所选表的内容。

  1. 目前数据库连接正在工作。
  2. 下拉列表中显示表格列表。

问题:未显示数据库表中的内容。我已经检查了代码,一切看起来都不错,但它似乎仍然只能部分工作。

<?php 
//update this to your DB connection details.
$dbh = "localhost";
$dbn = "dbname";
$dbu = "dbuser";
$dbp = "dbpass";

$conn = mysql_connect($dbh,$dbu,$dbp) or die("Unable to connect do database.");
mysql_select_db($dbn, $conn) or die("Unable to select database.");

//Some vars for Order by and Limit.
if (!isset($ordBy)){
$ordBy = 1;
}
if (!isset($ps)){
$ps = 0;
}
if (!isset($ord)){
$ord = 1;
}
if ($ord == 1){
$tOrder = "ASC";
} else {
$tOrder = "DESC";
}

//Tables drop-down

$result = mysql_query("SHOW TABLES FROM $dbn") or die("Cannot list table names.");
echo "
<form name=\"table_browser\" action=\"".$PHP_SELF."\" method=\"GET\" >
<select name=\"t\" onChange=\"javascript:submit();\">
<option>Select a table</option>
";
while ($row = mysql_fetch_row($result)){
echo " <option value=".$row[0].">".$row[0]."</option>\n";
}
echo " </select>
</form>\n";

if (!isset($t)){
die("Please select a table");
}

//Get number of rows in $t and then select
$result = mysql_query("SELECT COUNT(*) FROM $t") or die("The requested table doesn't exist.");
$total = mysql_result($result,0);
$qry = "SELECT * FROM $t ORDER BY ".$ordBy." ".$tOrder." LIMIT ".($ps*20).",20 ";

if (isset($qry)) {
$result = mysql_query($qry) or die("The requested table doesn't exist.");
$i = 0;
while ($i < mysql_num_fields($result)) {
$meta = mysql_fetch_field($result);
if (!$meta) {
echo "No information available on the table<br />\n";
}
$name[$i] = $meta->name;
$i++;
}
//Display table details
echo "Rows ".($ps*20+1)." to ".((($ps+1)*20 < $total) ? (($ps+1)*20) : ($total))." of $total from table:

<b>$meta->table</b>\n<br /><br />\n";

//Count results
if ($ps > 0) {
echo "<a href=\"browse.php?t=$t&ps=".($ps-1)."&ordBy=$ordBy&ord=$ord\">20 Previous</a> - ";
} else {
echo "20 Previous - ";
}
if ((($ps+1)*20) < $total ){
echo "<a href=\"browse.php?t=$t&ps=".($ps+1)."&ordBy=$ordBy&ord=$ord\">Next 20</a>\n";
} else {
echo "Next 20\n";
}

//Column names
echo "<br /><br />\n<table>\n <tr>\n";
for ($j = 0; $j < $i; $j++){
echo " <td><b><a href=\"browse.php?t=$t&ps=$ps&ordBy=$name[$j]&ord=".(-$ord)."\">$name[$j]</a></b>";
if ($ordBy == $name[$j]) {

echo "<img src=\"images/arrow$ord.gif\">";
}
echo "</td>\n";
}
echo " </tr>\n";

//Data
while ($row = mysql_fetch_array($result)){
echo " <tr onmouseover=\"this.style.background='#DDDDDD'\" onmouseout=\"this.style.background=''\">";
for ($j = 0; $j < $i; $j++){
echo "<td>".$row[$name[$j]]."</td>";
}
echo "</tr>\n";
}
echo "</table>\n";
}
mysql_close();
?>

最佳答案

@gentlebreeze - 我的眼睛因为试图阅读而受伤 - 但我看不到你实际上在哪里设置名为 $t 的变量 - 它用于确定表。你应该有

$t = $_GET['t'];

行前某处:

....if (!isset($t)){....

因为你需要在行中使用它:

....$result = mysql_query("SELECT COUNT(*) FROM $t"....

并且您也不应该使用 mysql_query - 旧的且现已弃用。您应该切换到 PDO 和绑定(bind)变量。

关于php - 显示内容的表格下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37097368/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com