0

My main issue that I am running into is basically this:

I have a while loop that generates results from a query. With the results that have been generated, I want the ability to update the table the original query was from.

The query produces the expected results, but the table is not being updated when I click the REMOVE button. I am also trying to find a solution for the results to be updated after the UPDATE query executes...

<?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);
?>
4

1 回答 1

0

那是一个提交按钮,没有表单标签将无法工作。你不能这样做。

您可以将 写remove code在单独的页面上并将其转换submit buttonnormal button并使用.vin idclickbuttoncallajax

或者,如果您不知道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);
?>
于 2013-03-22T03:22:18.863 回答