查询未运行,switch 语句(PHP/SQL)
Posted
技术标签:
【中文标题】查询未运行,switch 语句(PHP/SQL)【英文标题】:Query not running, switch statement(PHP/SQL) 【发布时间】:2017-07-24 16:41:13 【问题描述】:所以我有一个下拉列表,可以更改表单下方部分中显示的信息。 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>
如果您需要代码中的更多内容,请告诉我!提前致谢!
【问题讨论】:
有一些未知数 - 你有一个有效的连接吗?查询后,mysqli_error($dbconn)
有没有回滚?是否有任何 PHP 错误(启用错误报告并检查您的日志)。您还应该在运行查询之前检查表单是否已提交。
如果你去 www.mommy-info.com/pregnancy/fetal-development,你可以看到发生了什么。表单正在提交,因为正在执行 switch 语句(例如,选择了第三周,然后 $week 变量显示为第三周)。从 mysqli_error($dbconn) 打印没有错误。并且此代码编辑器上没有错误报告或日志。谢谢!
您的开关是否必须使用字符串大小写?这有点不相关,但是使用 PHP,如果您不必按照目前的方式使用它,您可以将大量代码变成一个非常短的集合 - 这将更容易排除故障、阅读和进一步开发跨度>
我会写一个简单的例子来说明我的意思
快速建议 - 将 value='one'
更改为 value='1'
,这将删除开关,因为您可以直接使用该值。
【参考方案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
如果有任何问题,请告诉我,我会更新答案以反映解决方案。
【讨论】:
它只显示“请选择一周”部分。 在代码中它甚至不会阻止该元素的显示,您是否添加了其他任何内容? 我刚刚复制并粘贴了你所拥有的。 我明白我做了什么。将if(isset($week))
更改为 if(isset($_GET['week'])
- 实际上 - 我更新了我的代码
这是因为变量不存在了。尝试新代码【参考方案2】:
您的逻辑有些分歧。如果查询从未运行,则不应使用仅在查询之后定义的变量。那么你不应该运行查询,除非$_GET['week']
设置了一个值。
我已使用以下代码修改了您的代码
-
使用
for
-loop 创建您的选项,因为它们每次都是相同的(只是递增) - 您可以使用它缩短几行。
使用value="1"
代替value="one"
作为您的选项。这允许您在查询中直接使用该值,这样您就不需要开关来将其转换回来。
在运行查询之前或在使用查询中定义的变量之前检查 isset($_GET['week'])
。
将查询的输出放入循环 (while ($stmt->fetch())
) 中,这样您就可以获得所有记录,而不仅仅是最后一条。
安全性改进:使用有界参数的预准备语句,以防止 SQL 注入。这是因为我们现在直接在查询中使用$_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>
参考文献
when to use htmlspecialchars() function? How can I prevent SQL injection in PHP? How to get useful error messages in PHP? PHP manual onmysqli_stmt::bind_param()
和 - PHP manual on mysqli_stmt::prepare()
即使这可能无法解决所有问题,也应该是找出问题所在的开始,因为我们现在已经对您的代码实施了一些错误检查。
【讨论】:
以上是关于查询未运行,switch 语句(PHP/SQL)的主要内容,如果未能解决你的问题,请参考以下文章
如何在 switch 语句中使用 laravel 模型运行多个 Where 子句