Problem in Insert shows no error

Asked

Viewed 404 times

0

Everyone all right? I’ve searched the entire forum and even other forums and couldn’t find answers on this. If anyone can take a look and point out my mistake I’d appreciate.

Follow the form:

   <form action="add.php" method="post" name="form1">
   div> <table class='table table-hover table-responsive table-bordered' style="background-color:white;">


    <tr>
        <td>Data de Cadastro:</td>
        <td></td>
    </tr>

    <tr>
        <td>Nome Fantasia:</td>
        <td><input type="text" name="nomefantasia"  required autofocus class='form-control'/>
    </tr>

     <tr>
        <td>Razão Social</td>
        <td><input type="text" name="razaosocial"  required class='form-control'/><td>
    </tr>

    <tr>
        <td>CNPJ:</td>
        <td><input type="text" name="cnpj"  required class='form-control'/></td>
    </tr>   

    <tr>
        <td>Inscrição Estadual:</td>
        <td><input type="text" name="inscricaoestadual"  required class='form-control'/></td>
    </tr>           

   <tr>
        <td>Categoria:</td>
        <td><select name="categoria" size="1">
                     <option>Selecione...</option>
                     <?PHP mysql_connect('localhost','root','') or die(mysql_error());

                        mysql_select_db('suinos') or die(mysql_error()); 
                        mysqli_set_charset($mysqli,"utf8");
                        ?>
                     <?php $query = mysql_query("SELECT categoria_id, categoria_titulo FROM comp_categoria");
                     while($categoria = mysql_fetch_array($query)) { ?>
                     <option class='form-control' value="<?php echo $categoria['categoria_id'] ?>"><?php echo $categoria['categoria_titulo'] ?></option>
                     <?php } ?>                      
            </select></td>
    </tr>

    <tr>
        <td>Endereço:</td>
        <td><input type="text" name="endereco"  required class='form-control'/></td>
    </tr>

    <tr>
        <td>Número:</td>
        <td><input type="text" name="numero"  required class='form-control'/></td>
    </tr>

    <tr>
        <td>Bairro:</td>
        <td><input type="text" name="bairro"  required class='form-control'/></td>
    </tr>

    <td>CEP:</td>
        <td><input type="text" name="cep"  required class='form-control'/>
    </td>
    </tr>

    <tr>
        <td>Cidade:</td>
        <td>
            <select name= "cidade"><option>Escolha a cidade</option> 

                     <?php $queryy = mysql_query("SELECT Id, Nome FROM comp_cidade ORDER BY Nome ASC");
                     while($cidade = mysql_fetch_array($queryy)) { ?>
                     <option class='form-control' value="<?php echo $cidade['Id'] ?>"><?php echo $cidade['Nome'] ?></option>
                     <?php } ?>
            </select>
        </td>
    </tr>

    <tr>
        <td> Estado:</td>
        <td><select name="estado" size="1">
                     <option>Selecione...</option>

                     <?php $bquery = mysql_query("SELECT Id, Nome FROM comp_estado ORDER BY Nome ASC");
                     while($estado = mysql_fetch_array($bquery)) { ?>
                     <option class='form-control' value="<?php echo $estado['Id'] ?>"><?php echo $estado['Nome'] ?></option>
                     <?php } ?>

            </select></td>
    </tr>



    <tr>
        <td>E-mail:</td>
        <td><input type="email" name="email"  required class='form-control'/></td>
    </tr>

    <tr>
        <td>Contato:</td>
        <td><input type="text" name="contato"  required class='form-control'/></td>
    </tr>

    <tr>
        <td>Telefone:</td>
        <td><input type="text" name="fone"  required class='form-control'/> 
        </td>
    </tr>

    <tr>
        <td> Nome do Banco:</td>
        <td><select name="banco">
                     <option>Selecione...</option>

                     <?php $bquery = mysql_query("SELECT id_banco, nomebanco FROM fat_bancos");
                     while($nbanco = mysql_fetch_array($bquery)) { ?>
                     <option class='form-control' value="<?php echo $nbanco['id_banco'] ?>"><?php echo $nbanco['nomebanco'] ?></option>
                     <?php } ?>

            </select></td>
    </tr>    
        <tr>
        <td>Número da conta</td>
        <td><input type="text" name="nconta"  required class='form-control'/></td>
    </tr>
    <tr>
        <td>Número da Agencia:</td>
        <td><input type="text" name="nagencia"  required class='form-control'/></td>
    </tr>

         <td>
            <center>
            <input type="Submit" name="Submit" value="Salvar" class="btn btn-primary">      
            </center>
        </td>           
    </tr> 

    <tr>
        <td>


    </tr>
    </table>


   </div>
   </form>

And here PHP with Insert:

<?php

