Query is not running, inserts nothing

Asked

Viewed 75 times

0

What could be wrong? Inserts nothing into the bank.

Form:

<form class="form-horizontal" action="chk-gerente.php?nro_pergunta=<?php  echo $nro_pergunta; ?>" method="GET">
    <fieldset>
        <legend><b>Pergunta <?php echo"$ordem";?> de <?php echo"$totalpergunta";?></b></legend>
        <div class="form-group">
            <center><h3><label><?php echo"$descpergunta";?></label></h3></center>
            <div class="col-lg-10">
                <div class="radio"><label><input type="radio" name="resp" id="optionsRadios1" value="5">OTIMO        </label></div>
                <div class="radio"><label><input type="radio" name="resp" id="optionsRadios2" value="3">REGULAR      </label></div>
                <div class="radio"><label><input type="radio" name="resp" id="optionsRadios3" value="1">RUIM         </label></div>
                <div class="radio"><label><input type="radio" name="resp" id="optionsRadios4" value="0">NAO APLICADO </label></div>
            </div>
        </div>
        <?php
        $timestamp            = @mktime(date("H")-4, date("i"),date("s"),date("m"),date("d"),date("Y"),0);
        $DataCad              = gmdate("Y-m-d", $timestamp); // Gravar $DataCad no BD
        echo"<input type='hidden' name='loja'         id='loja'         value='$loja'>";
        echo"<input type='hidden' name='id_usuario'   id='id_usuario'   value='$id_usuario'>";
        echo"<input type='hidden' name='questionario' id='questionario' value='$questionario'>";
        echo"<input type='hidden' name='area'         id='area'         value='$area'>";
        echo"<input type='hidden' name='nro_pergunta' id='nro_pergunta' value='$nro_pergunta'>";     
        echo"<input type='hidden' name='DataCad'      id='DataCad'      value='$DataCad'>";
        echo"<input type='hidden' name='pergunta'     id='pergunta'     value='$idpergunta'>";
        ?>
        <div class="form-group">
            <div class="col-lg-10 col-lg-offset-2">
                <button id='button' type="submit" class="btn btn-primary">Responder</button>
            </div>
        </div>
    </fieldset>
</form>

The INSERT is at the top of the page:

require("conexao.php");

