How can you get a user's id?

0

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.

    
asked by Spartan456 20.10.2017 в 06:35
source

1 answer

0

In this line of code

VALUES(null,'".$titulo."','".$fecha."','".$descripcion."','".$resul."')";

Replace it with

VALUES("'.$id."',".$titulo."','".$fecha."','".$descripcion."','".$resul."')";

Foreign keys must not have a value in null.

    
answered by 20.10.2017 в 07:22