Answer the question
In order to leave comments, you need to log in
PDO code to embed?
Just learning php
I found a source with this code to insert into a table: I
need your help to understand this code
function pdoSet($allowed, &$values, $source = array()) {
$set = '';
$values = array();
if (!$source) $source = &$_POST;
foreach ($allowed as $field) {
if (isset($source[$field])) {
$set.="`".str_replace("`","``",$field)."`". "=:$field, ";
$values[$field] = $source[$field];
}
}
return substr($set, 0, -2);
}
$allowed = array("name","surname","email"); // allowed fields
$sql = "INSERT INTO users SET ".pdoSet($allowed,$values);
$stm = $dbh->prepare($sql);
$stm->execute($values);
Answer the question
In order to leave comments, you need to log in
Let's start with the fact that this is an automated embed code.
And first you need to learn how to insert manually.
Everything is done manually
$sql = "INSERT INTO users (name, surname, sex) VALUES (?,?,?)";
$stmt= $pdo->prepare($sql);
$stmt->execute([$name, $surname, $sex]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question