IF conditional in MySQL

0

I'm doing a system of inventories so far all my form inserted correctly just want to place a condition that does not allow insert another record with the identifier and then leave a notice that that is wrong.

            <?php

     require("connect.php");




   if(isset($_POST['identificador']) && !empty($_POST['identificador']) &&
    isset($_POST['nombre']) && !empty($_POST['nombre']) &&
   isset($_POST['ap']) && !empty($_POST['ap']) &&
   isset($_POST['am']) && !empty($_POST['am']) &&
   isset($_POST['fecha']) && !empty($_POST['fecha']) &&
   isset($_POST['localidad']) && !empty($_POST['localidad']) &&
   isset($_POST['departamento']) && !empty($_POST['departamento'])){



   $identificadorUsuarios = $_POST['identificador'];
   $nombreUsuarios = $_POST['nombre'];
   $apUsuarios = $_POST['ap'];
   $amUsuarios = $_POST['am'];
   $fechaUsuarios = $_POST['fecha'];
   $localidadUsuarios = $_POST['localidad'];
   $departamentoUsuarios = $_POST['departamento'];


    mysql_query("INSERT INTO usuarios 
    (identificador,nombre,ap,am,fecha,localidad,departamento)
    values ('$identificadorUsuarios','$nombreUsuarios','$apUsuarios','$amUsuarios','$fechaUsuarios','$localidadUsuarios','$departamentoUsuarios')");




    echo '<script>alert("Datos Ingresados Correctamente")</script> ';

    }


    ?>

    <html>

    <head>

    <form action="" method=post name="formulario">



    <body>
    <title>Gesti&oacuten de Inventarios</title>



    <body>





    <center><fieldset style="width:40%" "width:900px">

    <td>


        <legend >Registro de Usuarios</legend>

        <div><br>

           <label>Identificador del equipo: </label></th>
            <input type="text" required name="identificador">
             </br>
              </div>

        <div><br>

   <label>Nombre </label></th>
    <input type="text" required name="nombre">
     </br>
      </div>

<div><br>

   <label>Apellido Paterno:</label></th>
    <input type="text" required name="ap">
     </br>
      </div>

<div><br>

   <label>Apellido Materno:</label></th>
    <input type="text" required name="am">
     </br>
      </div>


      <div><br>

   <label>Fecha:</label></th>
    <input type="date" required name="fecha">
     </br>
      </div>

      <div><br>

   <label>Localidad:</label></th>
    <input type="text" required name="localidad">
     </br>
      </div>


<div><br>

   <label>Departamento:</label></th>
    <input type="text" required name="departamento">
     </br>
      </div>          



    </td><center></fieldset><br></br>






    </form>



      </body>



        </html>
    
asked by Carlos 17.04.2017 в 21:17
source

2 answers

0

You can create a query, to check if the identifier exists, I'll give you the example:

<?php

         require("connect.php");

      if(isset($_POST['identificador']) && !empty($_POST['identificador']) &&
       isset($_POST['nombre']) && !empty($_POST['nombre']) &&
       isset($_POST['ap']) && !empty($_POST['ap']) &&
       isset($_POST['am']) && !empty($_POST['am']) &&
       isset($_POST['fecha']) && !empty($_POST['fecha']) &&
       isset($_POST['localidad']) && !empty($_POST['localidad']) &&
       isset($_POST['departamento']) && !empty($_POST['departamento'])){



       $identificadorUsuarios = $_POST['identificador'];
       $nombreUsuarios = $_POST['nombre'];
       $apUsuarios = $_POST['ap'];
       $amUsuarios = $_POST['am'];
       $fechaUsuarios = $_POST['fecha'];
       $localidadUsuarios = $_POST['localidad'];
       $departamentoUsuarios = $_POST['departamento'];

       //Comprobamos si ya existe
       $q = mysql_query("select identificador from usuarios where identificador = '".$_POST['identificador']."'");

       if(mysql_num_rows($q) > 0){

          echo '<script>alert("El identificador ya existe")</script> ';

       }else{

            mysql_query("INSERT INTO usuarios (identificador,nombre,ap,am,fecha,localidad,departamento)
    values ('$identificadorUsuarios','$nombreUsuarios','$apUsuarios','$amUsuarios','$fechaUsuarios','$localidadUsuarios','$departamentoUsuarios')");

           echo '<script>alert("Datos Ingresados Correctamente")</script> ';
       }
    }
  ?> 
    
answered by 17.04.2017 / 22:19
source
0

You have the option of creating a "UNIQUE" condition for the identifier in the database, and when executing the query verify the error.

$result = mysql_query("INSERT INTO usuarios 
    (identificador,nombre,ap,am,fecha,localidad,departamento)
    values ('$identificadorUsuarios','$nombreUsuarios','$apUsuarios','$amUsuarios','$fechaUsuarios','$localidadUsuarios','$departamentoUsuarios')");

if (!$result) {
    $error= 'Error en la operación: ' . mysql_error() . "\n";
}
    
answered by 17.04.2017 в 21:43