How to take the value of POST and save to the bank

Asked

Viewed 193 times

0

Good afternoon friends, I’m not able to save the second INSERT in the bank, because when I save, it goes as array, how can I solve this problem?

Image of the View:

inserir a descrição da imagem aqui

Picture of the Bank

inserir a descrição da imagem aqui

Code

$nome_plano = trim($_POST["nome_plano"]);
    $quemcadastrou = $userRow['nome_funcionario'];
    
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $conn->beginTransaction();
      $conn->exec("INSERT INTO plano (nome_plano, quemcadastrou, data_hora_cadastro) VALUES ('$nome_plano', '$quemcadastrou', NOW())");
        $lastid = $conn->lastInsertId();
    
        foreach($_POST['informacao'] as $informacao){    
                $parcelaplano[''] = trim($_POST['parcelaplano']);
                $porcentagem[] = trim($_POST['porcentagem']);
                $conn->exec("INSERT INTO inform_plano (id_plano, parcelaplano, porcentagem) VALUES ('$lastid', '$parcelaplano', '$porcentagem')");
                print_r($informacao);
    }
    // commit the transaction
    $conn->commit();
     echo "<h1>CADASTRADO COM SUCESSO</h1>";
    }
catch(PDOException $e)
    {
    // roll back the transaction if something failed
    $conn->rollback();
    echo "Error: " . $e->getMessage();
    }

$conn = null;

2 answers

2


Apparently the information you need to write is in the variable $informacao.

Try replacing the following lines:

$parcelaplano[''] = trim($_POST['parcelaplano']);
$porcentagem[] = trim($_POST['porcentagem']);

For:

$parcelaplano = trim($informacao['parcelaplano']);
$porcentagem = trim($informacao['porcentagem']);
  • It worked out, thank you buddy...

0

You are setting an array at this point in the code:

 $parcelaplano[''] = trim($_POST['parcelaplano']);
 $porcentagem[] = trim($_POST['porcentagem']);

Try to modify these two lines above, by these below:

 $parcelaplano = trim($_POST['parcelaplano']);
 $porcentagem = trim($_POST['porcentagem']);
  • I put, but no information was entered in the database, until the name of the Array disappeared, it was just the id_plano

  • Do a var_dump to check the value of these variables: $_POST['parcelplano'] and $_POST['percentage'], probably they are empty.

  • Really, they’re coming: null, how do I fix this? And why is the Array information in the View being viewed?

Browser other questions tagged

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