Reputation: 115
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();
}
Upvotes: 0
Views: 1158
Reputation: 16923
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
Upvotes: 1