Do not save data with Mysql

Asked

Viewed 99 times

-2

Good people, I have the following complication, each patient has a so-called "PII" and this same "PII" has several data. The problem is when filling the PII by clicking write it simply wipes me all data instead of storing in the database.

The initial snippet of php connecting to Mysql:

    <?php
session_start();
error_reporting(0);
include('includes/config.php');
if(strlen($_SESSION['alogin'])==0)
    {   
header('location:index.php');
}
else{
if(isset($_POST['add']))
{
$avinicial=$_POST['AvInicial'];
$meta=$_POST['Meta'];
$avintercalar=$_POST['AvIntercalar'];
$avfinal=$_POST['AvFinal'];
$datainicio=$_POST['DataInicio'];
$datafim=$_POST['DataFim'];
$resultadouni=$_POST['ResultadoUni'];
$avalexp=$_POST['AvalExpec'];
$concretizaobj=$_POST['ConcretizaObj'];
$objdefinidos=$_POST['ObjDefinidos'];
$objatingidos=$_POST['ObjAtingidos'];
$totalalcancados=$_POST['TotalAlcancados'];
$sql = "INSERT INTO tblobjetivos(AvInicial, Meta, AvIntercalar, AvFinal, DataInicio, DataFim, ResultadoUni, AvalExpec, ConcretizaObj, ObjDefinidos, ObjAtingidos, TotalAlcancados) VALUES(:avinicial, :meta, :avintercalar, :avfinal, :datainicio, :datafim, :resultadouni, :avalexp, :concretizaobj, :objdefinidos, :objatingidos, :totalalcancados)";
$query = $dbh->prepare($sql);
$query->bindParam(':avinicial', $avinicial, PDO::PARAM_STR);
$query->bindParam(':meta', $meta, PDO::PARAM_STR);
$query->bindParam(':avintercalar', $avintercalar, PDO::PARAM_STR);
$query->bindParam(':avfinal', $avfinal, PDO::PARAM_STR);
$query->bindParam(':datainicio', $datainicio, PDO::PARAM_STR);
$query->bindParam(':datafim', $datafim, PDO::PARAM_STR);
$query->bindParam(':resultadouni', $resultadouni, PDO::PARAM_STR);
$query->bindParam(':avalexp', $avalexp, PDO::PARAM_STR);
$query->bindParam(':concretizaobj', $concretizaobj, PDO::PARAM_STR);
$query->bindParam(':objdefinidos', $objdefinidos, PDO::PARAM_STR);
$query->bindParam(':objatingidos', $objatingidos, PDO::PARAM_STR);
$query->bindParam(':totalalcancados', $totalalcancados, PDO::PARAM_STR);
$lastInsertId = $dbh->lastInsertId();
$query->execute();

    if ($lastInsertId) {
        $msg = "PII Adicionado com Sucesso";
    } else {
        $error = "Confirme se preencheu tudo corretamente!";
    }
}?>

Tabela que deveria armazenar os valores do PII Tabela dos doentes

  • Need to be more clear on the question. Clear how? Clear where or what exactly?

  • When I click for the form data to be stored in the database, everything I filled in the form disappears and in the database no value appears.

  • @sam the problem is that each patient can have up to 12 goals, ie are 12 times each table objective understand? how can I do this store right in the database? I should create some more table?

  • @Bacco if you had already said it in another question why do you have to say it again? Just once my dear. But thank you for the time of your life you lost trying to "help" me! Greetings!

1 answer

-1

Not a solution but a hint: in the connection file (probably /include/config.php), set the PDO to trigger exceptions. These exceptions will include important information, such as why the data is not being persisted in the database. For example, if in the database a column is NOT NULL and you are trying to add a null value, it will trigger an exception with the message.

http://php.net/manual/en/pdo.error-handling.php

<?php
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

?>

I hope I’ve helped.

  • 2

    The -1 is not mine, and I don’t know exactly why you voted negative, but I find it interesting in these cases perhaps to leave a comment instead of reply. I understand that since it has code, it would be a little more complicated, but I’m just commenting because I see that you are looking to answer several questions, and your account is new, maybe it’s nice to pay attention to these details that can cause a certain rejection in the answers. As you already have enough points, you can attend the network chat to discuss these points with the staff.

  • 2

    One thing that can help, is to ask the questioner for more details, to help surround the problem (for example, suggest that he post the upload form code, and/or the last lines of the PHP error log for example), which avoids a series of answers that in the end may not even have connection with the real problem of the author.

  • Thanks for the tip.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.