I'm trying to make an insert into the database using a function but I'm not able to enter its values;
HTML:
<?php if (isset($_POST['submitTexto'])) {
inserir('empresa', $_POST);
} ?>
where submitTexto
is a submit 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());
}
}
Outcome:
Error! Array ([0] = > 00000 [1] = > [2] = >)