Foros del Web » Programando para Internet » PHP »

Condicion if

Estas en el tema de Condicion if en el foro de PHP en Foros del Web. Hola a todos; Me esta volviendo loco este IF, lo que quiero hacer es que si el campo $nombre_img llegue vacio no me inserte el ...
  #1 (permalink)  
Antiguo 28/10/2009, 18:44
 
Fecha de Ingreso: febrero-2008
Mensajes: 675
Antigüedad: 16 años, 3 meses
Puntos: 5
Condicion if

Hola a todos;
Me esta volviendo loco este IF, lo que quiero hacer es que si el campo $nombre_img llegue vacio no me inserte el campo "imagen" y me escriba en la bd "noimage.jpg", lo tengo echo asi...

Código PHP:
srand (time());
//generamos un número aleatorio
$mismo=str_replace(" ","_",$mismo);
$mismo rand(1,100000000000); 

$nombre_img str_replace(" ","",$_FILES['imagen'] ['name']);

$editFormAction $_SERVER['PHP_SELF'];
if (isset(
$_SERVER['QUERY_STRING'])) {
  
$editFormAction .= "?" htmlentities($_SERVER['QUERY_STRING']);
}
if ((
$nombre_img)!="") {
 
if ((isset(
$_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  
$updateSQL sprintf("UPDATE articulos SET articulo=%s, argrupo=%s, arforma=%s, quien=%s, precio=%s, imagenarticulo=%s, ud=%s WHERE id_articulos=%s",
                       
GetSQLValueString($_POST['articulo'], "text"),
                       
GetSQLValueString($_POST['grupo'], "int"),
                       
GetSQLValueString($_POST['forma'], "int"),
                       
GetSQLValueString($_POST['quien'], "text"),
                       
GetSQLValueString($_POST['precio'], "double"),
                       
GetSQLValueString("../archivos/".$mismo.$nombre_img"text"),
                       
GetSQLValueString($_POST['uc'], "int"),
                       
GetSQLValueString($_POST['id'], "int"));

$copy copy($_FILES['imagen']['tmp_name'], "../archivos/" .$mismo .$nombre_img);
}
else {

  
$updateSQL sprintf("UPDATE articulos SET articulo=%s, argrupo=%s, arforma=%s, quien=%s, precio=%s, imagenarticulo=%s, ud=%s WHERE id_articulos=%s",
                       
GetSQLValueString($_POST['articulo'], "text"),
                       
GetSQLValueString($_POST['grupo'], "int"),
                       
GetSQLValueString($_POST['forma'], "int"),
                       
GetSQLValueString($_POST['quien'], "text"),
                       
GetSQLValueString($_POST['precio'], "double"),
                       
GetSQLValueString("../archivos/noimage.jpg""text"),
                       
GetSQLValueString($_POST['uc'], "int"),
                       
GetSQLValueString($_POST['id'], "int"));
                  }     
                       
  
mysql_select_db($database_deume$deume);
  
$Result1 mysql_query($updateSQL$deume) or die(mysql_error());

  
$updateGoTo "mod_articulo.php";
  if (isset(
$_SERVER['QUERY_STRING'])) {
    
$updateGoTo .= (strpos($updateGoTo'?')) ? "&" "?";
    
$updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  
header(sprintf("Location: %s"$updateGoTo));

y este es el form
Código PHP:
<form action="<?php echo $editFormAction?>" id="form1" name="form1" method="POST" enctype="multipart/form-data">
          <table width="635">
            <tr>
              <td width="127">Articulo:</td>
              <td width="285"><input name="articulo" type="text" id="articulo" value="<?php echo $row_articulo['articulo']; ?>" /></td>
              <td width="102"><a href="<?php printf("%s?pageNum_articulo=%d%s"$currentPagemin($totalPages_articulo$pageNum_articulo 1), $queryString_articulo); ?>">Siguiente</a></td>
              <td width="103"><a href="<?php printf("%s?pageNum_articulo=%d%s"$currentPagemax(0$pageNum_articulo 1), $queryString_articulo); ?>">Anterior</a></td>
            </tr>
            <tr>
              <td>Grupo:</td>
              <td><select name="grupo" id="grupo">
                <?php
do {  
?>
                <option value="<?php echo $row_grupo['id_grupo']?>"<?php if (!(strcmp($row_grupo['id_grupo'], $row_articulo['argrupo']))) {echo "selected=\"selected\"";} ?>><?php echo $row_grupo['grupo']?></option>
                <?php
} while ($row_grupo mysql_fetch_assoc($grupo));
  
$rows mysql_num_rows($grupo);
  if(
$rows 0) {
      
mysql_data_seek($grupo0);
      
$row_grupo mysql_fetch_assoc($grupo);
  }
?>
              </select></td>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <td>Presentación:</td>
              <td><select name="forma" id="forma">
                  <?php
do {  
?><option value="<?php echo $row_forma['id_forma']?>"<?php if (!(strcmp($row_forma['id_forma'], $row_articulo['arforma']))) {echo "selected=\"selected\"";} ?>><?php echo $row_forma['forma']?></option>
                  <?php
} while ($row_forma mysql_fetch_assoc($forma));
  
$rows mysql_num_rows($forma);
  if(
$rows 0) {
      
mysql_data_seek($forma0);
      
$row_forma mysql_fetch_assoc($forma);
  }
?>
              </select></td>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <td>Proveedor</td>
              <td><select name="quien" id="quien">
                  <option value="G" <?php if (!(strcmp("G"$row_articulo['quien']))) {echo "selected=\"selected\"";} ?>>INTERNO</option>
                  <option value="S" <?php if (!(strcmp("S"$row_articulo['quien']))) {echo "selected=\"selected\"";} ?>>EXTERNO</option>
              </select></td>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <td>Imagen:</td>
              <td><input name="imagen" type="file" id="imagen" value="" /></td>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <td>Precio:</td>
              <td><input name="precio" type="text" id="precio" value="<?php echo $row_articulo['precio']; ?>" size="4" />
                €</td>
              <td colspan="2"><input name="id" type="hidden" id="id" value="<?php echo $row_articulo['id_articulos']; ?>" /></td>
            </tr>
            <tr>
              <td>Unidades:</td>
              <td><label>
                <input name="uc" type="text" id="uc" value="<?php echo $row_articulo['ud']; ?>" size="4" />
              </label></td>
              <td colspan="2">Unidades por envase</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td><label>
                <input name="button" type="submit" class="boton" id="button" value="MODIFICAR ARTICULO" />
              </label></td>
              <td colspan="2">&nbsp;</td>
            </tr>
          </table>
          <input type="hidden" name="MM_update" value="form1" />
        </form>
El caso es que si coloco una imagen, me funciona, pero si no coloco imagen, no me hace el update.
Alguien me puede ayudar?
Un saludo
  #2 (permalink)  
Antiguo 28/10/2009, 21:35
Avatar de mortiprogramador
Colaborador
 
Fecha de Ingreso: septiembre-2009
Ubicación: mortuoria
Mensajes: 3.805
Antigüedad: 14 años, 8 meses
Puntos: 214
Respuesta: Condicion if

Hola
Pues creo que es porque tu estas haciendo la clausula "else" dentro del if interno, no dentro del general, es decir tienes esto

Código PHP:
if (($nombre_img)!="") {
echo 
"HOLA"
if ((isset(
$_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
//$updateSQL con el valor de la imagen
}else{
//$updateSQL con noimage
}
  
mysql_select_db($database_deume$deume);
  
$Result1 mysql_query($updateSQL$deume) or die(mysql_error());

  
$updateGoTo "mod_articulo.php";
  if (isset(
$_SERVER['QUERY_STRING'])) {
    
$updateGoTo .= (strpos($updateGoTo'?')) ? "&" "?";
    
$updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  
header(sprintf("Location: %s"$updateGoTo));
}
//FIN DEL IF DE $nombre_img!="" 
y si te fijas, obviamente no hara el else internamente pues ya la primera condición lo restringe, ese else creería yo que va por fuera del primer if para que sea el else del if($nombre_img != ""), revisa y nos comentas
saludos
  #3 (permalink)  
Antiguo 29/10/2009, 01:12
Avatar de luckystrikede11  
Fecha de Ingreso: octubre-2009
Ubicación: Concepcion, Chile
Mensajes: 240
Antigüedad: 14 años, 6 meses
Puntos: 17
Respuesta: Condicion if

Código PHP:
if (empty($nombre_img))
    echo 
"sin imagen";
else
   echo 
"con imagen"
puede que sirva
  #4 (permalink)  
Antiguo 29/10/2009, 08:35
 
Fecha de Ingreso: febrero-2008
Mensajes: 675
Antigüedad: 16 años, 3 meses
Puntos: 5
Respuesta: Condicion if

Gracias mortiprogramador, ya me funciona, tenias mucha razon.

Un saludo
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 21:16.