Foros del Web » Programando para Internet » PHP »

problemas un sentencia if

Estas en el tema de problemas un sentencia if en el foro de PHP en Foros del Web. tengo una duda de como hacer esta comparacion osea no ho podido crear la sentencia correcta seria posible que me dieran una mano y me ...
  #1 (permalink)  
Antiguo 12/12/2008, 13:49
 
Fecha de Ingreso: julio-2007
Mensajes: 114
Antigüedad: 16 años, 8 meses
Puntos: 0
Pregunta problemas un sentencia if

tengo una duda de como hacer esta comparacion osea no ho podido crear la sentencia correcta seria posible que me dieran una mano y me ayuden con este codigo
Código PHP:
<?php
include("conex.php");
$tipo=$_POST['tipo'];
$pb=$_POST['pb'];
$oficina=$_POST['oficina'] ;
$ubicacion=$_POST['ubicacion'] ;
$usuario=$_POST['usuario'] ;
$estatus=$_POST['estatus'] ;
$bienc=$_POST['bienc'] ;
$serialc=$_POST['serialc'];
$marcac=$_POST['marcac'];
$bienm=$_POST['bienm'];
$serialm=$_POST['serialm'];
$marcam=$_POST['marcam'];
$bient=$_POST['bient'];
$serialt=$_POST['serialt'];
$marcat=$_POST['marcat'];
$bienmo=$_POST['bienmo'];
$serialmo=$_POST['serialmo'];
$marcamo=$_POST['marcamo'];
$bienr=$_POST['bienr'];
$serialr=$_POST['serialr'];
$marcar=$_POST['marcar'];
$observa=$_POST['observa'];


$consulta_usuario mysql_query ("SELECT * FROM `equipo` "$link);

$row mysql_num_rows($consulta_usuario);
$serial1=$row["serialc"];
$serial2 =$row["serialm"];
$serial3 =$row["serialt"];
$serial4 =$row["serialmo"];
$serial5 =$row["serialr"];



   if (
$serial1 == $serialc){
      print 
"ESTE SERIA LDE CPU= $serialc  YA EXISTE\n<br>";}
       elseif (
$serial2 == $serialm){
       print 
"ESTE SERIAL DE MONITOR= $serialm  YA EXISTE\n<br>";}
   elseif (
$serial3 == $serialt){
      print 
"ESTE SERIAL DE TECLADO= $seriat  YA EXISTE\n<br>";}
elseif (
$serial4 == $serialmo){
      print 
"ESTE SERIAL DE MOUSE= $serialmo  YA EXISTE\n<br>";
      }
elseif (
$serial5 == $serialr){
      print 
"ESTE SERIAL DE REGULADOR= $serialr  YA EXISTE\n<br>";
      }


       else {

$ingresa=mysql_query("INSERT INTO equipo (tipo,pb,oficina,ubicacion,usuario,estatus,bienc,serialc,marcac,bienm,serialm,marcam,bient,serialt,marcat,bienmo,serialmo,marcamo,bienr,serialr,marcar,observa) VALUES ('$tipo','$pb','$oficina','$ubicacion','$usuario','$estatus','$bienc','$serialc','$marcac','$bienm','$serialm','$marcam','$bient','$serialt','$marcat','$bienmo','$serialmo','$marcamo','$bienr','$serialr','$marcar','$observa')");


mysql_query($link);
      if (
$ingresa) {
         print 
"SE INGRESO EL CORRECTAMENTE EL INVENTARIO";
      } else {
         print 
"ERROR, NO SE PUDO INGRESAR ";
      }
}   

?>
  #2 (permalink)  
Antiguo 12/12/2008, 13:51
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 10 meses
Puntos: 2135
Respuesta: problemas un sentencia if

No entiendo porque abres los ifs y nunca los cierras, por otro lado mejor te conviene separar los ifs en lugar de anidarlos y mejor mantener un flag booleano para ver si hay un error y desplegar los mensajes.

Saludos.
  #3 (permalink)  
Antiguo 12/12/2008, 13:57
 
