PDO UPDATE array using php mysql -



PDO UPDATE array using php mysql -

hello i'm trying create code update or edit survey answers , comments per reply when execute function submiting form, did not save value database. can prepare it?

i'm new in pdo.

thanks in advance.

database structure

"questions" (idquestion, question) "surveys" (idsurvey, idquestion, answers, comments_per_question, survey_number)

update function

public function modifysurveymulti($answer = array()) { if(!empty($answer)) { foreach($answer $questi => $value ) { $this->mydb->write("update survey set( `idquestion` = '".$questi."', `answers` = '".$value[0]."', `comments_per_answer`= '".$_post["comment"][$questi]."')"); } } }

modify_surveyform.php

<th><?php echo $row["questions"];?></th> <td> <input type = "text" name = "answer[<?php echo $row['idquestion'];?>][]" value = "<?php echo $row["answers"];?>"> </input> </td> <td> <textarea type = "text" name = "comment[<?php echo $row['idquestion'];?>]" cols = "50" rows = "3"/> <?php echo $row["comment"];? </textarea> </td> </tr><?php } ?>

mydbconnect.php

<?php // i'm adding pdo database because yours deprecated class dbconnect { public $con; // create default database element public function __construct($host = '',$db = '',$user = '',$pass = '') { seek { $this->con = new pdo("mysql:host=$host; dbname=$db",$user, $pass, array( pdo::attr_errmode => pdo::errmode_warning ) ); } grab (exception $e) { homecoming 0; } } // simple fetch , homecoming method public function fetch($_sql) { $query = $this->con->prepare($_sql); $query->execute(); if($query->rowcount() > 0) { while($array = $query->fetch(pdo::fetch_assoc)) { $rows[] = $array; } } homecoming (isset($rows) && $rows !== 0 && !empty($rows))? $rows: 0; } // simple write db method public function write($_sql) { $query = $this->con->prepare($_sql); $query->execute(); } }?>

few things need do:

first of ditch code, useless , expose sql injection use pdo straight prepared statement the query need :

update survey set(`answers`= ?,`comments_per_answer`= ?) idquestion = ?

you need adjust class create connection

class dbconnect { public $con; public function __construct($host = '',$db = '',$user = '',$pass = '') { seek { $this->con = new pdo( "mysql:host=$host;dbname=$db", $user,$pass, array(pdo::attr_errmode => pdo::errmode_warning) ); } grab (exception $e) { die($e); } } public function get_connection(){ homecoming $this->con; } }

so can create this:

$db = new dbconnect(/*pass arguments here*/); $this->mydb = $db->get_connection();

modify , utilize in function:

public function modifysurveymulti($answer = array(), $comments) { $sql = 'update survey set(`answers`= ?,`comments_per_answer`= ?) idquestion = ?'; $stmt->prepare($sql); foreach($answer $questi => $value ) { $stmt->execute(array($value, $comments[$questi],$questi)); $count = $stmt->rowcount(); echo $count > 0 ? $questi.' updated' : $questi.' did not update'; } }

call function :

if(isset($_post['answer'], $_post['comments'])){ $answers = $_post['answer']; $comments = $_post['comments']; modifysurveymulti($answers, $comments); }

php mysql arrays pdo survey

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -