I have a project in which when the user logs in, a session variable is created with the user field of my form. Now I would like to know how I can obtain the id, that is, the primary key of a user from a session variable, and then insert this value as a foreign key in another table. This is the php code that I have so far:
<?php
include("Conexion.php");
$titulo = $_POST["titulo"];
$fecha = $_POST["fecha"];
$descripcion = $_POST["descripcion"];
session_start();
$id = $_SESSION['Session'];
$sql2 = "SELECT idusuario FROM usuario WHERE usuario='".$id."'";
$resul = mysql_query($sql2) or die ("Error" .mysql_error());
$sql = "INSERT INTO tarea VALUES(null,'".$titulo."','".$fecha."','".$descripcion."','".$resul."')";
$resultado = mysql_query($sql) or die ("Error" .mysql_error());
header("location: ../html/Tareas.php");
?>
But you send me the following error:
ErrorCannot add or update to child row: a foreign key constraint fails ( apptic
. tarea
, CONSTRAINT fk_tarea_usuario
FOREIGN KEY ( usuario_idusuario
) REFERENCES usuario
( idusuario
) ON DELETE NO ACTION ON UPDATE NO ACTION )
I hope you can help me and I thank you.