Foros del Web » Programando para Internet » PHP »

Problemas al insertar registros con PHP a MySql

Estas en el tema de Problemas al insertar registros con PHP a MySql en el foro de PHP en Foros del Web. Lo que deseo hacer es lo siguiente: tengo un formulario donde inserto una noticia en mi web, guardando los datos en la tabla_noticias, a su ...
  #1 (permalink)  
Antiguo 13/09/2007, 04:56
 
Fecha de Ingreso: febrero-2007
Mensajes: 2
Antigüedad: 17 años, 1 mes
Puntos: 0
Problemas al insertar registros con PHP a MySql

Lo que deseo hacer es lo siguiente:
tengo un formulario donde inserto una noticia en mi web, guardando los datos en la tabla_noticias, a su vez tengo relacionada la tabla_noticias con otra llamada tabla_aparecer, donde guardaré la id_noticia y la id_foto (asi tendre cada noticia relacionada con su correspondiente foto), mi problema es el siguiente, no me inserta bien la id_noticia y la id_foto en la tabla_aparecer y no se lo que estoy haciendo mal.... dejo aqui mi código por si alguien puede verer que es lo que hago mal:


Código:
<?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);
	$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
			
			$SQL_id_foto= "SELECT MAX(id_foto) FROM tabla_fotos"; //saco la id_foto de tabla_fotos
			$SQL_id_noticia= "SELECT MAX(id_noticia) FROM tabla_noticias";//saco la id_noticia de tabla_noticias
	
			mysql_query($sentencia2,$link);
			$my_error2 = mysql_error($link);
			
			$Max_id_foto = mysql_query($SQL_id_foto,$link);
			$my_error3 = mysql_error($link);
			
			$Max_id_noticia = mysql_query($SQL_id_noticia,$link);
			$my_error4 = 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>";
		}
?>
  #2 (permalink)  
Antiguo 14/09/2007, 09:07
Avatar de 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
__________________

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 03:09.