include($_SERVER['DOCUMENT_ROOT']."/NovoModelo/config.php");


if(isset($_POST['Submit'])) {



$nomefantasia = mysqli_real_escape_string($mysqli, trim($_POST['nomefantasia']));
$razaosocial = mysqli_real_escape_string($mysqli, trim($_POST['razaosocial']));
$endereco = mysqli_real_escape_string($mysqli, trim($_POST['endereco']));
$numero = mysqli_real_escape_string($mysqli, trim($_POST['numero']));
$bairro = mysqli_real_escape_string($mysqli, trim($_POST['bairro']));
$idcidade =  $_POST['cidade'];
$idestado = $_POST['estado'];
$email = mysqli_real_escape_string($mysqli, trim($_POST['email']));
$contato = mysqli_real_escape_string($mysqli, trim($_POST['contato']));
$fone = mysqli_real_escape_string($mysqli, trim($_POST['fone']));
$cnpj = mysqli_real_escape_string($mysqli, trim($_POST['cnpj']));
$cep = mysqli_real_escape_string($mysqli, trim($_POST['cep']));
$inscricaoestadual = mysqli_real_escape_string($mysqli, trim($_POST['inscricaoestadual']));
$nbanco = mysqli_real_escape_string($mysqli, trim($_POST['banco']));
$nconta = mysqli_real_escape_string($mysqli, trim($_POST['nconta']));
$nagencia = mysqli_real_escape_string($mysqli, trim($_POST['nagencia']));
$categoria = $_POST['categoria'];


 $result = mysqli_query($mysqli, "INSERT INTO comp_fornecedor(FORN_ID, FORN_NOMEFANTASIA, FORN_RAZAOSOCIAL, FORN_ENDERECO, FORN_NUMERO, FORN_BAIRRO, FORN_IDCIDADE, FORN_IDESTADO, FORN_EMAIL, FORN_CONTATO, FORN_FONE, FORN_CNPJ, FORN_CEP,FORN_INSCRICAOESTADUAL, FORN_CATEGORIAID, FORN_IDBANCO,FORN_NCONTA,FORN_NAGENCIA) 
VALUES('','$nomefantasia', '$razaosocial', '$endereco','$numero', '$bairro', '$idcidade', '$idestado', '$email', '$contato', '$fone','$cnpj','$cep','$inscricaoestadual','$categoria','$nbanco','$nconta', '$nagencia')") or die(mysqli_error($mysqli));

}
?>

Thank you in advance!

  • it is on an external page. In which I am calling on include.

  • "or die(mysqli_error))" I put to present error and still nothing.

  • In Insert it would not be ...or die (mysql_error())? Is only (mysql_error)

  • fixed or die (mysql_error()). Still, nothing yet.

  • note that the variable $razaosocial has to be wrapped in simple quotes like the others.

  • Boy, I created a bank just to test this and it’s working perfectly. Can you edit your question and put all the code fixed?

Show 1 more comment

2 answers

2


To display the error, if there is an error in the query you should use mysqli_error and not mysql_error, this this wrong:

 or die(mysql_error));

Aside from the mysqli_query has no parentheses at the end ), in:

