简体   繁体   中英

How do I store a $_SESSION value within my database

I've been trying to store the session userUid and put it in the database under id. I want it to merge the two ids from different databases.

I tried setting the userUid as a variable and putting it into the database. userUid is the id from the signup/login page and profiles is the database i'm trying to insert it into.

if(!isset($_SESSION['userUid'])){ 
    header("location: index.php"); // Redirecting To Home Page 

    $switch = $_SESSION['userUid'];
    $Asql = "INSERT INTO profile (id) VALUES ('$switch');" ;
    mysqli_query($conn2, $Asql);
    $resultCheck = mysqli_num_rows($result);
    if ($resultCheck > 0) {
          while ($row = mysqli_fetch_assoc($result)) {
              echo $row['id'] . "<br>";
          }
    }
}

I expect it to out put the newly inserted $_SESSION['userUid'] as "id" from the database profiles.

A few things to note,

  • Your current logic will never really work, because you only execute the query when the session-value is not set
  • You should always exit; after a header("Location:..."); call
  • You should be using prepared statements and bind your values through placeholders
  • An insert query has no num_rows (the equivalent is affected_rows for insert/update/delete queries) or fetch_*() methods associated with them.
if (!isset($_SESSION['userUid'])) { 
    header("location: index.php"); // Redirecting To Home Page 
    exit;
}

$sql = "INSERT INTO profile (id) VALUES (?);";
$stmt = $conn2->prepare($sql);
$stmt->bind_param("s", $_SESSION['userUid']);
$stmt->execute();
echo $stmt->affected_rows." rows inserted";
$stmt->close();

In order to properly check for errors, you should enable MySQLi exception mode by adding the following line before you create your MySQLi connection. That means that you have to use a try/catch for your query statements, but in turn means that you don't have to do individual error-checking for each function-call.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

You are trying to use mysqli_fetch_assoc on an insert query.
You could either just use $_SESSION['userUid'] or you can make a seperate select query to get the new row from the database.

First check the "if" condition in your code. if(!isset($_SESSION['userUid'])): ... else: ... endif; The final solution to you,

if(!isset($_SESSION['userUid'])):
header("location: index.php"); // Redirecting To Home Page 
else:
$switch = $_SESSION['userUid'];
$Asql = "INSERT INTO profile (id) VALUES ('$switch');" ;
mysqli_query($conn2, $Asql);
$resultCheck = mysqli_num_rows($result);
  if ($resultCheck > 0):
      while ($row = mysqli_fetch_assoc($result))
        {
          echo $row['id'] . "<br>";
       }
   endif;
endif;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM