尝试更新while循环生成的结果

时间:2013-03-22 03:12:14

标签: php

我遇到的主要问题基本上就是:

我有一个while循环,可以从查询中生成结果。根据已生成的结果,我希望能够更新原始查询所来自的表。

查询产生预期结果,但单击REMOVE按钮时表未更新。我也试图找到一个解决方案,以便在UPDATE查询执行后更新结果...

<?php
    $sql = "SELECT * FROM vehicles WHERE sold='n' ORDER BY year DESC";

    $query = mysql_query($sql);


    while ($row = mysql_fetch_array($query)) { 

    echo 
    "       
        <tr>
            <td style='border-bottom-style:dotted;padding-top:10px;padding-bottom:10px;font-size:.9em'>",$row['year'],"</td>
            <td style='border-bottom-style:dotted;padding-top:10px;padding-bottom:10px;font-size:.9em'>",$row['make'],"</td>
            <td style='border-bottom-style:dotted;padding-top:10px;padding-bottom:10px;font-size:.9em'>",$row['model'],"</td>
            <td style='border-bottom-style:dotted;padding-top:10px;padding-bottom:10px;font-size:.9em'><input type='submit' name='remove' value='REMOVE' style='background-color:#C33;color:white;padding:10px;border-radius:5px;width:70px'/></td>
        </tr>";

    if(isset($_POST['remove'])){
        $removeSql = "UPDATE `table`.`vehicles` SET `display`='0' WHERE `vin`='{$row['vin']}'";
        mysql_query($removeSql) or die('check that code dummy');
    }

    }
    mysql_close($connection);
?>

1 个答案:

答案 0 :(得分:0)

这是一个提交按钮,没有表单标签就行不通。你不能这样做。

您可以在单独的页面上撰写remove code并将submit button转换为normal button并在vin id click上传递button和使用call的页面ajax

或者,如果您不知道ajax并希望在该页面上执行此操作,请按以下方式执行:

<?php
$sql = "SELECT * FROM vehicles WHERE sold='n' ORDER BY year DESC";

$query = mysql_query($sql);


while ($row = mysql_fetch_array($query)) { 

echo 
"       
    <tr>
        <td style='border-bottom-style:dotted;padding-top:10px;padding-bottom:10px;font-size:.9em'>",$row['year'],"</td>
        <td style='border-bottom-style:dotted;padding-top:10px;padding-bottom:10px;font-size:.9em'>",$row['make'],"</td>
        <td style='border-bottom-style:dotted;padding-top:10px;padding-bottom:10px;font-size:.9em'>",$row['model'],"</td>
        <td style='border-bottom-style:dotted;padding-top:10px;padding-bottom:10px;font-size:.9em'>
        <form action="" method="POST">
            <input type="hidden" name="vin_id" value="<?php echo $row['vin']; ?>">
            <input type='submit' name='remove' value='REMOVE' style='background-color:#C33;color:white;padding:10px;border-radius:5px;width:70px'/>
        </form></td>
    </tr>";

}

if(isset($_POST['remove'])){
    $removeSql = "UPDATE `table`.`vehicles` SET `display`='0' WHERE `vin`='".$_POST['vin_id']."'";
    mysql_query($removeSql) or die('check that code dummy');
}

mysql_close($connection);
?>