1

我需要有关使用 PHP 进行表单验证的帮助。我想在提交表单之前检查数据库中是否存在“邮件”和“d_place”字段。如果其中任何一个已经存在,您必须再次填写整个表格。因此,为了让用户更友好,最好在提交表单之前检查这个条件,或者至少能够保留输入的值,以便用户只更改产生错误的字段。

在这里,我的表格:

<form method="post" action="save_place.php">
     <section id="costado2">

        <div id="direx">
            <label for="search_new_places">Direccion del Local</label>
            <input type="text" name="d_place" placeholder="Direccion de Local.."  id="search_new_places" class='form-control' required>
        </div>

        <input type="hidden" name="place_id" id="place_id"/>

        <div id="nombre_loc">
            <label for="place">Nombre del Local</label>
            <input type="text" placeholder="Nombre de Local.." name="n_place" id="n_place" class='form-control' required>  
        </div>


        <?php


        //Get all country data
        $query = $db->query("SELECT * FROM provincias ORDER BY provincia ASC");

        //Count total number of rows
        $rowCount = $query->num_rows;

        $queryr = $db->query("SELECT * FROM rubros ORDER BY NOMBRE ASC");
        $rowCountr = $queryr->num_rows;

        ?>

        <div id="descrip">
             <select name="rubro" id="n_description" name="n_description" class='form-control' required>
                <option value="">Selecciona Rubro</option>
                <?php
                if($rowCount > 0){
                    while($row = $queryr->fetch_assoc()){ 
                        echo '<option value="'.$row['ID'].'">'.$row['NOMBRE'].'</option>';
                    }
                }else{
                    echo '<option value="">Rubro No Disponible</option>';
                }


                ?>
            </select>
        </div>

        <div id="pro">
            <select name="country" id="country" class='form-control' required>
                <option value="">Selecciona Provincia</option>
                <?php
                if($rowCount > 0){
                    while($row = $query->fetch_assoc()){ 
                        echo '<option value="'.$row['id'].'">'.utf8_encode($row['provincia']).'</option>';
                    }
                }else{
                    echo '<option value="">Provincia no Disponible</option>';
                }

                ?>
            </select>
        </div>

         <div id="loc">
            <select name="state" id="state" class='form-control' required>
                <option value="">Selecciona Localidad</option>
            </select>
        </div>

        <div id="tel">
            <label>Telefono</label>
            <input type="number" min="0" name="d_tel" placeholder="Telefono.." class='form-control'>
        </div>

        <div id="mail">
            <label>Mail</label>
            <br>
            <input name="mails" id="mails" type="email" placeholder="Mail.." class='form-control' pattern="[^@]+@[^@]+\.[a-zA-Z]{2,6}" required>
        </div>

        <div id="pass">
          <label><span>Password</span></label> <input pattern=".{5,10}" name="passw" id="passw" type="password" maxlength="10" placeholder="Password.." class='form-control' required>
        </div>

        <div id="repass">
          <label><span>Repetir Password</span></label> <input pattern=".{5,10}" name="reps" id="reps" type="password" maxlength="10" placeholder="Repetir Password.." class='form-control' required>
        </div>

        <div id="bot3">
            <input type="submit" id="btn_save" name="btn_save" class="btn btn-primary btn-sm" value="GUARDAR"/>
            <input id="lat" name="lat" value="" type="hidden" >
            <input id="lng" name="lng" value="" type="hidden" >
        </div>

        <div id="bot4">
            <a href="index.php" class="btn btn-success btn-sm"><i class="glyphicon glyphicon-backward"></i> &nbsp; VOLVER</a>
        </div>

    </section>
</form>

在这里,我进行验证的页面:

<?php
 require_once('DB.php');

  $place = $_POST['n_place'];
  $description = $_POST['rubro'];
  $latitude = $_POST['lat'];
  $longitude = $_POST['lng'];
  $direccion = $_POST['d_place'];
  $provincia = $_POST['country'];
  $localidad = $_POST['state'];
  $telefono = $_POST['d_tel'];
  $mail = $_POST['mails'];
  $pass = $_POST['passw'];
  $repass = $_POST['reps'];
  $fecha = date("Y/m/d");

  $di = ("SELECT * FROM locales WHERE DIRECCION = '$direccion'");
  $querydi = $dbh->prepare($di);
  $querydi->execute(array($direccion));

  $q = ("SELECT * FROM usuarios WHERE MAIL = '$mail'");
  $ql = ("SELECT * FROM locales WHERE MAIL = '$mail'");
  $query = $dbh->prepare($q);
  $queryl = $dbh->prepare($ql);
  $query->execute(array($mail));
  $queryl->execute(array($mail));

  session_start();
  if (($query->rowCount() == 0) && ($queryl->rowCount() == 0) && ($querydi->rowCount() == 0) && ($pass == $repass)) {

$dbp->query("INSERT INTO locales (NOMBRE, SUCURSAL, ESTADO, ID_LOCALIDAD, LAT, LNG, ID_RUBRO, ID_ROL, DIRECCION, TELEFONO, MAIL, PASSWORD, FECHA_ALTA) VALUES ('$place', 1,1,'$localidad', '$latitude', '$longitude', '$description', 3, '$direccion', '$telefono', '$mail', '$pass', '$fecha')");

$place_id = $dbp->insert_id;

    $_SESSION['msg_box'] = array("Local ingresado exitosamente", "success");
    echo header("location:login.php");
}else{
    $_SESSION['msg_box'] = array("Hubo un error al insertar el local", "warning");
    if ($querydi->rowCount() > 0) {
        $_SESSION['msg_box'][0] .= "<br>La dirección ya se encuentra registrada.";
    }
    if ($query->rowCount() > 0 || $queryl->rowCount() > 0) {
    $_SESSION['msg_box'][0] .= "<br>El mail ingresado ya se encuentra registrado.";
    }   
    echo header("location:registrar_local.php");
}


?>

非常感谢您!

4

0 回答 0