php 用php数据库中的数据填充组合框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17930312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
filling combobox with data from php database
提问by Bassam Badr
this is my code
这是我的代码
<select>
<?php
$query = "SELECT * FROM 'sections'";
$result = mysql_query($query);
while($row=mysql_fetch_array($result)){
echo "<option value='".$row[id]."'>".$row[sectionName]."</option>";
}
?>
</select>
nothing happen I don't know why this my page maybe there is wrong some where
什么都没有发生 我不知道为什么这是我的页面 也许有些地方有问题
<?php
ob_start();
session_start();
include('../includes/connect.php');
include('../includes/phpCodes.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>???? ??????</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="../css/mainstyle.css">
<link rel="stylesheet" type="text/css" href="css/controlstyle.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#tabs div').hide();
$('#tabs div:first').show();
$('#tabs ul li:first').addClass('active');
$('#tabs ul li a').click(function(){
$('#tabs ul li').removeClass('active');
$(this).parent().addClass('active');
var currentTab = $(this).attr('href');
$('#tabs div').hide();
$(currentTab).show();
return false;
});
});
</script>
</head>
<body>
<div class="wrapper">
<?php headerCode(); ?>
<div class="content">
<div id="tabs">
<ul>
<li><a href="#add">????? ?????</a></li>
<li><a href="#remove">??? ?????</a></li>
<li><a href="#edit">????? ?????</a></li>
<li><a href="#edit">?????? ????????</a></li>
</ul>
<div id="add">
<form method="POST" action="includes/add.php" dir="rtl" enctype="multipart/from-data">
<br>
??? ????? :
<select>
<?php
$query = "SELECT * FROM 'sectionsd'";
$result = mysql_query($query);
while($row=mysql_fetch_array($result)){
echo "<option value='".$row[id]."'>".$row[sectionName]."</option>";
}?>
</select><br>
????? ??????? :<input type="text" name="title" class="mem-information"/><br>
??????? : <br /><textarea name="subject" rows="10" cols="50" class="mem-information" style="width: 500px"></textarea><br /><br>
?????? :<input type="file" name="image"><br>
<input type="submit" value="?????" name="send" class="log" style="color: black">
</form>
</div>
<div id="remove">
<form method="POST" action="includes/remove.php" dir="rtl"><br>
??? ????? :
<select name ="sectionsName">
<option value="">dd</option>
</select>
<input type="submit" value="???" name="send" class="log" style="color: black">
</form>
</div>
<div id="edit">
</div>
<div id="addDep">
</div>
</div>
</div>
</div>
</body>
</html>
sorry for my bad English
my table
对不起我的英语不好我的桌子
回答by vee
The problem I see is you are using single quotes on your table name in your query, you should either use back tick or no back ticks for table name. Please try the following code:
我看到的问题是您在查询中的表名上使用了单引号,您应该对表名使用反引号或不使用反引号。请尝试以下代码:
<?php
$query = "SELECT * FROM `sections`";
$result = mysql_query($query);
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
echo "<option value='".$row['id']."'>".$row['sectionName']."</option>";
}
?>
Also start looking into using mysqli
(http://php.net/manual/en/book.mysqli.php) or PDO
(http://php.net/manual/en/book.pdo.php), as mysql
is deprecated.
也开始考虑使用mysqli
( http://php.net/manual/en/book.mysqli.php) 或PDO
( http://php.net/manual/en/book.pdo.php),因为mysql
已弃用。
回答by JuanBonnett
Try this... And we are all Asuming the name of the column is "sectionsd" with the "d".
试试这个......我们都假设列的名称是“sectionsd”和“d”。
<?php
$query = "SELECT * FROM 'sectionsd'";
$result = mysql_query($query);
while($row=mysql_fetch_assoc($result)){
echo "<option value='".$row["id"]."'>".$row["sectionName"]."</option>";
}?>
</select>
If it's not working it may be due to:
如果它不起作用,可能是由于:
1) You're using a mysql_ function and you're not sending the parameter $link to the function. Like: mysql_query($query, $connectionlink);
1) 您正在使用 mysql_ 函数并且您没有将参数 $link 发送到该函数。如:mysql_query($query, $connectionlink);
2) The table name is wrong
2)表名错误
3) You have an error: Use mysql_query() or die(mysql_error()); to see what's going on
3) 你有一个错误:使用 mysql_query() 或 die(mysql_error()); 看看发生了什么
4) DO NOT use single quotes ' around your table name in the query
4) 不要在查询中的表名周围使用单引号 '
回答by norman
echo '
<option value='.$row["id"].'>'.$row["sectionName"].'</option>
';