0
I have this code, but for some reason you’re not saving the information in the database. Can someone explain to me what’s wrong with the code ?
<?php
require_once('functions.php');
add();
?>
<h2>Novo Cliente</h2>
<form action="add.php" method="post">
<hr />
<div class="row">
<div class="form-group col-md-7">
<label for="name">Nome</label>
<input type="text" class="form-control" id="name" name="customer['name']">
</div>
</div>
<div class="row">
<div class="form-group col-md-3">
<label for="cpf">CPF</label>
<input type="text" class="form-control" id="cpf" name="customer['cpf']">
</div>
</div>
</form>
<!--/////////////////////////////////////////////////////////////////////////////////////////////////////////
Este código esta em outro arquivo-->
<?php
function add() {
if (!empty($_POST['customer'])) {
$customer = $_POST['customer'];
save('pessoa', $customer);
header('location: index.php');
}
}
?>
<!--/////////////////////////////////////////////////////////////////////////////////////////////////////////
Este código esta em outro arquivo-->
<?php
function save($table = null, $data = null) {
$database = open_database();
$columns = null;
$values = null;
foreach ($data as $key => $value) {
$columns .= trim($key, "'") . ",";
$values .= "'$value',";
}
$columns = rtrim($columns, ',');
$values = rtrim($values, ',');
$sql = "INSERT INTO " . $table . "($columns)" . " VALUES " . "($values);";
try {
$database->query($sql);
$_SESSION['message'] = 'Registro cadastrado com sucesso.';
$_SESSION['type'] = 'success';
} catch (Exception $e) {
$_SESSION['message'] = 'Nao foi possivel realizar a operacao.';
$_SESSION['type'] = 'danger';
}
close_database($database);
}
?>
http://php.net/manual/en/mysqli.error.php
– MagicHat
You have some error message so we can help you better?
– Fabricio
The answers to this question may help you. https://answall.com/questions/192239/dados-nao-salvos-no-banco-de-dados-em-php?rq=1
– Fabricio
No error message appears, just runs everything, but does not insert in the seat. I’ve already put var_dump() in the variables and they’re taking the correct values from html. I’m using mysqli_
– w.rock
Have you checked if the connection class is working?
– Fabricio
Already tried placing the Location header after insertion into the BD?
– Sam
The connection class is working correctly. I just put the Location header after insertion, but it still didn’t work.
– w.rock
This question is too broad. You need to eliminate as many variables as possible, the way you are, you just copied the code and expect people to solve your problem. Check if the database connection works; if the function
save()
is being executed; if the functionadd
is called, since she is responsible for calling the functionsave
... Try to isolate yourself the problem so that it is easier to help you. A sign that the question needs to be rewritten is the amount of comments questioning whether you have ever tried X or Y.– Filipe Moraes
Takes the value of
$sql
, runs right on the seat and sees if it works.– Francisco
It’s something else then. I tested your code exactly as it is in the question and is inserting it normally in my bank.
– Sam
Comments on the header redirect Location to stay on the same page and see if it returns any errors. I think the problem may be in this function
open_database()
– Sam
Thanks to everyone who tried to help. The problem was a typo in the name of a column in the databank. I was able to find out after I did what Francis said, Thank you!
– w.rock
Arrange! = ) @w. rock
– Francisco