So I have a dropdown list that should change the information displayed within the section below the form. The switch statement checks which option is selected and then assigns the correct query to run to the variable $query. I’m not sure why the query isn’t running, as I’m using a code editor on cPanel’s file manager. I have a similar php code on another page of my website that works perfectly, so I’m even more puzzled.
<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>
If there is anything more from the code that you need, just let me know! Thanks in advance!
2
Answers
Try the following code, I was able to get rid of the switch completely, minimized your code using loops to prevent so much data repetition, and made your code not switch between PHP and HTML so much for more readability. I also switched you to prepared statements to avoid SQL injection.
Your problem was that you were setting variables and then displaying information after the database loop, you have to display the information DURING the loop to get info for each row in your database.
If there are any issues, please let me know and I will update the answer to reflect a solution to them.
Your logic is somewhat divided. You shouldn’t use variables that are only defined after a query if the query is never run. Then you shouldn’t run the query unless there’s a value set by
$_GET['week']
.I’ve modified your code with the following
for
-loop, as they’re the same each time (just incrementing) – you can shorten a few lines with it.value="1"
instead ofvalue="one"
for your options. This allows you to use that value directly in a query, so that you don’t need a switch to convert it back.isset($_GET['week'])
before running the query, or before using the variables that are being defined in the query.while ($stmt->fetch())
) so that you can get all the records, and not just the last one.$_GET['week']
directly in the query.htmlspecialchars($_GET['week'])
when echoing – this will at the very least prevent invalid HTML (bugs), but it will prevent XSS-attacks if malicious HTML/JavaScript enters your database.error_reporting(E_ALL); ini_set('display_errors', 1);
will allow you to find any PHP errors, and using$stmt->error
and/or$dbconn->error
will give you any MySQL errors.With those changes implemented, your code would look something like this.
References
mysqli_stmt::bind_param()
and – PHP manual onmysqli_stmt::prepare()
Even if this might not fix everything, it should be a start to figuring out what’s wrong, as we’ve now implemented some error-checking to your code.