Ver Mensaje Individual
  #2 (permalink)  
Antiguo 14/09/2007, 09:07
Avatar de gerson
gerson
 
Fecha de Ingreso: febrero-2005
Ubicación: Lima
Mensajes: 481
Antigüedad: 19 años, 2 meses
Puntos: 4
Re: Problemas al insertar registros con PHP a MySql

Hola embedding pues para obtener el ultimo id insertado utiliza mysql_insert_id(). valga la aclaracion q es aplicable para el campo de tipo auto_increment
Código PHP:
<?php
// INSERTAR UNA NOTICIA EN LA BASE DE DATOS
include('../conexion.php');
    
    
$link conectarse();
    
$_POST['data']=date("Y-m-d");
    
$_POST['hora']=date("H:i:s");
        
    
// Con esta sentencia SQL insertaremos la noticia en la base de datos
    
$sentencia="INSERT INTO tabla_noticias (titol,noticiaBreu,noticiaCompleta,codi_classificacio,link_arxiu,link_foto,data,hora)
VALUES ('{$_POST['titular']}','{$_POST['noticiaBreve']}','{$_POST['noticiaCompleta']}','{$_POST['codigoNoticia']}','{$_POST['archivo']}','{$_POST['foto']}','{$_POST['data']}','{$_POST['hora']}')"
;

    
mysql_query($sentencia,$link);
    
$Max_id_noticia mysql_insert_id();
    
$my_error mysql_error($link);
    
    if(!empty(
$_POST['foto'])) //insertara la imagen en la tabla_fotos si esta existe
        
{
            
$sentencia2="INSERT INTO tabla_fotos (link,codi_classificacio) VALUES ('{$_POST['foto']}','{$_POST['codigoNoticia']}')"//guardo la foto en tabla_fotos
    
            
mysql_query($sentencia2,$link);
            
$Max_id_foto mysql_insert_id();
            
$my_error2 mysql_error($link);
            
            
$SQL_insert_aparecer "INSERT INTO tabla_aparecer VALUES ('$Max_id_foto', '$Max_id_noticia')";
            
            
mysql_query($SQL_insert_aparecer,$link);
            
$my_error5 mysql_error($link);
        }
    
    if(!empty(
$my_error) || !empty($my_error2) || !empty($my_error3) || !empty($my_error4) || !empty($my_error5))
        { 
            echo 
"Hi ha hagut un error a l'inserir els valors: $my_error -- $my_error2 -- $my_error3 -- $my_error4 -- $my_error5"
        }
    else
        {
            echo 
"<body style=\"background-color:#FFFFCC; margin-top:60px; margin-left:300px;\"><p>La notícia s'ha introduït correctament</p><a href=\"../administrador_web/admin_menu.php\" >PANELL D'ADMINISTRACIÓ</a></body>";
        }
?>
Saludos
__________________