/    Sign up×
Community /Pin to ProfileBookmark

Insert Form data into MySQL db using sessions

Hello all, hope you are well.

I have a form split over 4 pages. I want to pass the data from each page to a session and then insert it into the database.

I am not sure what I’ve done wrong but fields from the last page get put into the first few fields of the database instead of where they should be, and the rest are blank.

Here’s the codes:

Top of page 2 before any html code

[code=php]session_start();

$_SESSION[‘name’] = $_POST[‘os0’];
$_SESSION[‘number’] = $_POST[‘os1′];
$_SESSION[’email’] = $_POST[‘os2’];
[/code]

Page 3

[code=php]session_start();

$_SESSION[‘collection_date’] = $_POST[‘os4’];
$_SESSION[‘address’] = $_POST[‘address’];
[/code]

Page 4

[code=php]session_start();

$_SESSION[‘packkit’] = $_POST[‘os5’];
$_SESSION[‘delivery’] = $_POST[‘on6’];
$_SESSION[‘additems’] = $_POST[‘os7’];
[/code]

php to insert into database

[code=php]<?php
session_start();

$con = mysql_connect(“XXXX”,”XXXXX”,”XXXXX”);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}

mysql_select_db(“XXXXX”, $con);

$number = mysql_real_escape_string(preg_replace(‘/[^0-9]/’, ”, $_SESSION[‘number’]));

$realname = mysql_real_escape_string($_SESSION[‘name’]);

$email = mysql_real_escape_string($_SESSION[’email’]);

$address = mysql_real_escape_string(preg_replace(‘/[rn]/’,’ ‘,$_SESSION[‘address’]));

$collection = mysql_real_escape_string($_SESSION[‘collection_date’]);

$packingkit = mysql_real_escape_string($_SESSION[‘packkit’]);

$delivery = mysql_real_escape_string($_SESSION[‘delivery’]);

$additems = mysql_real_escape_string($_SESSION[‘additems’]);

$tandc = mysql_real_escape_string($_POST[‘tandc’]);

$sql=”INSERT INTO Orders (Name, Number, Email, Size, Address, Collection_Date, Packing_kit, Delivery_date, Additional_Items, Date_received, Terms, Deposit)
VALUES
(‘$realname’,’$number’,’$email’,’Standard’,’$address’,’$collection’,’$packingkit’,’$delivery’,’$additems’, date_format(curdate(),’%d-%m-%Y’),’$tandc’, ‘Yes’)”;

if (!mysql_query($sql,$con))
{
die(‘Error: ‘ . mysql_error());
}
echo “”;

mysql_close($con)
?>

[/code]

Can anyone see what I’m doing wrong here?

Thanks in advance for any input, it is much appreciated.

Kind regards, Mike

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@kurbyMar 29.2011 — Nothing in the code that you provided looks wrong. Perhaps it could be in form construction that you didn't provide.

Have you checked the $_SESSION status at each stage? print_r($_SESSION); at the top of each page to see if it looks as it should.
Copy linkTweet thisAlerts:
@sss_1234authorMar 29.2011 — Hello Kurby, thank you for your swift response.

I am fairly sure my form code is ok as originally I had just one page with the whole form on it. I have used the same code but simply split across the 4 pages. I'll check it though and post it it up just to make sure.

I like your suggestion to test at each stage, I'll give that a go and post back.

Thanks again and best regards, Mike
Copy linkTweet thisAlerts:
@sss_1234authorMar 29.2011 — Hello again,

I cracked it (with your help!)

Printing the session after each stage showed me that it was breaking down after the second page.

You were right that my form code needed checking! The action= bit was set to a test php file I made earlier! I didn't notice because this php file still linked the third page so everything seemed to be ok!

Thanks very much for your help.

Kind regards, Mike
×

Success!

Help @sss_1234 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...