-1

This is my first time trying to work with php, I'm trying to store a user input into as session variable and pass it to another page which will then store it in a database. However my passed variables do not show up in the database. Any help is appreciated!

Here's where I get the variable values :

<form action ="csitProjectFunctionalityRegisterComplete.php">First name:<br>
                        <input id="fName" type="text" name="firstname" value="">
                        <br>
                        Last name:<br>
                        <input id="lName" type="text" name="lastname" value="">
                        <br>
                        Email:
                        <br>
                        <input id="email1" type="text" name="email" value="">
                        <br>
                        Username:
                        <br>
                        <input id="username1" type="text" name="username" value="">
                        <br>
                        Password:
                        <br>
                        <input id="password1" type="text" name="password" value="">
                        <br>
                        Re-enter Password:
                        <br>
                        <input id="password2" type="text" name="password" value="">

                        <br>
                        <br>
                        <input type="submit" class="btn btn-default btn-custom1">
                        </form>

Trying to set them as session variables:

$_SESSION['FName'] = $_POST['fName'];
$_SESSION['LName'] = $_POST['lName']; 
$_SESSION['UName'] = $_POST['username1'];
$_SESSION['E_Mail'] = $_POST['email1'];
$_SESSION['PWord1'] = $_POST['password1'];
$_SESSION['PWord2'] = $_POST['password2'];

receiving:

$FNameC = $_SESSION['FName'];
$LNameC = $_SESSION['LName']; 
$UNameC = $_SESSION['UName1'];
$E_MailC = $_SESSION['E_Mail'];
$PWord1C = $_SESSION['PWord1'];
$PWord2C = $_SESSION['PWord2'];
$User_ID = rand(1, 99999);

trying to send to database:

$sql = "INSERT INTO tempUsers (FirstName, LastName, Email, UserName, userID, Password)
VALUES ('$FNameC', '$LNameC', '$E_MailC', '$UNameC', '$User_IDC', '$PWord2C')";

EDIT: Again, I'm new to php, so perhaps I just don't understand it correctly, but this doesn't seem like a duplicate of that question to me. That seems to address checking if the variable is null, whereas I know the variable shouldn't be null, but for some reason between one page and the other it becomes null

Matt
  • 81
  • 2
  • 11

1 Answers1

1

When html forms are submitted it is the name attribute which is used to identify the different fields. So your $_POST array needs to be changed to reference the name attribute rather than the id attribute, e.g.:

$_SESSION['FName'] = $_POST['fName'];

As a tip, you can quickly confirm what is being held by each variable using PHP's var_dump() function, e.g.:

$_SESSION['FName'] = $_POST['fName'];
var_dump($FNameC);
exit();

This should help you confirm if the different variables are being set correctly and identify where any problem is occurring.

Maltronic
  • 1,782
  • 10
  • 21
  • Changed to select the name instead of id, and used var_dump on the variables, all but User_ID returned as null, any ideas? – Matt Nov 10 '15 at 03:21
  • @Matt, apologies. I referenced the wrong line of your code. I have now updated my answer. It is the $_POST array that you need to change, not the $_SESSION array. – Maltronic Nov 10 '15 at 03:23
  • Hmmm.. I changed it to `$_SESSION['FName'] = $_POST['firstname']; $_SESSION['LName'] = $_POST['lastname']; $_SESSION['UName'] = $_POST['username']; $_SESSION['E_Mail'] = $_POST['email']; $_SESSION['PWord1'] = $_POST['password1']; $_SESSION['PWord2'] = $_POST['password2'];` That's what you were referring to right? – Matt Nov 10 '15 at 03:30
  • @Matt, yes. That is correct. – Maltronic Nov 10 '15 at 03:34
  • var_dump is still returning null for those variables – Matt Nov 10 '15 at 03:47
  • @Matt, try var_dump($_POST['firstname']); on the first page after submitting the form. This should definitely return the value. – Maltronic Nov 10 '15 at 03:50
  • Oddly, that also returns null, which answers one question at least, I'd assumed that I somehow the value was getting lost between pages. In my address bar I am getting `...csitProjectFunctionalityRegister.php?firstname=JMatt` – Matt Nov 10 '15 at 04:01
  • @Matt, it seems your form is being submitted as a GET request instead of a POST request. You could either fetch the variables using $_GET['firstname'] etc or put 'method="POST"' in your html form tag. – Maltronic Nov 10 '15 at 04:22
  • That seemed really odd to me since I was using post, but you were right, var_dump now shows correctly on the first page. However when attempting to implement for use with the second page, I still receive null values. Is there something else I should be doing to correctly receive the session variables? – Matt Nov 10 '15 at 04:33