1
hello
I created the code below and when I run it inserts a record in the database, but it does not insert the informed data in the form. I wonder if someone could help me?
on the screen displays the message: Invalid query: You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near ') VALUES )' at line 1
class Banco {
//parametros de configuração de banco
protected static $servidor = "localhost";
protected static $usuario = "root";
protected static $senha = "root";
protected static $esquema = "mapa";
//conexao com banco
public function conectar() {
$db = mysqli_connect(self:: $servidor, self:: $usuario, self:: $senha, self::$esquema);
if (!$db) {
die('Não foi possivel conectar: ' . mysqli_error());
}
return $db;
}// fim conectar
//desconexão com banco
public function desconectar($db) {
mysqli_close($db);
}//fim desconectar
public function salvar() {
$campos = get_object_vars($this);
$sqlI = "INSERT INTO " . get_class($this) . " (";
$sqlF = ") VALUES (";
foreach ($campos as $key => $value) {
$sqlI.= $key . ",";
if (!is_object($campos[$key])) {
$sqlF.= "'" . $value . "',";
} else {
$sqlF.= "'" . $campos[$key]->getId() . "',";
}
}//fim foreach
$sql = substr($sqlI, 0, strlen($sqlI) - 1) . substr($sqlF, 0, strlen($sqlF) - 1) . ")";
$db = $this->conectar();
$res = mysqli_query($db, $sql);
if (!$res) {
die('Query inválida: ' . mysqli_error($db));
}
$id = mysqli_insert_id($db);
$this->desconectar($db);
return $id;
}//fim salvar
thank you in advance
Hello, a tip, before recording in the database check what will be saved in the database, print (echo) the variable $sql - copy and paste into Mysql and try to run it. Possible errors whole field containing apostrophe('), missing apostrophe(') in text content, number of characters larger than the field in the Support database,
– Wilson Rosa Gomes
Thanks for the golden tip!!! helped a lot!!!!
– jvmoura