好吧,我已经四处走动,但是我很难过。我是PDO的初学者,但是我已经狂热地研究了几天而没有运气。
我尝试的是一个过滤器(在这种情况下是扬声器名称)。在我的SELECT语句中,没有WHERE过滤器,一切都显示正常;所有的音频文件+来自测试数据库的信息就像我想要的那样出现。如果我从数据库中插入带有实际发言人姓名的WHERE语句,那将按预期工作,即仅显示该发言者的音频。但是我从选定的表单数据中动态地使WHERE语句动态化是不成功的。
正如你所看到的,代码有点混乱,因为我已经尝试了很多不同的方式,包括(如你所见)声明中的发布值,以及尝试使用绑定值(例如:speaker_name)或更一般的变量。我此时没有生成错误,但我也没有显示过滤结果。基本上看起来剧本根本没有看到表格结果,但我无法弄清楚原因。
<?php
include './config2.php';
$speaker = isset($_POST['speaker_name']) ? $_POST['speaker_name'] : false;
try {
$dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username,
$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$speaker = ( ! empty($_POST['speaker_name']) ? $_POST['speaker_name'] : '');
if ( isset($_POST['speaker_name'])) {
$speaker = $_POST['speaker_name'];
}
$results = $dbh->prepare("SELECT * FROM sermons WHERE speaker_name = '".$_post['speaker_name']."' ");
$results->bindValue(':speaker', $_POST['speaker_name'], PDO::PARAM_STR);
$results->execute();
for($i=0; $row = $results->fetch(); $i++){
?>
<div class="sermon-box">
<h3 class="sermon-title"><?php echo $row['title']; ?></h3>
<p class="date-detail"><?php echo date("F j, Y", strtotime($row['date_preached'])); ?></p>
<p class="text-detail"><?php $str = $row['text_book']; $str = substr($str, 3); echo $str; ?> <?php echo $row['text_reference']; ?></p>
<p class="speaker">Preacher: <?php echo $row['speaker_name']; ?></p>
<audio controls>
<source src="/files/<?php echo $row['file_name']; ?>" type="audio/mpeg">
</audio>
<p class="download"><a href="/files/download.php?file=<?php echo $row['file_name']; ?>">▼ Download MP3 <span>(<?php echo human_filesize($row['file_size']); ?> MB)</span></a></p>
<?php
}
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$dbh = null;
?>
</div><!-- end sermon-box -->
这是形式本身:
<?php
include './config.php';
?>
<form action="sermon-search-action4.php" method="post">
<?php
$smt = $dbh->prepare('SELECT DISTINCT speaker_name FROM sermons ORDER BY SUBSTRING_INDEX(speaker_name, " ", -1)');
$smt->execute();
$data = $smt->fetchAll();
?>
<label for="speaker">Speaker</label><select name="speaker" id="speaker" autocomplete="off">
<?php foreach ($data as $row): ?>
<option name="<?php echo $row["speaker_name"]?>" value="<?php echo $row["speaker_name"]?>"><?php echo $row["speaker_name"]?></option>
<?php endforeach ?>
</select>
<input type="submit" value="Go!">
</form>
答案 0 :(得分:0)
<?php
include './config2.php';
$speaker = isset($_POST['speaker_name']) ? $_POST['speaker_name'] : false;
try {
$dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username,
$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$speaker = ( ! empty($_POST['speaker_name']) ? $_POST['speaker_name'] : '');
if ( isset($_POST['speaker_name'])) {
$speaker = $_POST['speaker_name'];
}
$results = $dbh->prepare("SELECT * FROM sermons WHERE speaker_name = '".$_post['speaker_name']."' ");
$results->bindValue(':speaker', $_POST['speaker_name'], PDO::PARAM_STR);
$results->execute();
for($i=0; $row = $results->fetch(); $i++){
?>
<div class="sermon-box">
<h3 class="sermon-title"><?php echo $row[$i]['title']; ?></h3>
<p class="date-detail"><?php echo date("F j, Y", strtotime($row[$i]['date_preached'])); ?></p>
<p class="text-detail"><?php $str = $row[$i]['text_book']; $str = substr($str, 3); echo $str; ?> <?php echo $row[$i]['text_reference']; ?></p>
<p class="speaker">Preacher: <?php echo $row[$i]['speaker_name']; ?></p>
<audio controls>
<source src="/files/<?php echo $row[$i]['file_name']; ?>" type="audio/mpeg">
</audio>
<p class="download"><a href="/files/download.php?file=<?php echo $row[$i]['file_name']; ?>">▼ Download MP3 <span>(<?php echo human_filesize($row[$i]['file_size']); ?> MB)</span></a></p>
<?php
}
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$dbh = null;
?>
</div><!-- end sermon-box -->
您应该使用$ data [&#39; speaker_name&#39;]
而不是使用$ row [&#39; speaker_name&#39;] <?php
include './config.php';
?>
<form action="sermon-search-action4.php" method="post">
<?php
$smt = $dbh->prepare('SELECT DISTINCT speaker_name FROM sermons ORDER BY SUBSTRING_INDEX(speaker_name, " ", -1)');
$smt->execute();
$data = $smt->fetchAll();
?>
<label for="speaker">Speaker</label><select name="speaker" id="speaker" autocomplete="off">
<?php foreach ($data as $row): ?>
<option name="<?php echo $data["speaker_name"]?>" value="<?php echo $data["speaker_name"]?>"><?php echo $data["speaker_name"]?></option>
<?php endforeach ?>
</select>
<input type="submit" value="Go!">
</form>
答案 1 :(得分:0)
我已经完成了。
扬声器Display.php的:
<?php
include './config2.php';
$speaker_name = isset($_POST['speaker_name']) ? $_POST['speaker_name'] : false;
try {
$dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username,
$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$speaker_name = ( ! empty($_POST['speaker_name']) ? $_POST['speaker_name'] : '');
if ( isset($_POST['speaker_name'])) {
$speaker = $_POST['speaker_name'];
}
$results = $dbh->prepare("SELECT * FROM sermons WHERE speaker_name = ?");
$results->execute(array($speaker_name));
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Sermon Search</title>
<meta name="description" content="">
<link href="/screen.css" rel="stylesheet" media="screen">
<!--[if lt IE 9]>
<script src="/dist/html5shiv.js"></script>
<![endif]-->
<!--[if lte IE 9]>
<link href="/_ie9.css" rel="stylesheet" type="text/css" media="screen" />
<![endif]-->
<meta name=viewport content="width=device-width, initial-scale=1">
<link rel="icon" href="/favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
</head>
<body class="churchuse">
<?php for($i=0; $row = $results->fetch(); $i++){
$found_rows = true;
?>
<div class="sermon-box">
<h3 class="sermon-title"><?php echo $row['title']; ?></h3>
<p class="date-detail"><?php echo date("F j, Y", strtotime($row['date_preached'])); ?></p>
<p class="text-detail"><?php $str = $row['text_book']; $str = substr($str, 3); echo $str; ?> <?php echo $row['text_reference']; ?></p>
<p class="content-detail">Keywords: <?php echo $row['keywords']; ?></p>
<p class="speaker">Preacher: <?php echo $row['speaker_name']; ?></p>
<audio controls>
<source src="/files/<?php echo $row['file_name']; ?>" type="audio/mpeg">
</audio>
<p class="download"><a href="/files/download.php?file=<?php echo $row['file_name']; ?>">▼ Download MP3 <span>(<?php echo human_filesize($row['file_size']); ?> MB)</span></a></p>
</div><!-- end sermon-box -->
<?php
}
if (!isset($found_rows)) echo "Sorry, no sermons found for this book.";
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$dbh = null;
?>
</body>
搜索框:
<div class="searchbox">
<?php
include './config.php';
?>
<form action="speaker-display.php" method="post">
<?php
$smt = $dbh->prepare('SELECT DISTINCT speaker_name FROM sermons ORDER BY SUBSTRING_INDEX(speaker_name, " ", -1)');
$smt->execute();
$data = $smt->fetchAll(PDO::FETCH_ASSOC);
?>
<label for="text_book">Search by Speaker</label><select name="speaker_name" id="speaker_name" autocomplete="off">
<?php foreach ($data as $row): ?>
<option name="<?php echo $row['speaker_name']?>" value="<?php echo $row['speaker_name']?>"><?php echo $row['speaker_name']?></option>
<?php endforeach; ?>
</select>
<input type="submit" value="Go!">
</form>
</div><!-- end search box -->