Problem in insert does not display error [closed]

0

Personal beauty all? I've already gone through the entire forum and up to other forums and found no answers about it.  If anyone can take a look and point out my error thank you.

Follow the form below:

   <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's PHP with the 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!

    
asked by anonymous 27.05.2017 / 16:38

2 answers

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

 or die(mysql_error));

Otherwise the mysqli_query does not have the 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));

The right thing should be this:

$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 are probably not seeing any errors because the errors should be turned off in php.ini:

display_errors=Off

I recommend that if it is on your computer, just edit php.ini:

display_errors=On

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

Also check that error_reporting is equal to E_ALL , it may be some configuration in your NovoModelo/config.php , see if you did not set the error_reporting(...); function.

I even recommend you read this to understand about PHP errors:

Note also that your script settings may not have an effect because you are using opcode, such as Xcache , Apcache or Opcache , I recommend read this:

Do not mix mysql_ with mysqli _

I noticed that you are mixing mysql_ which are functions of the old API with mysqli_ , this should be causing the whole problem, I recommend that you change everything to mysqli which is the newest API, here I explain why do not use old APIs link

See here for example you mixed mysqli_ 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 } ?> 

Another thing is not necessary to be opening connection to the mysql server several times for each <select> you created, just once and get the handle , another missing thing 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']);
    
27.05.2017 / 20:00
1
  

Your query has errors:

  • mysqli_error and not mysql_error according to my comment that by tb sign has errors :) the correct is or die(mysqli_error($mysqli))

  • The variable $razaosocial has to be enclosed in single quotation marks like the others.

    $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));
    
27.05.2017 / 19:28