所以我有一个下拉列表,应该更改表单下方部分显示的信息。 switch语句检查选择了哪个选项,然后分配正确的查询以运行变量$ query。我不确定为什么查询没有运行,因为我在cPanel的文件管理器上使用代码编辑器。我在我的网站的另一个页面上有一个类似的PHP代码,完美的工作,所以我更困惑。
<section class="content">
<h2 class="heading">
Fetal Development Navigation
</h2>
<div id="navigationButtons">
<form action="" style="display:inline;" method="GET">
<select name="week" style="width:50%; display:inline; height:5%;">
<option title='Select A Week' value='select'>Please select a week! </option>
<option title='Week 1' value='one'>Week 1</option>
<option title='Week 2' value='two'>Week 2</option>
<option title='Week 3' value='three'>Week 3</option>
<option title='Week 4' value='four'>Week 4</option>
<option title='Week 5' value='five'>Week 5</option>
<option title='Week 6' value='six'>Week 6</option>
<option title='Week 7' value='seven'>Week 7</option>
<option title='Week 8' value='eight'>Week 8</option>
<option title='Week 9' value='nine'>Week 9</option>
<option title='Week 10' value='ten'>Week 10</option>
<option title='Week 11' value='eleven'>Week 11</option>
<option title='Week 12' value='twelve'>Week 12</option>
<option title='Week 13' value='thirteen'>Week 13</option>
<option title='Week 14' value='fourteen'>Week 14</option>
<option title='Week 15' value='fifteen'>Week 15</option>
<option title='Week 16' value='sixteen'>Week 16</option>
<option title='Week 17' value='seventeen'>Week 17</option>
<option title='Week 18' value='eightteen'>Week 18</option>
<option title='Week 19' value='nineteen'>Week 19</option>
<option title='Week 20' value='twenty'>Week 20</option>
<option title='Week 21' value='twenty-one'>Week 21</option>
<option title='Week 22' value='twenty-two'>Week 22</option>
<option title='Week 23' value='twenty-three'>Week 23</option>
<option title='Week 24' value='twenty-four'>Week 24</option>
<option title='Week 25' value='twenty-five'>Week 25</option>
<option title='Week 26' value='twenty-six'>Week 26</option>
<option title='Week 27' value='twenty-seven'>Week 27</option>
<option title='Week 28' value='twenty-eight'>Week 28</option>
<option title='Week 29' value='twenty-nine'>Week 29</option>
<option title='Week 30' value='thirty'>Week 30</option>
<option title='Week 31' value='thirty-one'>Week 31</option>
<option title='Week 32' value='thirty-two'>Week 32</option>
<option title='Week 33' value='thirty-three'>Week 33</option>
<option title='Week 34' value='thirty-four'>Week 34</option>
<option title='Week 35' value='thirty-five'>Week 35</option>
<option title='Week 36' value='thirty-six'>Week 36</option>
<option title='Week 37' value='thirty-seven'>Week 37</option>
<option title='Week 38' value='thirty-eight'>Week 38</option>
<option title='Week 39' value='thirty-nine'>Week 39</option>
<option title='Week 40' value='forty'>Week 40</option>
</select>
<input type="submit" value="Go" style="width:25%; display:inline; height:5%;"/>
</form>
</div>
</section>
<section id="week" class="content">
<?php
$i = $_GET['week'];
switch ($i) {
case "one":
$query = "SELECT * FROM fetaldev WHERE week = 1;";
$week = "Week One";
break;
case "two":
$query = "SELECT * FROM fetaldev WHERE week = 2;";
$week = "Week Two";
break;
case "three":
$query = "SELECT * FROM fetaldev WHERE week = 3;";
$week = "Week Three";
break;
case "four":
$query = "SELECT * FROM fetaldev WHERE week = 4;";
$week = "Week Four";
break;
case "five":
$query = "SELECT * FROM fetaldev WHERE week = 5;";
$week = "Week Five";
break;
}
/*if($i == 'one') {
$query = "SELECT * FROM fetaldev WHERE week = 1;";
$week = "Week One";
echo $query . $week;
}*/
$run = mysqli_query($dbconn, $query);
while ($row = mysqli_fetch_array($run)){
$id = $row['id'];
$tag = $row['tag'];
$description = $row['description'];
}
//query isn't running
?>
<?php if(isset($week)){?>
<h2 class="heading"><?php echo $week; ?></h2>
<h4>Size Comparisons</h4>
<?php } else { ?>
<h3>Please select a week from the dropdown list!</h3>
<p>Each week will list out the size comparisons and notable milestones in the development of your baby!</p>
<?php } ?>
<p>
<?php if($tag == 'size'){
echo $description . ', ';
}?>
</p>
<?php if(isset($week)){?>
<h4>Approximate Weight and Length</h4>
<?php } ?>
<p><?php if($tag=='weight' || $tag =='length'){echo $description;}?></p>
</section>
如果您需要的代码中还有其他内容,请告诉我们!提前谢谢!
答案 0 :(得分:1)
尝试以下代码,我能够完全摆脱开关,使用循环最小化代码以防止如此多的数据重复,并使代码不能在PHP和HTML之间切换以获得更多可读性。我还将您转换为准备语句以避免SQL注入。
您的问题是您在数据库循环后设置变量然后显示信息,您必须在循环期间显示信息以获取数据库中每行的信息。
<section class="content">
<h2 class="heading">
Fetal Development Navigation
</h2>
<div id="navigationButtons">
<form action="" style="display:inline;" method="GET">
<select name="week" style="width:50%; display:inline; height:5%;">
<option title='Select A Week' value='select'>Please select a week! </option>
<?php
for ($i = 1; $i <= 40; $i++) {
echo "<option title='Week {$i}' value='{$i}'>Week {$i}</option>";
}
?>
</select>
<input type="submit" value="Go" style="width:25%; display:inline; height:5%;"/>
</form>
</div>
</section>
<?
if(!empty($_GET['week'])) { ?>
<section id="week" class="content">
<?php
$week = $_GET['week'];
if(!empty($week)){
echo "<h2 class='heading'>{$week}</h2>";
echo '<h4>Size Comparisons</h4>';
} else {
echo '<h3>Please select a week from the dropdown list!</h3>';
echo '<p>Each week will list out the size comparisons and notable milestones in the development of your baby!</p>';
}
$stmt = $db_conn->prepare("SELECT id, tag, description FROM fetaldev WHERE week=? ORDER BY tag ASC");
$stmt->bind_param('s', $week);
$stmt->execute();
$stmt->bind_result($id, $tag, $description);
if(!empty($week)) {
echo '<h4>Approximate Weight and Length</h4>';
}
while ($stmt->fetch()) {
echo "<p>".(($tag == "size") ? $description.', ' : $tag)."</p>";
if($tag == "weight") {
echo "Weight: " . $description;
}
if($tag == "length") {
echo "Length: " . $description;
}
}
?>
</section>
<?php
}
如果有任何问题,请告诉我,我会更新答案以反映他们的解决方案。
答案 1 :(得分:0)
你的逻辑有些分歧。如果从不运行查询,则不应使用仅在查询后定义的变量。然后,除非$_GET['week']
设置了值,否则不应运行查询。
我已使用以下
修改了您的代码for
循环创建选项,因为它们每次都相同(只是递增) - 您可以用它缩短几行。value="1"
代替value="one"
作为选项。这允许您在查询中直接使用该值 ,这样您就不需要切换将其转换回来。 isset($_GET['week'])
。while ($stmt->fetch())
)中,以便您可以获取所有记录,而不仅仅是最后一个记录。$_GET['week']
。 htmlspecialchars($_GET['week'])
- 这至少可以防止无效的HTML(错误),但如果恶意HTML / JavaScript进入您的数据库,它将阻止XSS攻击。 error_reporting(E_ALL); ini_set('display_errors', 1);
将允许您查找任何PHP错误,使用$stmt->error
和/或$dbconn->error
将为您提供任何MySQL错误。
注意:您永远不应该向实时环境显示错误 - 只应记录它们!在开发过程中,向浏览器显示错误很好。
实施这些更改后,您的代码将如下所示。
<?php
// Enabling error-reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<section class="content">
<h2 class="heading">
Fetal Development Navigation
</h2>
<div id="navigationButtons">
<form style="display:inline;" method="GET">
<select name="week" style="width:50%; display:inline; height:5%;">
<option title='Select A Week' value='select'>Please select a week! </option>
<?php
for ($i = 1; $i <= 40; $i++) {
echo '<option title=Week '.$i.'" value="'.$i.'">Week '.$i.'</option>';
}
?>
</select>
<input type="submit" value="Go" style="width:25%; display:inline; height:5%;"/>
</form>
</div>
</section>
<section id="week" class="content">
<?php
if (isset($_GET['week'])) {
?>
<h2 class="heading"><?= htmlspecialchars($_GET['week']); ?></h2>
<?php
if ($stmt = $dbconn->prepare("SELECT id, tag, description FROM fetaldev WHERE week = ?")) {
$stmt->bind_param("s", $_GET['week']);
if (!$stmt->execute()) // If the query failed to execute
die("Error executing query: ".$stmt->error);
$stmt->bind_result($id, $tag, $description);
while ($stmt->fetch()) {
if ($tag == 'size') {
?><h4>Size Comparisons</h4>
<p><?= htmlspecialchars($description); ?>, </p>
<?php
}
if ($tag == 'weight' || $tag == 'length') {
?><h4>Approximate Weight and Length</h4>
<p><?= htmlspecialchars($description); ?></p>
<?php
}
}
$stmt->close();
} else {
die("Error preparing query: ".$dbconn->error);
}
} else { ?>
<h3>Please select a week from the dropdown list!</h3>
<p>Each week will list out the size comparisons and notable milestones in the development of your baby!</p>
<?php
} ?>
</section>
mysqli_stmt::bind_param()
和 - PHP manual on mysqli_stmt::prepare()
即使这可能无法解决所有问题,也应该首先找出问题所在,因为我们现在已经对您的代码实施了一些错误检查。