dongwen7423 2014-04-01 13:44
浏览 44

如果在同一个php页面上不同时间提交两个$ _POST值,我怎么能缓存它们?

I have a html form which generates a second html form via $_POST.

The first form submits $_POST['id'], which will then generate another form that will submit $_POST['fields'] which is used to update a mysql db. However, the mysqli query used to update the db relies on both $_POST['id'] and $_POST['fields'] values.

I can't work around how to submit $_POST['fields'] without $_POST['id'] being reset, because $_POST['fields'] is generated precisely by the submission of $_POST['id'].

I tried passing $_POST values to $_SESSION, and while the values are passed successfully, the $_POST['id'] value will always reset to NULL once $_POST['fields'] is submitted. Here's the whole code:

<?php 

include 'head.html';
echo "<body>";
include 'connection.php';   

$query = mysqli_query("SELECT * FROM table") or die(mysqli_error());
$querytwo = mysqli_query("SELECT * FROM table WHERE id='{$_POST[id]}'") or die(mysqli_error());

//generates $_POST['id'] form

echo "<form action=\"admin.php\" method=\"post\"><select name=\"id\">";
    while ($row = mysqli_fetch_array($query)) 
    {
        echo "<option value=\"$row[id]\">$row[firstName] $row[lastName]</option>";
    }
echo "</select><input type=\"submit\"></form><br>";

 //generates $_POST['fields'] form

if (isset($_POST['id']) && !empty($_POST['id'])) 
{   
        $rowtwo = mysqli_fetch_array($querytwo);    
        $exclude = array(2);

        for($column_value = 0; $column_value < 17; $column_value++) 
         {
            if (in_array($column_value, $exclude)) 
            {
                continue;
            }
                $field = mysqli_field_name($querytwo, $column_value);
                echo "<form method=\"POST\">" . "$field" . ": <input type=\"text\" name=\"fields\" value=\"$rowtwo[$column_value]\"><input type=\"submit\"></form>";    
        }
}  

 //updates db with $_POST['id'] and $_POST['fields'] values

if (isset($_POST['field']) && !empty($_POST['field']))
{
    $column_value = range(0, 20);
    $field = mysqli_field_name($querytwo, $column_value);
    mysqli_query("UPDATE table SET '$field'='{$_POST[fields]}' where id='{$_POST[id]}'");

} else 
{
 echo "not updated yet";    
}
?>
</body>
</html>
  • 写回答

4条回答 默认 最新

  • dsflxcfuw27742248 2014-04-01 13:49
    关注

    When you generate the second form, pass $_POST['id'] to it and put it in a hidden input.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么