我想从数据库表中检索记录并从章节表中获取id,title,我想在select标签中显示章节名称。
当用户选择任何一章时,我想获得章节的id并通过表格发布。
我尝试添加select查询但是我没有得到如何在select标签中显示这个并获取选择章节的id。
<?php
ini_set('display_errors', 1);
error_reporting(1);
ini_set('error_reporting', E_ALL);
$dbh = new PDO('mysql:host;dbname','', '');
$stmt = $dbh->prepare("SELECT * FROM chapters");
$stmt->execute();
$stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
<body>
<form action="fileUpload.php" method="post" enctype="multipart/form-data">
<p> Select image to upload:</p>
<input name = "file" type="file" id="fileToUpload"><br><br>
<input type="submit" value = "Upload Image">
</form>
</body>
</head>
</html>
有人可以帮忙吗?谢谢..
编辑:
<!DOCTYPE html>
<html>
<head>
<body>
<?php
<select>
foreach ($stmt as $row)
{
<option value=$row['chapterName']>$row['chapterName']</option>
}
</select>
?>
答案 0 :(得分:1)
首先,head
加价无效,body
之后您无法关闭<!DOCTYPE html>
<html>
<head>
<title>This is a title </title>
META TAGs
</head>
<body
BODY CONTENT
</body>
</html>
标记
这就是html标记的样子:
<?php
ini_set('display_errors', 1);
error_reporting(1);
ini_set('error_reporting', E_ALL);
$dbh = new PDO('mysql:host=host;dbname=db', 'airman', 'pass');
?>
<!DOCTYPE html>
<html>
<head> </head>
<body>
<form action="fileUpload.php" method="post" enctype="multipart/form-data">
<p> Select image to upload:</p>
<input name = "file" type="file" id="fileToUpload"><br><br>
<input type="submit" value = "Upload Image">
<select name="chapters">
<?php
$stmt = $dbh->query("SELECT * FROM chapters")->fetchAll(FETCH_ASSOC);
foreach($stmt as $row):?>
<option value="<?php echo $row['chapterid'];?>"><?php echo $row['chapterName'];?></option>
<?php
endforeach;?>
</select>
</form>
</body>
</html>
现在这就是你的代码的样子:
foreach ($stmt as $row)
{
<option value=$row['chapterName']>$row['chapterName']</option>
}
你在这里得到500错误的原因:
echo
它因为你混合php和html错误。它要么使用<?php
ini_set('display_errors', 1);
error_reporting(1);
ini_set('error_reporting', E_ALL);
$dbh = new PDO('mysql:host=host;dbname=db', 'airman', 'pass');
?>
<!DOCTYPE html>
<html>
<head> </head>
<body>
<form action="fileUpload.php" method="post" enctype="multipart/form-data">
<p> Select image to upload:</p>
<input name = "file" type="file" id="fileToUpload"><br><br>
<input type="submit" value = "Upload Image">
<select name="chapters">
<?php
$stmt = $dbh->query("SELECT * FROM chapters");
while($row = $stmt->fetchall(FETCH_ASSOC)):?>
<option value="<?php echo $row['chapterid'];?>"><?php echo $row['chapterName'];?></option>
<?php
endwhile;?>
</select>
</form>
</body>
</html>
或:
<?php
ini_set('display_errors', 1);
error_reporting(1);
ini_set('error_reporting', E_ALL);
$dbh = new PDO('mysql:host=host;dbname=db', 'airman', 'pass');
?>
<!DOCTYPE html>
<html>
<head> </head>
<body>
<form action="fileUpload.php" method="post" enctype="multipart/form-data">
<p> Select image to upload:</p>
<input name = "file" type="file" id="fileToUpload"><br><br>
<input type="submit" value = "Upload Image">
<select name="chapters">
<?php
$stmt = $dbh->prepare("SELECT * FROM chapters");
$stmt->execute();
$results = $stmt->fetchall(PDO::FETCH_ASSOC);
if(count($results > 0)){
foreach($results as $row):?>
<option value="<?php echo $row['chapterid'];?>"><?php echo $row['chapterName'];?></option>
<?php
endforeach;
}else{?>
<option value="0">No data found</option>
<?php}?>
</select>
</form>
</body>
</html>
或强>
{{1}}