php - Update a value dynamically with a $variable inside a while loop -


i think got pretty question. produced loop 2 input radio button inside of each loop. when press on 1 of these radio buttons, function changeit changes value inside database. stuck : have id column each rows , want find id , put changeit function, statement (at end, must put dynamic id). loop shows each rows inside of separated divs. put variable $id in while loop, thinking about, maybe, put function changeit inside function admin()? 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 * 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' `form`.`id` = '$id'");         $st->execute();     } 

it's easy send get/post data in associative array format. change input names 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> , submit button <input type="submit"... outside loop if like.

with form inside loop $_post this:

array('123' => 'accepter') 

with form outside loop $_post this:

array('123' => 'accepter', '124' => 'refuser', '125' => 'refuser', ...) 

so need change update code:

$conf_arr = $_post['confirmation']; foreach($conf_arr $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 need escape sql values prevent sql injection


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -