Foros del Web » Programando para Internet » PHP »

En vez de actualizar, me crea otro registro (php+mysql)

Estas en el tema de En vez de actualizar, me crea otro registro (php+mysql) en el foro de PHP en Foros del Web. Que tal colegas les cuento que desarrollo un porgrama para editar las noticias que me estan enviando, pero en ves de actualizar los registros, me ...
  #1 (permalink)  
Antiguo 13/06/2005, 20:10
 
Fecha de Ingreso: febrero-2002
Ubicación: Chile
Mensajes: 1.573
Antigüedad: 22 años, 2 meses
Puntos: 2
En vez de actualizar, me crea otro registro (php+mysql)

Que tal colegas

les cuento que desarrollo un porgrama para editar las noticias que me estan enviando, pero en ves de actualizar los registros, me crea uno nuevo, aqui va el codigo, es un poco largo pero se "entiende"

Código PHP:


<body>

<?


if($_POST['Submit'] != "Enviar")    
{
?>

<?
$db_conexion
mysql_connect("$sql_host""$sql_usuario""$sql_pass") or die(header ("Location:  $redir?error_login=0"));
mysql_select_db("$sql_db");

$usuario_consulta mysql_query("SELECT N.noticia_texto,N.noticia_titulo,N.noticia_id,N.noticia_mostrar, N.noticia_nivel,L.id_nivel,L.nivel_nombre FROM noticias AS N, niveles as L WHERE N.noticia_id = $_GET[id] AND N.noticia_nivel = L.id_nivel") or die(header ("Location:  $redir?error_login=1"));
 if (
mysql_num_rows($usuario_consulta) == 0)
 {
 die(
"Noticia no encontrada");
 exit;
 }
 
      
$usuario_datos mysql_fetch_array($usuario_consulta);
    
mysql_free_result($usuario_consulta);
    
mysql_close($db_conexion);
?>

<form name="add_news" method="post" action="add_news.php">
<table width="100%"  border="1" cellpadding="0" cellspacing="0" bordercolor="#000000">
        <tr bgcolor="#000000">
          <th colspan="2" scope="col"><div align="left" class="Estilo1 Estilo1">Modificar noticia </div></th>
    </tr>
        <tr>
          <th scope="row"><div align="left" >Titulo </div></th>
          <th scope="row">
            <div align="left">
              <input name="titulo_noticia" type="text"  id="titulo_noticia2" value="<? echo $usuario_datos['noticia_titulo']; ?>">
            </div></th>
        </tr>
        <tr>
          <th scope="row"><div align="left" >Texto de la noticia </div></th>
          <th scope="row"><div align="left">
            <textarea name="texto_noticia" cols="50" rows="20"  id="texto_noticia"><? echo $usuario_datos['noticia_texto']; ?>
</textarea>
          </div></th>
        </tr>
        <tr>
          <th scope="row"><div align="left">Nivel (quien la podr&aacute; ver) </div></th>
          <th scope="row"><div align="left">
        
          
            <select name="nivel_news"  id="nivel_news">
                
                         <?php 
$db_conexion
mysql_connect("$sql_host""$sql_usuario""$sql_pass") or die(header ("Location:  $redir?error_login=0"));
              
mysql_select_db("$sql_db");
$query mysql_query("SELECT L.id_nivel,L.nivel_nombre FROM niveles AS L");
while(
$row=mysql_fetch_array($query)){
$id$row[id_nivel];
$nivel_nombre$row[nivel_nombre];
 
?>
         
             <option value="<?php echo $id ?>"><?php echo $nivel_nombre ?></option>  
                <?php  }
        
mysql_free_result($query); 
mysql_close($db_conexion);
?>
            </select>
          Actual: <? echo $usuario_datos['nivel_nombre']; ?></div></th>
        </tr>
        <tr>
          <th scope="row"><div align="left" >Mostrar</div></th>
          <th scope="row"><div align="left">
         
            <select name="mostrar"  id="select">
                  <option value="1">Si</option>
                  <option value="0">No</option>
            </select>
        
         
          
           Actual: <?  if($usuario_datos['noticia_mostrar'] == 0)
           {
           
printf("NO");
           }else{
           
printf("SI");
           }
          
           
?></div></th>
        </tr>
  </table>     
      <input name="id" type="hidden" value="<? echo $_GET[id]; ?>">
      <input name="Submit" type="submit"  value="Enviar"></form>
<?

}else{

$usuario $_SESSION['usuario_id'];
$texto_noticia $_POST['texto_noticia'];
$titulo_noticia $_POST['titulo_noticia'];
$nivel $_POST[nivel_news];
$mostrar $_POST[mostrar];
$id $_POST[id];
if(empty(
$texto_noticia) || empty($titulo_noticia))
{
die(
"Debe rellenar todos los campos");
exit;
}else {

$db_conexionmysql_connect("$sql_host""$sql_usuario""$sql_pass") or die(header ("Location:  $redir?error_login=0"));
              
mysql_select_db("$sql_db");
//$query = "INSERT INTO noticias ( `noticia_id` , `noticia_usuario` , `noticia_texto` , `noticia_fecha` , `noticia_titulo` , `noticia_nivel` , `noticia_mostrar` ) VALUES ('', $usuario, '$texto_noticia', NOW( ) , '$titulo_noticia', $nivel, $mostrar)";
$sql "UPDATE noticias SET noticia_texto = '$texto_noticia', noticia_titulo = '$titulo_noticia', noticia_nivel = '$nivel', `noticia_mostrar` = '$mostrar' WHERE noticia_id = $id LIMIT 1"

 if(@!
mysql_query($sql))
{
die(
"Error guardando datos");
exit;
}else{
printf("Noticia modificada");
 
/* if($mostrar == 0)
    {
           printf("<br><b>La noticia fue modificada.</b>");
           }*/
}
mysql_close($db_conexion);

}


}