Fecha de Ingreso: julio-2007
Mensajes: 114
Antigüedad: 16 años, 8 meses
Puntos: 0
gracias por responder tan rapido

dejame averiguar como es lo de los if anidados a ver si lo puedo hacer como tu dices

epalex GatorV me podrias explicar como es que tu dices que haga porfa

Última edición por GatorV; 12/12/2008 a las 16:02
  #4 (permalink)  
Antiguo 12/12/2008, 16:06
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 10 meses
Puntos: 2135
Respuesta: problemas un sentencia if

Pues simplemente compara cada if por separado y con un campo booleano:
Código php:
Ver original
  1. $todoBien = true;
  2. if($algo != $otracosa) {
  3.      $todoBien = false;
  4. }
  5. if($otro == $otra) {
  6.      $todoBien = false;
  7. }
  8.  
  9. if($todoBien) {
  10.      // Inserto en la BDD
  11. } else {
  12.      // muestro algun error
  13. }

Saludos.
  #5 (permalink)  
Antiguo 12/12/2008, 16:31
 
Fecha de Ingreso: julio-2007
Mensajes: 114
Antigüedad: 16 años, 8 meses
Puntos: 0
Respuesta: problemas un sentencia if

ealex GatorV

intente como tu me dijiste pero que va no cuadro lo estoy intentando asi tu crees que tenga logica
Código PHP:
<?php
include("conex.php");
$tipo=$_POST['tipo'];
$pb=$_POST['pb'];
$oficina=$_POST['oficina'] ;
$ubicacion=$_POST['ubicacion'] ;
$usuario=$_POST['usuario'] ;
$estatus=$_POST['estatus'] ;
$bienc=$_POST['bienc'] ;
$serialc=$_POST['serialc'];
$marcac=$_POST['marcac'];
$bienm=$_POST['bienm'];
$serialm=$_POST['serialm'];
$marcam=$_POST['marcam'];
$bient=$_POST['bient'];
$serialt=$_POST['serialt'];
$marcat=$_POST['marcat'];
$bienmo=$_POST['bienmo'];
$serialmo=$_POST['serialmo'];
$marcamo=$_POST['marcamo'];
$bienr=$_POST['bienr'];
$serialr=$_POST['serialr'];
$marcar=$_POST['marcar'];
$observa=$_POST['observa'];


$consulta_usuario mysql_query ("SELECT * FROM `equipo` "$link);

$row mysql_num_rows($consulta_usuario);
//$serial1=$row["serialc"];
//$serial2 =$row["serialm"];
//$serial3 =$row["serialt"];
//($serial4 =$row["serialmo"];
//$serial5 =$row["serialr"];
while  ($row mysql_num_rows ($consulta_usuario)) {

$serial1=$_POST['serialc'] == $row['serialc'];
echo 
$serial1;
$serial2=$_POST['serialm'] == $row['serialm'];
echo 
$serial2;
$serial3=$_POST['serialt'] == $row['serialt'];
echo 
$serial3;
$serial4=$_POST['serialmo'] == $row['serialmo'];
echo 
$serial4;
$serial5=$_POST['serialr'] == $row['serialr'];
echo 
$serial5
}

else
{
$ingresa=mysql_query("INSERT INTO equipo (tipo,pb,oficina,ubicacion,usuario,estatus,bienc,serialc,marcac,bienm,serialm,marcam,bient,serialt,marcat,bienmo,serialmo,marcamo,bienr,serialr,marcar,observa) VALUES ('$tipo','$pb','$oficina','$ubicacion','$usuario','$estatus','$bienc','$serialc','$marcac','$bienm','$serialm','$marcam','$bient','$serialt','$marcat','$bienmo','$serialmo','$marcamo','$bienr','$serialr','$marcar','$observa')");


mysql_query($link);
      if (
$ingresa) {
         print 
"SE INGRESO EL CORRECTAMENTE EL INVENTARIO";
      } else {
         print 
"ERROR, NO SE PUDO INGRESAR ";
      }
}   

?>
  #6 (permalink)  
