dpd7195 2014-03-24 20:07
浏览 67
已采纳

在while循环中使用$变量动态更新值

I think I got a pretty good question. I produced a loop with 2 input radio button inside of each loop. When you press on one of these radio buttons, my function ChangeIt changes a value inside my database. This is where I am stuck : I have an ID column for each rows and I want to find that ID and put it into my ChangeIt function, into my statement (at the very end, I must put a dynamic ID). My Loop shows each rows inside of separated divs. I put a variable $id in my while loop, so I was thinking about, maybe, put my function ChangeIt inside my function Admin()? is this possible? THX!

Any ideas?

PHP on index.php

if(isset($_POST['confirmation'])){
$conf = $_POST['confirmation'];
    if($conf == 'Accepter'){
        $object = new User;
        $object->ChangeIt($conf,$id);
    } else if($conf == 'Refuser'){
        $object = new User;
        $object->ChangeIt($conf,$id);
    }   
}

PHP

public function Admin(){
        $st = $this->db->prepare("SELECT * FROM form");
        $st->execute();
        while($r = $st->fetch()){
            $id = $r['ID'];
            echo '<hr>';
            echo 'Username : ' .$r['username'].' <br>';
            echo 'Adresse : ' .$r['Adresse'].' <br>';
            echo 'Met : ' .$r['Met'].' <br>';
            echo 'Age : ' .$r['Age'].' <br>';
            echo 'Statut : ' .$r['Statut'].' <br>';
            echo '<form action="compte.php" method="post">';
            echo '<input type="radio" name="confirmation" value="Accepter"> Accepter';
            echo '<input type="radio" name="confirmation" value="Refuser"> Refuser<br>';
            echo '<input type="submit" name="submit" value="Confirmer">';
            echo '</form>';
            echo '<hr>';
        }
    }
public function ChangeIt($ans,$id){
        $st = $this->db->prepare("UPDATE `test`.`form` SET `Statut` = '$ans' WHERE `form`.`ID` = '$id'");
        $st->execute();
    }
  • 写回答

1条回答 默认 最新

  • doujie9252 2014-03-24 20:12
    关注

    It's very easy to send GET/POST data in associative array format. Change your input names to confirmation[id goes here...]:

    echo '<input type="radio" name="confirmation['.$id.']" value="Accepter"> Accepter';
    echo '<input type="radio" name="confirmation['.$id.']" value="Refuser"> Refuser<br>';
    

    you can put form tags <form>...</form> and submit button <input type="submit"... outside your loop if you like.

    with form inside loop your $_POST will look like this:

    Array('123' => 'Accepter')
    

    with form outside loop your $_POST will look like this:

    Array('123' => 'Accepter', '124' => 'Refuser', '125' => 'Refuser', ...)
    

    so you need to change your update code:

    $conf_arr = $_POST['confirmation'];
    foreach($conf_arr as $id => $conf) {
        if($conf == 'Accepter'){
            $object = new User;
            $object->ChangeIt($conf,$id);
        } else if($conf == 'Refuser'){
            $object = new User;
            $object->ChangeIt($conf,$id);
        }   
    }
    

    Please mind you need to escape SQL values to prevent SQL Injection

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题