PHP - No ejecutar un insert si el campo esta vacio

 
Vista:
sin imagen de perfil
Val: 14
Ha aumentado su posición en 14 puestos en PHP (en relación al último mes)
Gráfica de PHP

No ejecutar un insert si el campo esta vacio

Publicado por David (9 intervenciones) el 28/12/2017 20:40:07
BUeno, el titulo no es muy claro, asiq ue me explicare lo mejor posible por aqui, tengo el siguiente codigo:
codigo HTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<table border="5px" align="center" class="tb">
	<tr>
		<td>LINEA</td>
		<td>MESA</td>
		<td>CANASTILLAS DISPONIBLES</td>
		<td>CANASTILLAS ASIGNADAS</td>
		<td>HORA DE INICIO</td>
		<td>HORA DE FINALIZACION</td>
	</tr>
	<tr>
		<td> <input list="filas" name="fila1" placeholder="seleccione fila"> </td>
		<td> <input type="text" name="mesa1" placeholder="seleccione mesa"> </td>
		<td> <input type=t ext name=valor1 id=uno></td>
		<td> <input type=t ext name=valor2 id=dos></td>
		<td> <input type="time" name="hing1"> </td>
		<td> <input type="time" name="hpro1"> </td>
	</tr>
	<tr>
		<td> <input list="filas" name="fila2" placeholder="..."> </td>
		<td> <input type="text" name="mesa2" placeholder="..."> </td>
		<td> <input type=text name=valor3 id=tres min="0"></td>
		<td> <input type=text name=valor4 id=cuatro></td>
		<td> <input type="time" name="hing2"> </td>
		<td> <input type="time" name="hpro2"> </td>
	</tr>
	<tr>
		<td> <input list="filas" name="fila3" placeholder="..."> </td>
		<td> <input type="text" name="mesa3" placeholder="..."> </td>
		<td> <input type=text name=valor5 id=cinco min="0"></td>
		<td> <input type=text name=valor6 id="seis"></td>
		<td> <input type="time" name="hing3"> </td>
		<td> <input type="time" name="hpro3"> </td>
		<tr>
			<td></td>
			<td>Canastillas faltantes: </td>
			<td> <input type=text name=valor7 id=siete min="0"></td>
		</tr>

y este codigo php:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
$fechae = $_POST["fechae"];
$fruta = $_POST["fruta"];
$prove = $_POST["prove"];
 
 
    //Estas son las variables para la tabla del patinador
 
$fila1 = $_POST["fila1"];
$fila2 = $_POST["fila2"];
$fila3 = $_POST["fila3"];
$mesa1 = $_POST["mesa1"];
$mesa2 = $_POST["mesa2"];
$mesa3 = $_POST["mesa3"];
$disp1 = $_POST["valor1"];
$disp2 = $_POST["valor3"];
$disp3 = $_POST["valor5"];
$qued1 = $_POST["valor2"];
$qued2 = $_POST["valor4"];
$qued3 = $_POST["valor6"];
$hing1 = $_POST["hing1"];
$hing2 = $_POST["hing2"];
$hing3 = $_POST["hing3"];
$hpro1 = $_POST["hpro1"];
$hpro2 = $_POST["hpro2"];
$hpro3 = $_POST["hpro3"];
 
    // Codigo para insertar en la tabla patinador
 
 $insertar = mysqli_query($mysqli,"INSERT INTO patinador (FECHA_PROCESO,FECHA_ENTRADA_FRUTA,FRUTA,CODIGO_PROVEEDOR,HORA_PROCESOI,HORA_PROCESOF) VALUES (now(),'$fechae','$fruta','$prove','$hing1','$hpro1')");
if (!$insertar){echo "Error al guardar";}else{echo "Guardado con exito";}
 
 
 
    // Codigo sql para insertar datos de la tabla manejo
 
$insertarr = mysqli_query($mysqli,"INSERT INTO manejo (FECHA_PROCESO,LINEA,MESA,CANASTILLAS_DISPONIBLES,CANASTILLAS_RESTANTES,HORA_PROCESOI,HORA_PROCESOF) VALUES (now(),'$fila1','$mesa1','$disp1','$qued1','$hing1','$hpro1')");
if (!$insertarr){echo "Error al guardar";}else{echo "";}
 