Antiguo 12/12/2008, 16:34
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 10 meses
Puntos: 2135
Respuesta: problemas un sentencia if

Pues no veo donde uses ifs() y si vas a recorrer toda la tabla, mejor te conviene filtrar directo desde SQL usando WHERE para que veas si hay registros similares. O si es tan criticio el proceso que le agregues unos indices UNIQUE a esas columnas para que no se puedan duplicar los resultados.

Saludos.
  #7 (permalink)  
Antiguo 12/12/2008, 16:41
 
Fecha de Ingreso: julio-2007
Mensajes: 114
Antigüedad: 16 años, 8 meses
Puntos: 0
disculpa pero me podrias decir como hacer para comparar esas variables con la bd y mostrar cual serial esta repetido

ve como lo llevo
Código PHP:
<?php
include("conex.php");
$tipo=$_POST['tipo'];
$pb=$_POST['pb'];
$oficina=$_POST['oficina'] ;
$ubicacion=$_POST['ubicacion'] ;
$usuario=$_POST['usuario'] ;
$estatus=$_POST['estatus'] ;
$bienc=$_POST['bienc'] ;
$serialc=$_POST['serialc'];
$marcac=$_POST['marcac'];
$bienm=$_POST['bienm'];
$serialm=$_POST['serialm'];
$marcam=$_POST['marcam'];
$bient=$_POST['bient'];
$serialt=$_POST['serialt'];
$marcat=$_POST['marcat'];
$bienmo=$_POST['bienmo'];
$serialmo=$_POST['serialmo'];
$marcamo=$_POST['marcamo'];
$bienr=$_POST['bienr'];
$serialr=$_POST['serialr'];
$marcar=$_POST['marcar'];
$observa=$_POST['observa'];


$consulta_usuario mysql_query ("SELECT * FROM `equipo` WHERE serialc='$serialc' AND serialm='$serialm' AND serialt='$serialt' AND serialmo='$serialmo' AND serialr='$serialr'"$link);

$row mysql_num_rows($consulta_usuario);
//$serial1=$row["serialc"];
//$serial2 =$row["serialm"];
//$serial3 =$row["serialt"];
//($serial4 =$row["serialmo"];
//$serial5 =$row["serialr"];
if ($row=1){
while  (
$row mysql_num_rows ($consulta_usuario)) {

$serial1=$_POST['serialc'] == $row['serialc'];
echo 
$serial1;
$serial2=$_POST['serialm'] == $row['serialm'];
echo 
$serial2;
$serial3=$_POST['serialt'] == $row['serialt'];
echo 
$serial3;
$serial4=$_POST['serialmo'] == $row['serialmo'];
echo 
$serial4;
$serial5=$_POST['serialr'] == $row['serialr'];
echo 
$serial5
}
}
else
{
$ingresa=mysql_query("INSERT INTO equipo (tipo,pb,oficina,ubicacion,usuario,estatus,bienc,serialc,marcac,bienm,serialm,marcam,bient,serialt,marcat,bienmo,serialmo,marcamo,bienr,serialr,marcar,observa) VALUES ('$tipo','$pb','$oficina','$ubicacion','$usuario','$estatus','$bienc','$serialc','$marcac','$bienm','$serialm','$marcam','$bient','$serialt','$marcat','$bienmo','$serialmo','$marcamo','$bienr','$serialr','$marcar','$observa')");


mysql_query($link);
      if (
$ingresa) {
         print 
"SE INGRESO EL CORRECTAMENTE EL INVENTARIO";
      } else {
         print 
"ERROR, NO SE PUDO INGRESAR ";
      }
}   

?>
  #8 (permalink)  
Antiguo 12/12/2008, 16:43
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 10 meses
Puntos: 2135
Respuesta: problemas un sentencia if

Pues ya te dije como, tienes que hacer un WHERE directo en tu consulta SQL, ya que recorriendo toda la tabla desde PHP va a ser mas lento.

Si no tienes conocimientos al respecto te recomiendo leas un tutorial de SQL, en www.w3schools.com hay uno muy bueno.

Saludos
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 17:13.