简体   繁体   English

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

[英]Attempting to update results generated from a while loop

My main issue that I am running into is basically this: 我遇到的主要问题基本上是这样的:

I have a while loop that generates results from a query. 我有一个while循环,可从查询生成结果。 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... 我还试图为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);
?>

That's a submit button, will not work without form tag. 这是一个提交按钮,没有表单标签将无法使用。 You can't do it this way. 你不能这样子。

You can write the remove code on a separate page and convert that submit button to normal button and pass vin id on click of that button and call that page using ajax . 您可以在单独的页面上编写remove code ,并将该submit button转换为normal button并在clickbutton传递vin idclick使用ajax call该页面。

Or if you don't know ajax and want to do it on that page itself then do it this way : 或者,如果您不了解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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM