2
I’m trying to make an entry in the database using a function but I’m not able to enter the respective values;
HTML:
<?php if (isset($_POST['submitTexto'])) {
inserir('empresa', $_POST);
} ?>
where submitTexto
is a send button.
Function:
<?php
function conectar() {
try {
$pdo = new PDO("mysql:host=localhost;dbname=fap", "root", "");
}
catch(PDOException $e) {
echo $e->getMessage();
}
return $pdo;
}
conectar();
function inserir($tabela, $dados) {
$con = conectar();
foreach ($dados as $dado => $valores) {
$campo = array();
array_push($campo, $dado);
$valor = array();
array_push($valor, $valores);
}
$campo = implode(',', $campo);
$valor = implode(',', $valor);
$inserir = $con->prepare("INSERT INTO $tabela($campo) VALUES($valor)");
$inserir->execute();
if ($inserir->execute()){
echo "Inserido com sucesso!";
}
else {
echo "Erro!";
print_r($con->errorInfo());
}
}
Upshot:
Error! Array ( [0] => 00000 [1] => [2] => )
Paste this into your function, so we can test if the posts are coming. foreach($_POST as $key=>$val){ $dados[$key] = $val; } echo "<pre>"; print_r($data); echo "<pre>"; die();
– Sr. André Baill
Print sql, in this Insert only has number? as it is needs to escape the strings with simple quotes. Call
$inserir->execute()
once.– rray