Shoosha
Shoosha

Reputation: 21

How can i send a session value into sql query in PHP?

When i'm sending a session value into sql query it send a NULL value. When i manually change the value to $userMail="[email protected]" instead of $userMail=$_SESSION['em'] everything works fine. Thank U for helping me :)

<?php
require_once("../php/db.php");

if(!isset($_SESSION['em'])) {
      $userMail = $_SESSION['em'];
  } 

$taskName = $_POST['taskName'];
$taskDate = $_POST['taskDate'];
$writerName = $_POST['writerName'];
$isDone = $_POST['isDone'];

$sql ="INSERT INTO `tasksToDo`( `taskName`, `taskDate`, `writerName` , `isDone`, `userMail`)
VALUES ('$taskName','$taskDate','$writerName','$isDone','$userMail')";
if ($conn->query($sql)===TRUE) {
    $last_id = $conn->insert_id;
    echo json_encode(array('success' => 1,"id"=>$last_id));
}else {
    echo json_encode(array('success' => 0));
}

$conn->close();

?>

Upvotes: 0

Views: 295

Answers (1)

Shoosha
Shoosha

Reputation: 21

Tnx to CBroe and ADyson, the solution is:

<?php session_start();
require_once("../php/db.php");
if(!isset($_SESSION['em'])){ // if the user is not logged in then don't enter the system
    header('Location: logIn.php');
    exit;
}
if(isset($_SESSION['em'])) {
    $userMail =$_SESSION['em'];

} 


$taskName = $_POST['taskName'];
$taskDate = $_POST['taskDate'];
$writerName = $_POST['writerName'];
$isDone = $_POST['isDone'];

$sql ="INSERT INTO `tasksToDo`( `taskName`, `taskDate`, `writerName` , `isDone`, `userMail`)
VALUES ('$taskName','$taskDate','$writerName','$isDone','$userMail')";
if ($conn->query($sql)===TRUE) {
    $last_id = $conn->insert_id;
    echo json_encode(array('success' => 1,"id"=>$last_id));
}else {
    echo json_encode(array('success' => 0));
}

$conn->close();

?>

Upvotes: 1

Related Questions