-2

so as the title says, I'm looking to include a Session Variable into a MySQL Update statement. I'm not exactly sure what I'm doing wrong, but I certainly know I am. I'm also fairly new to this, so that may explain it lol! I've looked around a lot for solutions, and I know the solution is very simple, but I just can't find it. Thanks in advance for your help.

Code in view.php:

$email = $conn->query("SELECT email FROM memberapplications WHERE ID = " . $_GET["appid"]);
$_SESSION["email"] = $email;

Code in redirect.php:

//There are also connections and stuff here, but didn't see a need to include it.
$sql = "UPDATE users SET appstatus='2' WHERE email='$email'";
  • (Possible) side note: Do not use string interpolation or concatenation to get values into SQL queries. That's error prone and might make your program vulnerable to SQL injection attacks. Use parameterized queries. See ["How to include a PHP variable inside a MySQL statement"](https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement) and ["How can I prevent SQL injection in PHP?"](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – sticky bit Jul 09 '21 at 02:28
  • Are you calling `session_start()` at the top of both PHP files? Where is `$email` initialised in `redirect.php`? – Tangentially Perpendicular Jul 09 '21 at 03:18
  • @TangentiallyPerpendicular Yeah, both are called by session_start(), and $email is initialised at around line 100, within bounds – Popcornography. Jul 09 '21 at 03:24
  • @popcorn I don't see how we can help, then, with no error message and only two lines of code. – Tangentially Perpendicular Jul 09 '21 at 07:22

1 Answers1

0

You need to put session variable in update query instead of email variable, you are assigning email to session variable but not using it

$sql = "UPDATE users SET appstatus='2' WHERE email='".$_SESSION["email"]."'";

If you have started session in both files and assign email to session variable correctly then it should work correctly

Virender Kumar
  • 182
  • 1
  • 8