if(isset($_GET['submit'])){
    $loja         = $_GET ["loja"];
    $id_ususario  = $_GET ["id_usuario"]; 
    $questionario = $_GET ["questionario"];
    $area         = $_GET ["area"];
    $idpergunta   = $_GET ["pergunta"];   
    $resp         = $_GET ["resp"]; 
    $datacad      = $_GET ["DataCad"]; 

    $sql_insert = mysql_query("INSERT INTO respostas
        (resp_id,resp_loja,resp_usuario,resp_questionario,resp_area,resp_subgrupo,resp_pergunta_id,resp_resposta,resp_data,resp_status)
        VALUES
        ('','$loja','$id_usuario','$questionario','$area','1','$idpergunta','$resp','$DataCad','1')") 
        or die (mysql_error());
    @mysql_query($sql_insert,$conexao);
} else {
} 

I tried to pass as well and it was not...

include("conexao.php");
if(isset($_GET['submit'])){
    $loja         = $_GET ["loja"];
    $id_ususario  = $_GET ["id_usuario"]; 
    $questionario = $_GET ["questionario"];
    $area         = $_GET ["area"];
    $idpergunta   = $_GET ["pergunta"];   
    $resp         = $_GET ["resp"]; 
    $datacad      = $_GET ["DataCad"]; 

    $sql=" INSERT INTO respostas
        (resp_id,resp_loja,resp_usuario,resp_questionario,resp_area,resp_subgrupo,resp_pergunta_id,resp_resposta,resp_data,resp_status)
        VALUES
        ('','$loja','$id_usuario','$questionario','$area','1','$idpergunta','$resp','$DataCad','1'";
} else {
} 
  • Have you tried turning the Insert straight to the bank to see if it’s working?

  • It is worth remembering that the functions mysql_* is already deprecated in PHP 5.5 (and removed from 7.0)... migrate to MySQLi or PDO_MySQL

  • It went right through the bank.

  • Remove all the@ from the code to see the error message.

  • Another thing: this @mysql_query($sql_insert,$conexao); would only be correct if $sql_insert query, but it is the result of a query...

  • @Berriel ,look there please what I added ...

  • Do the following mysql_query($sql) or die(mysql_error());

  • the way you did has no action, do what @rray said

  • Would that be it? ,because the screen returned empty without error ... $sql = "INSERT INTO respostas &#xA; (resp_id,resp_loja,resp_usuario,resp_questionario,resp_area,resp_subgrupo,resp_pergunta_id,resp_resposta,resp_data,resp_status)&#xA; VALUES &#xA; ('','$loja','$id_usuario','$questionario','$area','1','$idpergunta','$resp','$DataCad','1')" mysql_query($sql) or die(mysql_error());

  • This, some error appears?

  • No, the screen is blank even if I put the var_dump($sql); it shows nothing

  • puts this at the top of the file, ini_set('display_errors',true); error_reporting(E_ALL);

  • Opa,started to improve,I did a test here and if I change: if(isset($_GET['Submit']))' para if(getenv("REQUEST_METHOD") == "GET"){ ele da o Insert,more I noticed if updating the page inserts,and the first one inserts empty,to thinking that something wrong in GET,some hint?

  • some error appears?

  • not in this one,

  • is that just entering the page already inserts

  • @Otacio see my answer to know why you are entering just by entering the page, and inserting blank

Show 12 more comments

2 answers

1

Solved:

 include("conexao.php");
if(isset($_GET['submit'])){
    $loja         = $_GET ["loja"];
    $id_usuario  = $_GET ["id_usuario"]; 
    $questionario = $_GET ["questionario"];
    $area         = $_GET ["area"];
    $idpergunta   = $_GET ["pergunta"];   
    $resp         = $_GET ["resp"]; 
    $datacad      = $_GET ["data_cad"]; 

  $sql_insert = mysql_query("INSERT INTO respostas 
  (resp_id,resp_loja,resp_usuario,resp_questionario,resp_area,resp_subgrupo,resp_pergunta_id,resp_resposta,resp_data,resp_status)
  VALUES 
  ('','$loja','$id_usuario','$questionario','$area','1','$idpergunta','$resp','$datacad','1')") 
   or die (mysql_error());
   @mysql_query($sql_insert,$conexao); } 

It worked after making the following changes indicated by our colleague @Berriel in his reply.

0


You can’t change to

if (getenv("REQUEST_METHOD") == "GET") { ... }

Because the normal request is also a GET. In this case, it will enter only from entering the page, but as the form has not yet been sent, the data will be blank.


Try this:

include("conexao.php");
if(isset($_GET['submit'])){
    $loja         = $_GET ["loja"];
    $id_ususario  = $_GET ["id_usuario"]; 
    $questionario = $_GET ["questionario"];
    $area         = $_GET ["area"];
    $idpergunta   = $_GET ["pergunta"];   
    $resp         = $_GET ["resp"]; 
    $datacad      = $_GET ["DataCad"]; 

    // essa linha só define a query
    $sql = "INSERT INTO respostas
        (resp_id,resp_loja,resp_usuario,resp_questionario,resp_area,resp_subgrupo,resp_pergunta_id,resp_resposta,resp_data,resp_status)
        VALUES
        ('','$loja','$id_usuario','$questionario','$area','1','$idpergunta','$resp','$datacad','1')";

    // essa linha que executa
    mysql_query($sql, $conexao) or die(mysql_error());
}

But for this to work, you must change your button to:

<button id='button' type="submit" name="submit" class="btn btn-primary">Responder</button>

That way, it will only insert when you press the button, because the name="submit" will do the GET["submit"] exist and enter the if.

  • He passes everything right in the query_string, but still not inserting anything.

  • Hi Berriel, it worked out,plus the key point was the button,you indicated,Thank you,follow result in reply.

  • @Otacio if my reply was helpful, please mark as reply or give a vote, thank you

  • Okay, thank you again.

Browser other questions tagged

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