$result = mysqli_query($mysqli, "INSERT INTO comp_fornecedor(FORN_ID, FORN_NOMEFANTASIA, FORN_RAZAOSOCIAL, FORN_ENDERECO, FORN_NUMERO, FORN_BAIRRO, FORN_IDCIDADE, FORN_IDESTADO, FORN_EMAIL, FORN_CONTATO, FORN_FONE, FORN_CNPJ, FORN_CEP,FORN_INSCRICAOESTADUAL, FORN_CATEGORIAID, FORN_IDBANCO,FORN_NCONTA,FORN_NAGENCIA) 
VALUES('','$nomefantasia', $razaosocial, '$endereco','$numero', '$bairro', '$idcidade', '$idestado', '$email', '$contato', '$fone','$cnpj','$cep','$inscricaoestadual','$categoria','$nbanco','$nconta', '$nagencia')" or die(mysql_error));

It should be right:

$result = mysqli_query($mysqli, "INSERT INTO comp_fornecedor(FORN_ID, FORN_NOMEFANTASIA, FORN_RAZAOSOCIAL, FORN_ENDERECO, FORN_NUMERO, FORN_BAIRRO, FORN_IDCIDADE, FORN_IDESTADO, FORN_EMAIL, FORN_CONTATO, FORN_FONE, FORN_CNPJ, FORN_CEP,FORN_INSCRICAOESTADUAL, FORN_CATEGORIAID, FORN_IDBANCO,FORN_NCONTA,FORN_NAGENCIA) 
VALUES('','$nomefantasia', $razaosocial, '$endereco','$numero', '$bairro', '$idcidade', '$idestado', '$email', '$contato', '$fone','$cnpj','$cep','$inscricaoestadual','$categoria','$nbanco','$nconta', '$nagencia')") or die(mysqli_error($mysqli));

You’re probably not seeing any errors because the errors should be turned off in php.ini:

display_errors=Off

I recommend that if it’s on your computer to enable, just edit php.ini:

display_errors=On

After adjusting you need to restart Apache or Nginx or Lightttpd or IIS

Also check whether error_reporting this equal to E_ALL, may be some configuration in your NovoModelo/config.php, see if you haven’t adjusted the function error_reporting(...);.

I really recommend reading this to understand about PHP errors:

Also note that maybe your settings in the scripts have no effect because you are using opcode, for example Xcache, Apcache or Opcache, recommend you read this:

Do not mix mysql_with mysqli_

I noticed you were mixing mysql_ which are functions of the old API with mysqli_, this must be causing the whole problem, I recommend you change everything to mysqli which is the newest API, here I explain why not use the old Apis /a/66489/3635

See here for example you mixedmysqli_ and mysql_:

 <option>Selecione...</option>
 <?PHP mysql_connect('localhost','root','') or die(mysql_error());

    mysql_select_db('suinos') or die(mysql_error());  <--- Aqui
    mysqli_set_charset($mysqli,"utf8"); <--- Aqui
    ?>
 <?php $query = mysql_query("SELECT categoria_id, categoria_titulo FROM comp_categoria");
 while($categoria = mysql_fetch_array($query)) { ?>
 <option class='form-control' value="<?php echo $categoria['categoria_id'] ?>"><?php echo $categoria['categoria_titulo'] ?></option>
 <?php } ?> 

Something else is not necessary to open connection to the mysql server several times for each <select> that you created, just once and take the Handle, another thing that was missing was to escape some variables, do so:

$nomefantasia = mysqli_real_escape_string($mysqli, $_POST['nomefantasia']);
$razaosocial = mysqli_real_escape_string($mysqli, $_POST['razaosocial']);
$endereco = mysqli_real_escape_string($mysqli, $_POST['endereco']);
$numero = mysqli_real_escape_string($mysqli, $_POST['numero']);
$bairro = mysqli_real_escape_string($mysqli, $_POST['bairro']);
$idcidade =  mysqli_real_escape_string($mysqli, $_POST['cidade']);
$idestado = mysqli_real_escape_string($mysqli, $_POST['estado']);
$email = mysqli_real_escape_string($mysqli, $_POST['email']);
$contato = mysqli_real_escape_string($mysqli, $_POST['contato']);
$fone = mysqli_real_escape_string($mysqli, $_POST['fone']);
$cnpj = mysqli_real_escape_string($mysqli, $_POST['cnpj']);
$cep = mysqli_real_escape_string($mysqli, $_POST['cep']);
$inscricaoestadual = mysqli_real_escape_string($mysqli, $_POST['inscricaoestadual']);
$nbanco = mysqli_real_escape_string($mysqli, $_POST['banco']);
$nconta = mysqli_real_escape_string($mysqli, $_POST['nconta']);
$nagencia = mysqli_real_escape_string($mysqli, $_POST['nagencia']);
$categoria = mysqli_real_escape_string($mysqli, $_POST['categoria']);

1

Your query has errors:

  • mysqli_error and not mysql_error as my comment that by the way also has errors :) the correct is or die(mysqli_error($mysqli))

  • the variable $razaosocial has to be wrapped in simple quotes like the other.

    $result = mysqli_query($mysqli, "INSERT INTO comp_fornecedor(FORN_ID, FORN_NOMEFANTASIA, FORN_RAZAOSOCIAL, FORN_ENDERECO, FORN_NUMERO, FORN_BAIRRO, FORN_IDCIDADE, FORN_IDESTADO, FORN_EMAIL, FORN_CONTATO, FORN_FONE, FORN_CNPJ, FORN_CEP,FORN_INSCRICAOESTADUAL, FORN_CATEGORIAID, FORN_IDBANCO,FORN_NCONTA,FORN_NAGENCIA) 
    VALUES('','$nomefantasia', '$razaosocial', '$endereco','$numero', '$bairro', '$idcidade', '$idestado', '$email', '$contato', '$fone','$cnpj','$cep','$inscricaoestadual','$categoria','$nbanco','$nconta', '$nagencia')") or die(mysqli_error($mysqli));
    
  • Fixed but, still with the same problem.

  • Boy, I created a bank just to test this and it’s working perfectly. Can you edit your question and put all the code fixed?

  • I edited, I’m sorry I’m new and I’m adapting to the forum.

  • It must have something to do with my database then. I’m using phpmyadmin. I am saying this because I created another page with a simple form. I created the table for the test. And it is not saving or returning errors.

Browser other questions tagged

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