Hi can someone please help me. I have been struggling with this issue for a few days now, I've read lots of pages but all seem to say different things and are not direct to my question. I am fairly new at using PHP and MySQL and I apologise if my questions are stupid and the answers obvious, or if this is posted elsewhere – I have tried my best to find an answer to my problem.
I am using PHP sessions for a 3 page form and want to insert the form data into my database. My sessions for the form pages are working in printing the results, however, only the last page's data is being entered into the database. I find no errors except when trying to use mysql_real_escape_string, which will be my next step/challenge – but I want to ensure I have the form working with the database properly first, so that I can understand where my errors are coming from without being overwhelmed.
Here is my code:
form1.php
<form method="post" action="form2.php">
<p>Name:
<input type="text" name="name">
</p>
<p>Email address
<input type="text" name="emailaddress">
<input type="submit" value="Go To Step 2">
</p>
</form>
form2.php
<?php
//start the session
session_start();
?>
<?php
// defining variables
$_SESSION['name'] = $name;
$_SESSION['emailaddress'] = $emailaddress;
//store posted values in the session variables
$_SESSION['name'] = $_POST['name'];
$_SESSION['emailaddress'] = $_POST['emailaddress'];
?>
<form method="post" action="form3.php">
<p>Male
<input type="radio" name="gender" value="Male">
Female
<input type="radio" name="gender" value="Female">
</p>
<p>Age:
<input type="text" name="age">
</p>
<p>Location:
<input type="text" name="location">
<input type="submit" value="Go To Step 3">
</p>
</form>
form3.php
<?php
//start the session
session_start();
?>
<?php
// defining variables
$_SESSION['gender'] = $gender;
$_SESSION['age'] = $age;
$_SESSION['location'] = $location;
//store posted values in the session variables
$_SESSION['gender'] = $_POST['gender'];
$_SESSION['age'] = $_POST['age'];
$_SESSION['location'] = $_POST['location'];
?>
<form method="post" action="pageprocess.php">
<p>Employment status:
<input type="text" name="employmentstatus">
</p>
<p>Hobbies:
<input type="text" name="hobbies">
<input type="submit" value="Finish">
</p>
</form>
pageprocess.php
<?php
session_start();
$userid = $_SESSION['userid'];
$name = $_SESSION['name'];
$emailaddress = $_SESSION['emailaddress'];
$gender = $_SESSION['gender'];
$age = $_SESSION['age'];
$location = $_SESSION['location'] ;
$employment_status = $_SESSION['employmentstatus'];
$hobbies = $_SESSION['hobbies'];
?>
<?php
$con=mysqli_connect("localhost","username","password","databasename");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO userinfo (
userid,
name,
emailaddress,
gender,
age,
location,
employmentstatus,
hobbies
)
VALUES
( '" . $_POST[userid] . "',
'" . $_POST[name] . "',
'" . $_POST[emailaddress] . "',
'" . $_POST[gender] . "',
'" . $_POST[age] . "',
'" . $_POST[location] . "',
'" . $_POST[employmentstatus] . "',
'" . $_POST[hobbies] . "'
)";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
Any input is greatly appreciated – there is a lot of conflicting information out there and I need some clarity. Thank you.
In the values they need to be the variables you have set from the $_SESSION, not the $_POST
"VALUES
( '" . $userid . "',
'" . $name . "',
'" . $emailaddress . "',
'" . $gender . "',
'" . $age . "',
'" . $location . "',
'" . $employment_status . "',
'" . $hobbies . "'
)";
Also why are you defining the session variables? You don't need to do that. :)
one thing i do is the following:
foreach ($_POST as $key => $value)
{
$_SESSION[$key] = $value;
}
this will take each post and assign it automatically instead of you defining whats already defined.
this part is problematic:
// defining variables
$_SESSION['name'] = $name;
$_SESSION['emailaddress'] = $emailaddress;
those variables are empty. and then next lines you set them...
you never set the employmentstatus variable in the session array anywhere.
on pageprocess.php your values are POST values, those are empty. you should be using the session variables you set earlier. you also need to look into prevent mysql injection.