Advertisement
ribamarfs

Untitled

Jul 20th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. private function updateSet(){
  2. $set='';
  3.  
  4. for($x=0;$x < $this->numFields();$x++){
  5. $field = $this->fieldName($x);
  6.  
  7. $value = isset($_POST[$field]) ? $_POST[$field] : '';
  8. // A linha abaixo gerará a linha: $nome = 'Nome do cliente';
  9. //$$field = $_POST[$field];
  10. $$field = $value;
  11.  
  12. // Este if gerará a variável $set contendo "$nome = :$nome, $email = :$email, ...";
  13. if($x < $this->numFields()-1){
  14. $set .= "$field = :$field,";
  15. }else{
  16. $set .= "$field = :$field";
  17. }
  18. }
  19.  
  20. return $set;
  21. }
  22.  
  23. public function update($id){
  24.  
  25. if(isset($_POST['send'])){
  26.  
  27. $sql = "UPDATE {$this->table} SET {$this->updateSet()} WHERE id = :id";
  28.  
  29. $sth = $this->pdo->prepare($sql);
  30.  
  31. for($x=0;$x < $this->numFields()-1;$x++){
  32. // Se inserir um
  33. // print $field.'-'; ele me mostra os 6 campos, então por que invalid parameter number?
  34. $field = $this->fieldName($x);
  35. $sth->bindParam(":$field", $_POST["$field"], PDO::PARAM_INT);
  36. }
  37.  
  38. if($sth->execute()){
  39. print "<script>location='index.php?table=$this->table';</script>";
  40. }else{
  41. print "Error on update register!<br><br>";
  42. }
  43. }
  44. }
  45.  
  46. Ao tentar editar um registro recebo o erro:
  47.  
  48. Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number in /backup/www/auto-crud2/pt_BR/classes/crud.php on line 246
  49.  
  50. Linha 246 é esta:
  51. if($sth->execute()){
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement