只将数据的一半更新到MySQL表

时间:2013-08-07 20:48:09

标签: php mysql

以下代码用于更新MySQL表中的数据。它是通过从一个查询中提取所有数据来编写的,但我尝试调整它以从两个查询中提取数据以改进排序。现在只有部分记录在点击提交按钮时更新,我不确定如何修复它。

原始代码是:

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 .= " where ride_id=".$ride_id." and 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 from tpf_parks where park_id=".$id;
        $result = mysql_fetch_array(mysql_query($sql));
        echo '<h2>'.$result['name'].'</h2>';
        $qry = "select * from tpf_rides where 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 this park.";
            }
    }else{
        if(isset($_GET['msg'])){echo $_GET['msg'].'<br>';}
        $qry = "select * from  tpf_parks order by 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 } ?>

和我改变查询的新代码在这里:

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 .= " where ride_id=".$ride_id." and 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 from tpf_parks where park_id=".$id;
        $result = mysql_fetch_array(mysql_query($sql));
        echo '<h2>'.$result['name'].'</h2>';
        $qry = "SELECT * FROM  tpf_rides
WHERE park_id = $id AND type LIKE '%Roller Coaster%' ORDER BY name ASC";
        $res = mysql_query($qry);


        $qry2 = "SELECT * FROM  tpf_rides
WHERE park_id = $id AND type NOT LIKE '%Roller Coaster%' ORDER BY name ASC";
        $res2 = mysql_query($qry2);

        $qry3 = "SELECT * FROM  tpf_rides WHERE 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 this park.";
            }
    }else{
        if(isset($_GET['msg'])){echo $_GET['msg'].'<br>';}
        $qry = "select * from  tpf_parks order by 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 } ?>

在测试未修改的内容之后,是否跳过LIKE和NOT LIKE查询的数据,这可能是记录计数问题? 我做错了什么想法?

1 个答案:

答案 0 :(得分:0)

您确定您的列类型是否具有“过山车”的价值?

很抱歉发帖作为答案,因为我没有权利没有评论。