1
Hello, I’m wanting to do a function to insert the data in the table, is not returning me anything, look at the code:
function insertInto ($tabela, $colunas=array(), $valores=array()) {
foreach ( $valores as $valor ){
$valor = implode(',', $valores);
return $valor;
}
foreach ($colunas as $coluna){
$coluna = implode(',', $colunas);
return $coluna;
}
$sql = "INSERT INTO $tabela ($coluna) VALUES ('$valor')";
$handle = mysql_query($sql, $conexaobd) or die(mysql_error());
if(!$this->query($sql, $coluna)->error()){
return true;
}
return false;
}
this is the code of the data to insert in the table;
if ($_POST['status_atual'] != $_POST['status'])
{
$tabela = 'editais_status_historico';
$colunas = 'id_edital';
$colunas .= 'login_usuario';
$colunas .= 'data';
$colunas .= 'estado_anterior';
$colunas .= 'estado_novo';
$valores = $_POST['status'];
$valores .= $_POST['status_atual'];
$valores .= $_POST['idedital'];
$valores .= $_SESSION['user'];
$valores .= date("Y-m-d") . ' ' . date("H:i:s");
$status = $_POST['status'];
$status_atual = $_POST['status_atual'];
$id_edital = '0';
$id_usuario = $_SESSION['user'];
$data = date("Y-m-d") . ' ';
$data .= date("H:i:s");
insertInto($tabela, $colunas, $valores);
}
First the
implode
expects an Array as a second parameter, you even gave a default value of type array, but is concatenating everything as string ($colunas .=
). Second of all, you’re wearing areturn
right after each foreach. What causes the function to return and not be run anymore. So your code basically runs the first foreach and goes back to who called the function. Try to enable error viewing in your PHP configuration which should also help you identify all errors.– Pedro Henrique
Complementing @Pedrohenrique’s comment, it seems you don’t even need these
foreach
s, only theimplode
.– Oeslei