Insert into does not return

Asked

Viewed 118 times

1

I have the following table: tabelas relacionadas

Query with Insert

public function CadastrarArquivos($pdo, $arq_nome, $a_areaid, $u_userid){
        $ins = $pdo->prepare("INSERT INTO arq_arquivos(arq_nome, a_area_a_areaid, u_usuarios_u_useid) VALUES(:arq_nome, :a_areaid, :u_userid)"
            );
        $ins->bindParam(":arq_nome",$arq_nome);
        $ins->bindParam(":a_areaid",$a_areaid);
        $ins->bindParam(":u_userid",$u_userid);

    $obj = $ins->execute();

    return ($obj) ? $obj : false;<code>

//Php function to register

function cadastrarArquivo($app){           

$param = array("titulo"=>$app->site_titulo, "pagina" => "formarquivos", "dados" => array( "tituloform" => "Cadastrar novo Arquivo", "action"=>"execCadastrarArquivo", "arq_arqid"=>"", "arq_nome"=>"", "labelbtnsubmit"=>"Cadastrar" ) ); $app->loadView("Admin",$param); } function execCadastrarArquivo($app){ $admin = $app->loadModel("Admin"); $arq_nome = ($_POST["arq_nome"]); $a_areaid = 1; /*marque 1 para selecionar uma area já existente de teste.*/ $u_userid = $_SESSION["u_userid"]; $obj = $admin->CadastrarArquivos($app->conexao, $arq_nome, $a_areaid, $u_userid); if($obj) { $mensagem = "Cadastro efetuado com sucesso!"; } else { $mensagem = "Cadastro falhou!"; } $this->listarareainicial($app,$admin,$mensagem); }<code>

//That way there is no mistake, but do not register in the bank.

html form

<form method="POST" action="index.php?m=admin&c=minhaarea&a=<?=$tpl["dados"]["action"]?>"> <div class="row"> <div class="col-xs-2"> <strong>Nome do arquivo:</strong> </div> <div class="col-xs-10"> <input type="text" name="arq_nome" class="col-xs-12 form-control" value="<?=$tpl["dados"]["arq_nome"]?>" autofocus required /> </div> </div> <div class="row marginTop"> <div class="col-xs-2"> <input type="submit" value="<?=$tpl["dados"]["labelbtnsubmit"]?>" class="btn btn-primary btn-large" /> </div> </div> <input type="hidden" value="<?=$tpl["dados"]["arq_arqid"]?>" name="arq_arqid" /> </form>

  • 1

    Not that it can’t work that way, but it would be nice to simplify the code. If you have function CadastrarArquivos, function cadastrarArquivo and function execCadastrarArquivo, something doesn’t look very good in the way of organizing the code.

  • Then Function Register Files is separated, together only with the query, already the Register File returns to the user a page with the form to type, and to send has a button that calls the Function execCadastrarfile.

1 answer

0

Your problem seems to be in the part $ins->bindParam(":a_areaid",$a_areaid);. Put the parameter PDO::PARAM_INT, after the $a_areaid in that way:

$ins->bindParam(":a_areaid",$a_areaid,PDO::PARAM_INT);

use this parameter when passing an int value to the database.

More information on this link:http://php.net/manual/en/pdostatement.bindparam.php

  • I read the link, I understood, I did the test but still it was not, I put the html form in my question above.

  • I noticed the following, if I go to the bank and do an Insert, works normal, now by php will not, BUT I realized that after I try to register by php and it does not register, I go to the bank and the ID it generates one in sequence, as if php had worked. I don’t understand how it’s possible.

  • The action attribute may be conflicting in double quotes. It is written like this: <form method="POST" action="index.php? m=admin&c=minhaarea&a=<?= $tpl["data"]["action"]? >"> . Remove quotes from data and action, or place single quotes around.

  • I managed to make it work by making the link between arq_arquivos tables with a_area, I will rethink better about the two tables, I will comment if I see any more specific problem so that others can enjoy my topic, grateful to all.

Browser other questions tagged

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