$insertarr = mysqli_query($mysqli,"INSERT INTO manejo (FECHA_PROCESO,LINEA,MESA,CANASTILLAS_DISPONIBLES,CANASTILLAS_RESTANTES,HORA_PROCESOI,HORA_PROCESOF) VALUES (now(),'$fila2','$mesa2','$disp2','$qued2','$hing2','$hpro2')");
if (!$insertarr){echo "Error al guardar";}else{echo "";}
 
 
 $insertarr = mysqli_query($mysqli,"INSERT INTO manejo (FECHA_PROCESO,LINEA,MESA,CANASTILLAS_DISPONIBLES,CANASTILLAS_RESTANTES,HORA_PROCESOI,HORA_PROCESOF) VALUES (now(),'$fila3','$mesa3','$disp3','$qued3','$hing3','$hpro3')");
if (!$insertarr){echo "Error al guardar";}else{echo "";}
 
$borrar = mysqli_query($mysqli,"DELETE FROM `validar` WHERE 1");
    if (!$borrar){echo "no borro";}else{echo "";}
 
 
?>

todo funciona pero mi pregunta es, ¿es posible que si en la tabla dejo el input vacio, el codigo php que hace el insert no se ejecute, y si es posible como lo hago, si me logran entender jaja les doy gracias.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 604
Bronce
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

No ejecutar un insert si el campo esta vacio

Publicado por Miguel (218 intervenciones) el 29/12/2017 04:59:43
1
2
3
4
5
if (empty($campoX))
{
    echo "Complete el campo x.";
    exit();
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Georkis Santiesteban
Val: 75
Ha aumentado 1 puesto en PHP (en relación al último mes)
Gráfica de PHP

No ejecutar un insert si el campo esta vacio

Publicado por Georkis Santiesteban (32 intervenciones) el 29/12/2017 17:16:36
lo puede hacer de dos manera, las dos es validar lo que viene por request (POST, GET o $_REQUEST)

el primer codigo quedaria asi...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
 
if($fila1 =="" and $fila2 and $fila3, $mesa1){ //Poner todas las variables q faltan
     echo "Todos los campos son requeridos";
}else{
 $insertar = mysqli_query($mysqli,"INSERT INTO patinador (FECHA_PROCESO,FECHA_ENTRADA_FRUTA,FRUTA,CODIGO_PROVEEDOR,HORA_PROCESOI,HORA_PROCESOF) VALUES (now(),'$fechae','$fruta','$prove','$hing1','$hpro1')");
 
if (!$insertar){echo "Error al guardar";}else{echo "Guardado con exito";}
 
    // Codigo sql para insertar datos de la tabla manejo
 
$insertarr = mysqli_query($mysqli,"INSERT INTO manejo (FECHA_PROCESO,LINEA,MESA,CANASTILLAS_DISPONIBLES,CANASTILLAS_RESTANTES,HORA_PROCESOI,HORA_PROCESOF) VALUES (now(),'$fila1','$mesa1','$disp1','$qued1','$hing1','$hpro1')");
 
if (!$insertarr){echo "Error al guardar";}else{echo "";}
 
 
 
$insertarr = mysqli_query($mysqli,"INSERT INTO manejo (FECHA_PROCESO,LINEA,MESA,CANASTILLAS_DISPONIBLES,CANASTILLAS_RESTANTES,HORA_PROCESOI,HORA_PROCESOF) VALUES (now(),'$fila2','$mesa2','$disp2','$qued2','$hing2','$hpro2')");
 
if (!$insertarr){echo "Error al guardar";}else{echo "";}
 $insertarr = mysqli_query($mysqli,"INSERT INTO manejo (FECHA_PROCESO,LINEA,MESA,CANASTILLAS_DISPONIBLES,CANASTILLAS_RESTANTES,HORA_PROCESOI,HORA_PROCESOF) VALUES (now(),'$fila3','$mesa3','$disp3','$qued3','$hing3','$hpro3')");
 
if (!$insertarr){echo "Error al guardar";}else{echo "";}
 
 
 
$borrar = mysqli_query($mysqli,"DELETE FROM `validar` WHERE 1");
 
    if (!$borrar){echo "no borro";}else{echo "";}
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar