473,770 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

php sessions and db insert

22 New Member
Ok it seems there was some problems last time I posted (sorry about that guys) so Ill give it another shot. I am creating a form which is 3 pages long (step1.php, step2.php, and step3.php) then after all the forms are filled out I need to display all the data on a "final.php" page so the visitor can double check their input for errors and if any errors, beable to correct them (via back button) and if not, submit the forms and enter the data into the db. With that said here is some snippets of my forms...


This is "step1.php"
[PHP]<form name="step1" method="post" action="basic2. php">
<?php include ('db_scripts/db_connect_inse rt.php'); ?>
<table border="0" cellpadding="2" cellspacing="0" width="100%">
<tbody><tr>
<td colspan="2"><b> Please enter a valid e-mail address. You will need to confirm your e-mail address to activate your account.</b>
</td>
...
<td class="required ">Company Name</td>
<td class="required "><input type="text" name="comp_name " size="20" maxlength="15" /></td>
...
<td class="required ">Establish ment Type</td>
<td class="required ">
<select name="comp_type ">
<option selected>- Select One -</option>
<option value="bars">Ba r</option>
<option value="clubs">C lub</option>
<option value="coffee_s hops">Coffee Shop</option>
<option value="resturan ts">Resturant </option>
</select>
</td>
...
<td class="required ">Address</td>
<td class="required "><input type="text" name="comp_addr ess" size="30" maxlength="30" /></td>
...
<td class="required ">Phone Number</td>
<td><input type="text" name="comp_phon e" size="20" maxlength="40" /></td>
...
<td class="required ">Email Address</td>
<td><input type="text" name="comp_emai l" size="30" maxlength="20" /></td>
...
<td class="required ">Website Address</td>
<td><input type="text" name="comp_web" size="30" maxlength="20" /></td>
...
<td class="required ">Descripti on</td>
<td><textarea name="comp_desc " cols="30" rows="12"></textarea></td>
...
<td class="verdana1 1" align="right" width="125"><p> </td>
<td><input onClick="basic2 .php" type="submit" name="submit" value="Go to step 2" /></td>
</tbody>
</table>

</form>[/PHP]

and here is "step2.php"
[PHP]<div align="left"><h 2><font color="#b80101" >Types of Cuisine</font></h2></div>
<fieldset align="center">
<table>
<tr>
<td><input type="checkbox" id="foods[]" name="afghan"/></td><td>Afghan</td>
<td><input type="checkbox" id="foods[]" name="caribbean "/></td><td>Caribbea n</td>
<td><input type="checkbox" id="foods[]" name="greek"/></td><td>Greek</td>
<td><input type="checkbox" id="foods[]" name="korean"/></td><td>Korean</td>
<td><input type="checkbox" id="foods[]" name="pizza"/></td><td>Pizza</td>
<td><input type="checkbox" id="foods[]" name="swedish"/></td><td>Swedish</td>

</tr>

<tr>
<td><input type="checkbox" id="foods[]" name="american"/></td><td>American </td>
<td><input type="checkbox" id="foods[]" name="chinese"/></td><td>Chinese</td>
<td><input type="checkbox" id="foods[]" name="indian"/></td><td>Indian</td>
<td><input type="checkbox" id="foods[]" name="lebanese"/></td><td>Lebanese </td>
<td><input type="checkbox" id="foods[]" name="peruvian"/></td><td>Peruvian </td>
<td><input type="checkbox" id="foods[]" name="sweets"/></td><td>Sweets</td>

</tr>

<tr>
<td><input type="checkbox" id="foods[]" name="appetizer s"/></td><td>Appetize rs</td>
<td><input type="checkbox" id="foods[]" name="cuban"/></td><td>Cuban</td>
<td><input type="checkbox" id="foods[]" name="irish"/></td><td>Irish</td>
<td><input type="checkbox" id="foods[]" name="mediterra nean"/></td><td>Mediterr anean</td>
<td><input type="checkbox" id="foods[]" name="portugues e"/></td><td>Portugue se</td>
<td><input type="checkbox" id="foods[]" name="thai"/></td><td>Thai</td>

</tr>

<tr>
<td><input type="checkbox" id="foods[]" name="brazilian "/></td><td>Brazilia n</td>
<td><input type="checkbox" id="foods[]" name="french"/></td><td>French</td>
<td><input type="checkbox" id="foods[]" name="italian"/></td><td>Italian</td>
<td><input type="checkbox" id="foods[]" name="mexican"/></td><td>Mexican</td>
<td><input type="checkbox" id="foods[]" name="southwest ern"/></td><td>South Western</td>
<td><input type="checkbox" id="foods[]" name="turkish"/></td><td>Turkish</td>

</tr>

<tr>
<td><input type="checkbox" id="foods[]" name="cajun&cre ole"/></td><td>Cajun & Creole</td>
<td><input type="checkbox" id="foods[]" name="german"/></td><td>German</td>
<td><input type="checkbox" id="foods[]" name="japanese"/></td><td>Japanese </td>
<td><input type="checkbox" id="foods[]" name="middle_ea stern"/></td><td>Middle Eastern</td>
<td><input type="checkbox" id="foods[]" name="spanish"/></td><td>Spanish</td>
<td><input type="checkbox" id="foods[]" name="vietnames e"/></td><td>Vietname se</td>

</tr>
</table>
</fieldset>
<input onClick="basic3 .php" type="submit" name="submit" value="Go to step 3" />[/PHP]

Now last time I posted this I also added the third form but there was to much code posted here so the post had to get deleted. If this has to get deleted ill take an attempt to make the in here much shorter and simplified. Anyway what I am looking for cuz im such a noob, is to set these forms up with sessions and to do the db insert query when needed as described in the first paragraph. Thank you all so much in advance for your help.
Nov 28 '07 #1
35 3101
pbmods
5,821 Recognized Expert Expert
Heya, Crazy8. Welcome (back) to TSDN!

What you'll want to do here is create an array in your session and then copy $_POST values to it. At the end of step 3, you can then execute the query.

Expand|Select|Wrap|Line Numbers
  1. foreach( $_POST as $__key => $__val )
  2. {
  3.     $_SESSION['form'][$__key] = $__val;
  4. }
  5.  
The above code will copy every value in $_POST into $_SESSION['form']. Use this when processing the User's input.
Nov 28 '07 #2
crazy8
22 New Member
So then, to make sure I understand correctly I just have a few questions?

1) All I do is put that code at the top of each form and thats it?
2) I still need to do session_start() at the top, correct?
3) To echo this onto page 4 so the visitor can see everything they typed all I need to do in my script is echo $_SESSION['form'][$_key] ?
4) would I need to do anything special as far as my "back" buttons go with allof this?

thanks for the help I greatly appreciate it.
Nov 28 '07 #3
pbmods
5,821 Recognized Expert Expert
Heya, Crazy8.

1) All I do is put that code at the top of each form and thats it?
More or less. You'd only need to put this at the top of any page that processes User input. In other words, if you had page1.php, page2.php, page3.php and page1process.ph p, page2process.ph p and page3process.ph p, you'd only need to put this code at the top of page1process.ph p, page2process.ph p and page3process.ph p.
2) I still need to do session_start() at the top, correct?
Correct.
3) To echo this onto page 4 so the visitor can see everything they typed all I need to do in my script is echo $_SESSION['form'][$_key] ?
More or less. You'd just substitute $_key with the input's name. For example:
Expand|Select|Wrap|Line Numbers
  1. <input name="name" type="text" value="<?php if( isset($_SESSION['form']['name']) ) echo $_SESSION['form']['name']; ?>" />
  2.  
4) would I need to do anything special as far as my "back" buttons go with allof this?
Not specifically. The session would be updated only when the User submits a form, so he can go back/forward as much as he likes, and no changes will be made until he submits something.
Nov 28 '07 #4
crazy8
22 New Member
if you had page1.php, page2.php, page3.php and page1process.ph p, page2process.ph p and page3process.ph p, you'd only need to put this code at the top of page1process.ph p, page2process.ph p and page3process.ph p.
I dont actually have any scripts that process the form(s). Do I need anything else other then the session stuff and the inserting of the data into the db?

How do I take the session and insert the data into my db on the final submition?

So if I have this right (sorry for my noobiness) what I need to do is this...

[PHP]session_start()

foreach( $_POST as $__key => $__val )
{
$_SESSION['form'][$__key] = $__val;
}
<form name="step1" method="post" action="basic2. php">
<table border="0" cellpadding="2" cellspacing="0" width="100%">
<tbody><tr>
<td colspan="2"><b> Please enter a valid e-mail address. You will need to confirm your e-mail address to activate your account.</b>
</td>
</tr>
<tr>
<td class="required ">Company Name</td>
<td class="required "><input type="text" name="comp_name " size="20" maxlength="15" /></td>
</tr>
<tr>
<td class="required ">Establish ment Type</td>
<td class="required ">
<select name="comp_type ">
<option selected>- Select One -</option>
<option value="bars">Ba r</option>
<option value="clubs">C lub</option>
<option value="coffee_s hops">Coffee Shop</option>
<option value="resturan ts">Resturant </option>
</select>
</td>
</tr>
<tr>
<td class="required ">Address</td>
<td class="required "><input type="text" name="comp_addr ess" size="30" maxlength="30" /></td>
</tr>
<tr>
<td class="required ">Phone Number</td>
<td><input type="text" name="comp_phon e" size="20" maxlength="40" /></td>
</tr>
<tr>
<td class="required ">Email Address</td>
<td><input type="text" name="comp_emai l" size="30" maxlength="20" /></td>
</tr>
<tr>
<td class="required ">Website Address</td>
<td><input type="text" name="comp_web" size="30" maxlength="20" /></td>
</tr>
<tr>
<td class="required ">Descripti on</td>
<td><textarea name="comp_desc " cols="30" rows="12"></textarea></td>
</tr>
<tr>
<td class="verdana1 1" align="right" width="125"><p> </td>
<td><input onClick="basic2 .php" type="submit" name="submit" value="Go to step 2" /></td>
</tr>
</tbody>
</table>

</form>[/PHP]

Notice I added the session_start() and the snipet to the form code, I did that IF I dont need some "processing"scr ipt for my forms. If thats the case then I just add the sessio_start() and the little snipet to the tops of each of my forms correct?

Thanks alot, again, for your help.
Nov 29 '07 #5
pbmods
5,821 Recognized Expert Expert
Heya, Crazy8.

That should do it.

Once the User submits step 3, you can then pull the saved values from $_SESSION['form'] and build your SQL query.
Nov 29 '07 #6
crazy8
22 New Member
One other question I had is how would I account for check boxes if I wanted those to be in the session to? Or does this just grab everything?

Also maybe I am puttin my stuff in the wrong area im not sure but here is what I have done as of now...
[PHP]<?php
//session_start()
foreach( $_POST as $__key => $__val )
{
$_SESSION['form'][$__key] = $__val;
}
?>
<form name="step1" method="post" action="basic2. php">
...
...
...[/PHP]

Now I know I need the session_start() uncommented but for some reasn I was getting an error with it on and everything seems to be fine when its off, so I have that bit of code there pasted above my 3 forms. So now I made a 4th page to echo out everything and I cant get that to work. Here is what I have for that...
[PHP]<?php
<input name="name" type="text" value="<?php if( isset($_SESSION['form']['comp_name']) ) echo $_SESSION['form']['comp_name']; ?>" />
<input name="name" type="text" value="<?php if( isset($_SESSION['form']['comp_type']) ) echo $_SESSION['form']['comp_type']; ?>" />
<input name="name" type="text" value="<?php if( isset($_SESSION['form']['comp_address']) ) echo $_SESSION['form']['comp_address']; ?>" />
<input name="name" type="text" value="<?php if( isset($_SESSION['form']['comp_phone']) ) echo $_SESSION['form']['comp_phone']; ?>" />
<input name="name" type="text" value="<?php if( isset($_SESSION['form']['comp_email']) ) echo $_SESSION['form']['comp_email']; ?>" />
<input name="name" type="text" value="<?php if( isset($_SESSION['form']['comp_web']) ) echo $_SESSION['form']['comp_web']; ?>" />
<textarea name="comp_desc " value="<?php if( isset($_SESSION['form']['comp_desc']) ) echo $_SESSION['form']['comp_desc']; ?>"> </textarea>
?>[/PHP]

And I get this error...
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\xampp\htdocs \viewable\hsrev ised\live\step4 .php on line 2
Now if I am doing it right, I still get the error even if I write out each line like this...
echo '<input name="name" type="text" value="<?php if( isset($_SESSION['form']['comp_name']) ) echo $_SESSION['form']['comp_name']; ?>" />';
Any thoughts as to what I am doing wrong?

Thanks again
Nov 29 '07 #7
pbmods
5,821 Recognized Expert Expert
Heya, Crazy8.

You're missing a semicolon after session_start() .
Nov 29 '07 #8
crazy8
22 New Member
ok well i got that now (I do know about the semi colon, just must have missed it) but after fixing it I got an error so I thought maybe I had the code in the wrong spot so I moved it into the <head></head> area on page1 and everything is fine but then when I hit submit and goto page2 I get the error again which is this...
Warning: session_start() [function.sessio n-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs \viewable\hsrev ised\live\basic 2.php:5) in C:\xampp\htdocs \viewable\hsrev ised\live\basic 2.php on line 6

Warning: session_start() [function.sessio n-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs \viewable\hsrev ised\live\basic 2.php:5) in C:\xampp\htdocs \viewable\hsrev ised\live\basic 2.php on line 6
Now I am supposed to have this posted on all 3 forms right?
[PHP]<?php
session_start() ;
foreach( $_POST as $__key => $__val )
{
$_SESSION['form'][$__key] = $__val;
}
?>[/PHP]
Nov 29 '07 #9
pbmods
5,821 Recognized Expert Expert
Heya, Crazy8.

session_start() requires a couple of HTTP headers, which can only be sent before your script outputs anything.

Make sure you call session_start() at the very start of your script, before it outputs anything, including whitespace.
Nov 29 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

11
2047
by: Ken | last post by:
This does not work because of the need for 's. Is there a way to insert a $_SESSION variable into a database? Thanks. mysql_query("INSERT INTO master (id) Values('$_SESSION' ) " ) ; Ken
0
6029
by: Will Seay | last post by:
At the end of this message I've pasted a script we're trying to modify slightly. I don't believe it is VBscript or javascript but these are the closest groups I could find with my limited programming knowledge. Basically, we are trying to add a few lines to this script that will execute a few shell commands (see comments at the very end of the code). We think this may be ActionScript2 but aren't sure. If you can offer any help, or know...
3
2480
by: Maxime Ducharme | last post by:
Hi group We have a problem with sessions in one of our sites. Sessions are used to store login info & some other infos (no objects are stored in sessions). We are using Windows 2000 Server (IIS 5.0) with ASP 3.0 (no .NET on this site). Sometime, data in session is emptied. I say "sometime"
5
4011
by: Eugene | last post by:
I have a table EugeneTest(id_num, fname, minit, lname) where field "id_num" is type IDENTITY and has UNIQUE constraint Let's say 2 user execute this code at the same time: DECLARE @return integer use EugeneTest INSERT employees ( fname, minit, lname) VALUES
5
673
by: Rob | last post by:
I have an ASP.NET application that uses forms-based authentication. A user wishes to be able to run multiple sessions of this application simultaneously from the user's client machine. The web.config file is configured as such: <authentication mode="Forms"> <forms loginUrl="Login.aspx" protection="All" name="myApplication"/> </authentication>
3
1420
by: Miguel | last post by:
Hi all friends: It's said that Sessions objects in ASP 3.0 with IIS 5.0 occupy certain memory of the machine which take to take care about use a lot of Sessions objects in the ASPs pages of the applications. So my question is about how recommendable is to use a lot of Sessions objects in ASP.Net I want to use in my application 30 of this objects. Im gonna use them for give permissions to the users of the application. Regards.
6
3807
by: Daniel Walzenbach | last post by:
Hi, I have a web application which sometimes throws an “out of memory” exception. To get an idea what happens I traced some values using performance monitor and got the following values (for one day): \\FFDS24\ASP.NET Applications(_LM_W3SVC_1_Root_ATV2004)\Errors During Execution: 7 \\FFDS24\ASP.NET Apps v1.1.4322(_LM_W3SVC_1_Root_ATV2004)\Compilations
3
28323
Atli
by: Atli | last post by:
Introduction: Sessions are one of the simplest and more powerful tools in a web developers arsenal. This tool is invaluable in dynamic web page development and it is one of those things every developer needs to know how to use. This article explains the basics of PHP Sessions. Assumptions: Basic PHP knowledge is required (variables, arrays and such) HTML Forms. What are Sessions? Sessions are a way of storing data. When developing...
4
1724
by: DavidPr | last post by:
I start sessions on all pages with:ob_start(); session_start(); at the top of the page before anything else. When I login these sessions are set:$query = "SELECT * FROM users WHERE (email='$e' AND pass=SHA('$p')) AND active IS NULL"; $result = mysql_query ($query); if (@mysql_num_rows($result) == 1) { $row = mysql_fetch_array ($result, MYSQL_NUM);
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.