i ran strange problem mysql while() loop breaks early, , not continue loop each student iteration.
my code:
<?php session_start(); $con = mysqli_connect("host","user","pass","db") or die("error connecting database! " . mysqli_error($con)); $parentid = $_session['parentid']; $sql = "select * `studentdata` parent_id='".$parentid."'"; //define sql statement. $result = mysqli_query($con,$sql) or trigger_error('encountered error'); //call sql statement, if errors give de-bugging info. $datarow = mysqli_fetch_array($result); //starting value $total = 0; $studentcount = mysqli_num_rows($result); echo $studentcount." found."; while($row = mysqli_fetch_array($result)) { $currentname = $datarow['firstname']; echo "searching ".$currentname.""; if(isset($_post["$currentname"])){ echo "found ".$currentname." - "; $add = $datarow['price']; echo "adding ".$add." ".$total." - "; $total += $add; echo "new total ".$total."<br/>"; } } echo "final total is: ".$total.""; ?> my database:
+------------------------------------+ |first name| price | orchestra_group | +------------------------------------+ |patrick |270 | main | |erin |530 | main | +----------+-------+-----------------+ the output:
2 found.
searching erin
found erin - adding 530 0 - new total 530
final total is: 530
the problem:
the code should loop on both patrick , erin, stops looping after first loop. while() loop breaks before both students cycled through loop, though both $_post['erin'] , $_post['patrick'] set.
var_dump data:
array(3) { ["erin"]=> string(7) "checked" ["patrick"]=> string(7) "checked" ["choose"]=> string(6) "choose" }
code improvements
to fix error, should :
- remove
$datarow = mysqli_fetch_array($result);on line 10 - replace
$row = mysqli_fetch_array($result)$datarow = mysqli_fetch_array($result)on line 18
to improve readability of code , therewith reduce confusion, should :
- remove unnecessary
.""on line 20 , line 30 - replace
$_post["$currentname"]$_post[$currentname]on line 22
to improve performance of code, should additionally :
- replace double quotes single quotes
full code:
<?php session_start(); $con = mysqli_connect('host','user','pass','db') or die('error connecting database! ' . mysqli_error($con)); $parentid = $_session['parentid']; $sql = 'select * `studentdata` parent_id=\''.$parentid.'\''; //define sql statement. $result = mysqli_query($con,$sql) or trigger_error('encountered error'); //call sql statement, if errors give de-bugging info. //starting value $total = 0; $studentcount = mysqli_num_rows($result); echo $studentcount . ' found.'; while($datarow = mysqli_fetch_array($result)) { $currentname = $datarow['firstname']; echo 'searching ' . $currentname; if(isset($_post[$currentname])){ echo 'found ' . $currentname . ' - '; $add = $datarow['price']; echo 'adding ' . $add.' ' . $total . ' - '; $total += $add; echo 'new total ' . $total . '<br/>'; } } echo 'final total is: '. $total; ?> alternative
as alternative replacing double quotes single quotes, consider putting variables inside strings reduce complexity
full code:
<?php session_start(); $con = mysqli_connect('host','user','pass','db') or die('error connecting database! ' . mysqli_error($con)); $parentid = $_session['parentid']; $sql = "select * `studentdata` parent_id='$parentid'"; //define sql statement. $result = mysqli_query($con,$sql) or trigger_error('encountered error'); //call sql statement, if errors give de-bugging info. //starting value $total = 0; $studentcount = mysqli_num_rows($result); echo "$studentcount found."; while($datarow = mysqli_fetch_array($result)) { $currentname = $datarow['firstname']; echo "searching $currentname"; if(isset($_post[$currentname])){ echo "found $currentname - "; $add = $datarow['price']; echo "adding $add $total - "; $total += $add; echo "new total $total<br/>"; } } echo "final total is: $total"; ?>
Comments
Post a Comment