php - Only updating half the data to MySQL table -
the code below updating data in mysql table. written pulling data 1 query have tried adapt pull data 2 queries improve ordering. of records update when submit button clicked , i'm not sure how fix it.
the original code was:
if(isset($_post['submit'])){ $password = $_post['password']; $total = $_post['total']; $park_id = $_post['park_id']; if($password=="****"){ for($i =1; $i<=$total; $i++){ $ride_id = $_post['ride_id'.$i]; $name = $_post['ride_name'.$i]; $type = $_post['type'.$i]; $topride = $_post['topride'.$i]; $info = $_post['info'.$i]; $speed = $_post['speed'.$i]; $height = $_post['height'.$i]; $length = $_post['length'.$i]; $inversions = $_post['inversions'.$i]; $query = "update tpf_rides set name='$name',type='$type'"; if($topride!=""){$query .= ",top_ride=$topride";} if($info!=""){$query .= ",info='$info'";} if($height!=""){$query .= ",height=$height";} if($length!=""){$query .= ",length=$length";} if($speed!=""){$query .= ",speed=$speed";} if($inversions!=""){$query .= ",inversions=$inversions";} $query .= " ride_id=".$ride_id." , park_id=".$park_id; mysql_query($query); } header('location:index.php?msg=successfully updated.'); }else{ echo "enter correct password."; } } if(isset($_get['id'])){ $id = $_get['id']; $sql = "select name tpf_parks park_id=".$id; $result = mysql_fetch_array(mysql_query($sql)); echo '<h2>'.$result['name'].'</h2>'; $qry = "select * tpf_rides park_id=".$id; $res = mysql_query($qry); $no = mysql_num_rows($res); $x = 0; if($no>0){ ?> <form action="" method="post"> <input type="hidden" value="<?=$no?>" name="total"> <input type="hidden" value="<?=$id?>" name="park_id"> <table> <?php while($row = mysql_fetch_array($res)){ $x++; echo '<input type="hidden" value="'.$row['ride_id'].'" name="ride_id'.$x.'">'; echo '<tr><td>name : </td><td><input type="text" name="ride_name'.$x.'" value="'.$row['name'].'"></td></tr>'; echo '<tr><td>type : </td><td><input type="text" name="type'.$x.'" value="'.$row['type'].'"></td></tr>'; echo '<tr><td>top ride : </td><td><input type="text" name="topride'.$x.'" value="'.$row['top_ride'].'"></td></tr>'; echo '<tr><td>info : </td><td><input type="text" name="info'.$x.'" value="'.$row['info'].'"></td></tr>'; if($row['type']!="roller coaster"){ echo '<tr><td>speed : </td><td><input type="text" name="speed'.$x.'" value="'.$row['speed'].'"></td></tr>'; echo '<tr><td>height : </td><td><input type="text" name="height'.$x.'" value="'.$row['height'].'"></td></tr>'; echo '<tr><td>length : </td><td><input type="text" name="length'.$x.'" value="'.$row['length'].'"></td></tr>'; echo '<tr><td>inversions : </td><td><input type="text" name="inversions'.$x.'" value="'.$row['inversions'].'"></td></tr>'; } echo '<tr><td colspan="2"><hr></td></tr>'; } ?> <tr><td>password :</td><td><input type="password" value="" name="password" id="password"></td></tr> <tr><td></td><td><input onclick="return check()" type="submit" value="save" name="submit"></td></tr> </table> </form> <?php }else{ echo "no rides in park."; } }else{ if(isset($_get['msg'])){echo $_get['msg'].'<br>';} $qry = "select * tpf_parks order name"; $res = mysql_query($qry); ?> select park : <select name="park" onchange="getdata(this.options[this.selectedindex].value)"> <option value="">select park</option> <?php while($row = mysql_fetch_array($res)) { ?> <option value="<?=$row['park_id']?>"><?=$row['name']?></option> <? } ?> </select> <?php } ?>
and new code altered queries here:
if(isset($_post['submit'])){ $password = $_post['password']; $total = $_post['total']; $park_id = $_post['park_id']; if($password=="*****"){ for($i =1; $i<=$total; $i++){ $ride_id = $_post['ride_id'.$i]; $name = $_post['ride_name'.$i]; $type = $_post['type'.$i]; $topride = $_post['topride'.$i]; $info = $_post['info'.$i]; $speed = $_post['speed'.$i]; $height = $_post['height'.$i]; $length = $_post['length'.$i]; $inversions = $_post['inversions'.$i]; $query = "update tpf_rides set name='$name',type='$type'"; if($topride!=""){$query .= ",top_ride=$topride";} $query .= ",info='$info'"; if($height!=""){$query .= ",height=$height";} if($length!=""){$query .= ",length=$length";} if($speed!=""){$query .= ",speed=$speed";} if($inversions!=""){$query .= ",inversions=$inversions";} $query .= " ride_id=".$ride_id." , park_id=".$park_id; mysql_query($query); } header('location:index.php?msg=successfully updated.'); }else{ echo "enter correct password."; } } if(isset($_get['id'])){ $id = $_get['id']; $sql = "select name tpf_parks park_id=".$id; $result = mysql_fetch_array(mysql_query($sql)); echo '<h2>'.$result['name'].'</h2>'; $qry = "select * tpf_rides park_id = $id , type '%roller coaster%' order name asc"; $res = mysql_query($qry); $qry2 = "select * tpf_rides park_id = $id , type not '%roller coaster%' order name asc"; $res2 = mysql_query($qry2); $qry3 = "select * tpf_rides park_id = $id"; $res3 = mysql_query($qry2); $no = mysql_num_rows($res3); $x = 0; $xx = 0; if($no>0){ ?> <form action="" method="post"> <input type="hidden" value="<?=$no?>" name="total"> <input type="hidden" value="<?=$id?>" name="park_id"> <table> <?php while($row = mysql_fetch_array($res)){ $x++; echo '<input type="hidden" value="'.$row['ride_id'].'" name="ride_id'.$x.'">'; echo '<tr><td>name : </td><td><input type="text" name="ride_name'.$x.'" value="'.$row['name'].'"></td></tr>'; echo '<tr><td>type : </td><td><input type="text" name="type'.$x.'" value="'.$row['type'].'"></td></tr>'; echo '<tr><td>top ride : </td><td><input type="text" name="topride'.$x.'" value="'.$row['top_ride'].'"></td></tr>'; echo '<tr><td>info : </td><td><input type="text" name="info'.$x.'" value="'.$row['info'].'"></td></tr>'; echo '<tr><td>speed : </td><td><input type="text" name="speed'.$x.'" value="'.$row['speed'].'"></td></tr>'; echo '<tr><td>height : </td><td><input type="text" name="height'.$x.'" value="'.$row['height'].'"></td></tr>'; echo '<tr><td>length : </td><td><input type="text" name="length'.$x.'" value="'.$row['length'].'"></td></tr>'; echo '<tr><td>inversions : </td><td><input type="text" name="inversions'.$x.'" value="'.$row['inversions'].'"></td></tr>'; echo '<tr><td colspan="2"><hr></td></tr>'; } while($row2 = mysql_fetch_array($res2)){ $xx++; echo '<input type="hidden" value="'.$row2['ride_id'].'" name="ride_id'.$xx.'">'; echo '<tr><td>name : </td><td><input type="text" name="ride_name'.$xx.'" value="'.$row2['name'].'"></td></tr>'; echo '<tr><td>type : </td><td><input type="text" name="type'.$xx.'" value="'.$row2['type'].'"></td></tr>'; echo '<tr><td>top ride : </td><td><input type="text" name="topride'.$xx.'" value="'.$row2['top_ride'].'"></td></tr>'; echo '<tr><td>info : </td><td><input type="text" name="info'.$xx.'" value="'.$row2['info'].'"></td></tr>'; echo '<tr><td colspan="2"><hr></td></tr>'; } ?> <tr><td>password :</td><td><input type="password" value="" name="password" id="password"></td></tr> <tr><td></td><td><input onclick="return check()" type="submit" value="save" name="submit"></td></tr> </table> </form> <?php }else{ echo "no rides in park."; } }else{ if(isset($_get['msg'])){echo $_get['msg'].'<br>';} $qry = "select * tpf_parks order name"; $res = mysql_query($qry); ?> select park : <select name="park" onchange="getdata(this.options[this.selectedindex].value)"> <option value="">select park</option> <?php while($row = mysql_fetch_array($res)) { ?> <option value="<?=$row['park_id']?>"><?=$row['name']?></option> <? } ?> </select> <?php } ?>
after testing not getting amended, data both , not queries being skipped perhaps record count problem? ideas have done wrong?
are sure column type has value 'roller coaster' or not?
sorry post answer have no rights no comment.
Comments
Post a Comment