2
Could someone tell me why my insert2 function is returning null after insertion? she should be returning the string with the success message.
<?php
require_once 'conexao.php';
$con = new Conexao();
function insert($dados, $tabela, $campo_unico) : string{
global $con;
//Verifica se tem algum campo que tenha que ser único no BD
if(is_null($campo_unico)) {
insert2($dados, $tabela);
}else{ //Caso tenha verifica se já existe algum registro
$sql = "SELECT * FROM ".$tabela." WHERE ".$campo_unico."=:".$campo_unico;
$select = $con->conectar()->prepare($sql);
if(is_numeric($dados[$campo_unico])){
$select->bindValue(":".$campo_unico, $dados[$campo_unico], PDO::PARAM_INT);
}else {
$select->bindValue(":".$campo_unico, $dados[$campo_unico], PDO::PARAM_STR);
}
$select->execute();
if( ($select->rowCount()) > 0) {
return "O ".$campo_unico." informado já existe em nossos registros";
}else {
insert2($dados, $tabela);
}
}
}
function insert2($dados, $tabela) : string{
global $con;
$i = 0;
$sql = "INSERT INTO " . $tabela . " (";
foreach ($dados as $key => $valor) {
if ($i == count($dados)-1) {
$sql .= " ".$key." ";
} else {
$sql .= " ".$key.", ";
}
$i++;
}
$sql .= ")";
$sql .= " values (";
$i = 0;
foreach ($dados as $key => $valor) {
if ($i == count($dados)-1) {
$sql .= " :".$key." ";
} else {
$sql .= " :".$key.", ";
}
$i++;
}
$sql .= ")";
$insert = $con->conectar()->prepare($sql);
foreach ($dados as $key => $valor) {
if (is_numeric($valor)) {
$insert->bindValue(":$key", $valor, PDO::PARAM_INT);
} else {
$insert->bindValue(":$key", $valor, PDO::PARAM_STR);
}
}
if ($insert->execute()) {
return "Registro inserido com sucesso";
} else {
return $insert->errorInfo();
}
}
The mistake you’re making is this:
Fatal error: Uncaught Typeerror: Return value of Insert() must be of the type string, None returned in ...:34
Which is precisely why the return of the function is being null Stack trace:
It is easier to debug this code to find the cause of the error (line and message). For CRUD type operation I do not recommend you have
return
with success message or error; you will not know what is what. Return a bool flag or the last ID when it is Insert and in case of error use theexceptions
adequately.– Papa Charlie
I’ve never seen it, wear it
:
at the end of a function, you can indicate me a link where I can have more details of this ?– Leandro Lima
@Leandrolima in php7 you can specify the function return type.
– rray
Papa the error you are having is this: Fatal error: Uncaught Typeerror: Return value of Insert() must be of the type string, None returned in ... which is precisely why it is returning empty. Leandro Lima, these are new features of php 7 that allows you to choose the return type of the function, from a look ai : https://tableless.com.br/10-novidades-do-php-7/
– Leandro Silva Campos
@rray I also did not know, in which case would error if the function played a
exception
?– Papa Charlie
you always call
insert()
? see that in this function has a call frominsert2($dados, $tabela);
and you don’t get the result.– rray
Change
insert2($dados, $tabela)
for:return insert2($dados, $tabela)
– Papa Charlie
@guillermoscimento I think this only applies to php7 (forward) it seems to me important to keep the tag.
– rray