?>

Probablemente aqui esta el error, pero si copio el query en phpmyadmin funciona bien... las variableas tambien las pasa bien.. que sera?

Código PHP:
db_conexionmysql_connect("$sql_host""$sql_usuario""$sql_pass") or die(header ("Location:  $redir?error_login=0"));
              
mysql_select_db("$sql_db");
//$query = "INSERT INTO noticias ( `noticia_id` , `noticia_usuario` , `noticia_texto` , `noticia_fecha` , `noticia_titulo` , `noticia_nivel` , `noticia_mostrar` ) VALUES ('', $usuario, '$texto_noticia', NOW( ) , '$titulo_noticia', $nivel, $mostrar)";
$sql "UPDATE noticias SET noticia_texto = '$texto_noticia', noticia_titulo = '$titulo_noticia', noticia_nivel = '$nivel', `noticia_mostrar` = '$mostrar' WHERE noticia_id = $id LIMIT 1"

 if(@!
mysql_query($sql))

Agradeceria me dijiesen en que choco.

Saludos
  #2 (permalink)  
Antiguo 13/06/2005, 20:20
Avatar de deet  
Fecha de Ingreso: mayo-2005
Ubicación: Córdoba - Argentina
Mensajes: 269
Antigüedad: 18 años, 11 meses
Puntos: 1
Cita:
Iniciado por NRLABS
db_conexion= mysql_connect("$sql_host", "$sql_usuario", "$sql_pass") or die(header ("Location: $redir?error_login=0"));
mysql_select_db("$sql_db");
//$query = "INSERT INTO noticias ( `noticia_id` , `noticia_usuario` , `noticia_texto` , `noticia_fecha` , `noticia_titulo` , `noticia_nivel` , `noticia_mostrar` ) VALUES ('', $usuario, '$texto_noticia', NOW( ) , '$titulo_noticia', $nivel, $mostrar)";
$sql = "UPDATE noticias SET noticia_texto = '$texto_noticia', noticia_titulo = '$titulo_noticia', noticia_nivel = '$nivel', `noticia_mostrar` = '$mostrar' WHERE noticia_id = $id LIMIT 1";

if(@!mysql_query($sql))
{
por que en la consulta update terminas con LIMIT 1?
por lo poco que conozco de consultas creo que esta parte esta mal.

saludos, DEET
  #3 (permalink)  
Antiguo 13/06/2005, 20:24
 
Fecha de Ingreso: febrero-2002
Ubicación: Chile
Mensajes: 1.573
Antigüedad: 22 años, 2 meses
Puntos: 2
Agradezco tu respuesta,

Pero te contare que llevo cerca de 8 horas trabajando y estoy cansado, el error es MUY simple


<form name="add_news" method="post" action="add_news.php">


deberia ser

<form name="edit_news" method="post" action="edit_news.php">

Disculpen por no fijarme bien,

Un saludo

PD: el Limit 1 lo puso PHPMyadmin
  #4 (permalink)  
Antiguo 14/06/2005, 18:42
O_O
 
Fecha de Ingreso: enero-2002
Ubicación: Santiago - Chile
Mensajes: 34.417
Antigüedad: 22 años, 3 meses
Puntos: 129
Cita:
PD: el Limit 1 lo puso PHPMyadmin
mm pues quitaselo .. si tu haces una consulta SQL del tipo "WHERE tal=$tal" suponiendo que esos "tal" (tu id) es único (no se repite en tus tablas .. pues será un campo autonumético) .. no tiene sentido "limitar" a 1 lo que se haga ..pues siempre va a ser 1 el registro afectado.

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 02:10.