i have a piece of code i wrote in mysqli to insert multiple rows of data inside the database.the code actually is working.i'm tring to write the same code in PDO but i'm returned an error
<pre lang="PHP"> Catchable fatal error: Object of class PDOStatement could not be converted to string in C:\wamp64\www\test\Nouveau dossier\quiz\index.php on line 148
i do not really understand where is the problem.Please for help.
this is the piece of code in mysqli
if (isset($_POST['register']) && !empty($_POST['answer1']) && !empty($_POST['answer2'])
&& !empty($_POST['answer3']) && !empty($_POST['answer4'])) {
$answer1 = $_POST['answer1'];
$answer2 = $_POST['answer2'];
$answer3 = $_POST['answer3'];
$answer4 = $_POST['answer4'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$con = new mysqli($servername, $username, $password, $dbname);
if($con->connect_error){
die("Connection to db failed:" .$con->connect_error);
}
$answers_array = array(
array('AnswerText' => $answer1, 'AnswerStatus' => 0, 'QuestionId' => 1),
array('AnswerText' => $answer2, 'AnswerStatus' => 0, 'QuestionId' => 1),
array('AnswerText' => $answer3, 'AnswerStatus' => 1, 'QuestionId' => 1),
array('AnswerText' => $answer4, 'AnswerStatus' => 0, 'QuestionId' => 1) );
$sql = "INSERT INTO answers (AnswerText, AnswerStatus, QuestionId) VALUES ";
$total = count($answers_array);
$iterator = new ArrayIterator($answers_array);
while($iterator->valid()){
$currentItem = $iterator->current();
$sql .="('".$currentItem['AnswerText']."','".$currentItem['AnswerStatus']."','".$currentItem['QuestionId']."')";
$sql .= $iterator->key()? ',' : ';';
}
if ($con->multi_query($sql) === TRUE) {
echo "New records created successfully";
} else
{
echo "Error: " . $sql . "<br>" . $con->error;
}
$con->close();
}else
echo "Please, fill the form fields !";
thanks in advance
What I have tried:
this is the same code in PDO
if (isset($_POST['register']) && !empty($_POST['answer1']) && !empty($_POST['answer2'])
&& !empty($_POST['answer3']) && !empty($_POST['answer4'])) {
$answer1 = $_POST['answer1'];
$answer2 = $_POST['answer2'];
$answer3 = $_POST['answer3'];
$answer4 = $_POST['answer4'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$con = new PDO('mysql:$servername;$dbname', $username, $password);
$answers_array = array(
array('AnswerText' => $answer1, 'AnswerStatus' => 0, 'QuestionId' => 1),
array('AnswerText' => $answer2, 'AnswerStatus' => 0, 'QuestionId' => 1),
array('AnswerText' => $answer3, 'AnswerStatus' => 1, 'QuestionId' => 1),
array('AnswerText' => $answer4, 'AnswerStatus' => 0, 'QuestionId' => 1)
);
$sql=$con->prepare("INSERT INTO answers (AnswerText, AnswerStatus, QuestionId) VALUES ");
$total = count($answers_array);
$iterator = new ArrayIterator($answers_array);
while($iterator->valid()){
$currentItem = $iterator->current();
$sql .="('".$currentItem['AnswerText']."','".$currentItem['AnswerStatus']."','".$currentItem['QuestionId']."')";
$iterator->next();
$sql .= $iterator->key()? ',' : ';';
};
if ($sql === TRUE) {
echo "New records created successfully";
} else
{
echo "Error: " . $sql . "<br>" . $con->error;
}
$con->close();
}else
echo "Please, fill the form fields !";
below is the form from which i get my values
<form action="index.php" method="POST">
<legend>This form helps type in answers proposition to question</legend>
<br>
<label for="Answer1">Answer 1 :</label>
<br>
<textarea name="answer1" id="answer1" cols="90" rows="5">Bonjour le monde</textarea>
<br>
<label for="Answer2">Answer 2 :</label>
<br>
<textarea name="answer2" id="answer2" cols="90" rows="5">Hello world</textarea>
<br>
<label for="Answer3">Answer 3 :</label>
<br>
<textarea name="answer3" id="answer3" cols="90" rows="5">Mè tsa téh</textarea>
<br>
<label for="Answer4">Answer 4:</label>
<br>
<textarea name="answer4" id="answer4" cols="90" rows="5">Guten tag</textarea>
<br>
<input type="submit" name="register" value="Save">
</form>
</body>