$_Session variable does not write to database

Asked

Viewed 687 times

0

I’m using mysqli and I can’t save information in the database.
I ran the test with

echo var_dump($_SESSION); 
echo "<br />";
echo $_SESSION['login'];  


The value returned was:
array(2) { ["login"]=> string(6) "Djalma" ["password"]=> string(3) "123" }

    <?php
    session_start();

        require '../php/funcao_mysql.php';

    $cod_os = $_POST['N_OS'];
    $cod_usuario = $_SESSION['login']; 

    $sql = $con->prepare("insert into ControleOS (cod_os, cod_usuario, cod_cliente, NomeCliente, DataOs, Hora, DescProbl, Tp_Instalacao, MuCaboCliente,  
            MuCaboBackBone, MuRJ45, MuEmendaBargoa, MuTomada, MuBenjamimAdaptador, MuEsticadorCaboFE, MuRoldanaS, MuRoldanaD, MuAnel, MuFecho, 
            MuCaixaHermetica, MuBarraAterramento, MuTuboAterramento, MuAntena, MuRadio, TrPing, TrTracert, TrVelocidade, TrCaboCliente, TrPontoRede,
            TrRJ45, TrVerificPlacaRede, TrVerificDHCP, TrConfigDiscador, TrConfigRoteador, TrConfigRadio, TrHabilitacaoServico, TrInformacaoChuvas,
            TrTelefoneSuporte, TrMudancaPortaBackbone, TrMudancaPortaCliente, TrTreinamentoCliente )
                    Values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )");
$sql->bind_param('iiissssssssssssssssssssssssssssssssssssss', $cod_os, $cod_usuario, $cod_cliente, $NomeCliente, $DataOs, $Hora, $DescProbl, //7
                    $Tp_Instalacao, $MuCaboCliente, $MuCaboBackBone, $MuRJ45, $MuEmendaBargoa, $MuTomada, $MuBenjamimAdaptador, $MuEsticadorCaboFE,  //8
                    $MuRoldanaS, $MuRoldanaD, $MuAnel, $MuFecho, $MuCaixaHermetica, $MuBarraAterramento, $MuTuboAterramento, $MuAntena, $MuRadio, $TrPing, //10
                    $TrTracert, $TrVelocidade, $TrCaboCliente, $TrPontoRede, $TrRJ45, $TrVerificPlacaRede, $TrVerificDHCP, $TrConfigDiscador, //8
                    $TrConfigRoteador, $TrConfigRadio, $TrHabilitacaoServico, $TrInformacaoChuvas, $TrTelefoneSuporte, $TrMudancaPortaBackbone, //6
                    $TrMudancaPortaCliente, $TrTreinamentoCliente); //2
$sql->execute();
$sql->close();
?>


I did not put all variables to summarize and pq the purpose of this post and pass $_Session to the Form and save in the bank.

2 answers

3


Updating

I was reviewing a code pad and I remembered a combination of http_build_query and parse_str which may be useful to your case.

$_SESSION['session.A'] = 'Meu valor para A';
$_SESSION['session.B'] = 'Meu valor para B';
$build = http_build_query( $_SESSION );

$build will be a Sting with: Session. A=My+value+for+A&session. B=My+value+for+B

parse_str( $build , $parse );
print_r( $parse );

$parse back to being an array: array( 'session_A' => 'My value for A' , 'session_B' => 'My value for B')

* I didn’t want to update, but it’s still a way. Although I can get the same result, json_encode and serialize are even more recommended.


You can record two ways using json_encode and serialize, see below:

$_SESSION['name'] = 'meu nome';

Using json_encode

// output: {"name":"meu nome"}
$json = json_encode( $_SESSION );

// parâmetro TRUE retorna um array
// output: array( 'name' => 'meu nome' )
json_decode( $json , true );

// parâmetro stdClass retorna um onjeto stdClass
// output: stdClass Object $name -> 'meu nome'
json_decode( $json , false );

Using serialize

// output: a:1:{s:4:"name";s:8:"meu nome";}
$serialize = serialize( $_SESSION );

// output: array( 'name' => 'meu nome' )
unserialize( $serialize );

0

  • 1

    You can show me how you would?

  • 2

    I disagree with "Recommend exclusion". The explanation is short but there is, the answer is not a simple "Check {link}". @Renatotavares, see the guide [Answer] for more details.

  • I agree with you, @brasofilo. Thanks.

Browser other questions tagged

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