Jump to content

[SOLVED] Multiple Page Form Submit to Database?


twilitegxa

Recommended Posts

OK, for the sessions, leave page1.php alone.

 

page2.php

<?php
session_start();
$_SESSION['firstname;] = htmlentities($_POST['firstname'],ENT_QUOTES); // you could do the htmlentities thing later if you wanted
$_SESSION['lastname'] = htmlentities($_POST['lastname'],ENT_QUOTES);
?>
<html>
<head>
<title></title>
</head>
<body>
<form action="script.php" method="post">
 <input type="text" name="email" />
 <input type="text" name="url" />
 <input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>

 

script.php

<?php
session_start();
$con = mysql_connect("localhost","root","");
if (!$con) die('Could not connect: ' . mysql_error());
mysql_select_db("smrpg", $con);
$sql="INSERT INTO table VALUES ('{$_SESSION['firstname']}', '{$_SESSION['lastname']}', '{$_POST['email']}', '{$_POST['url']}')";
mysql_query($sql,$con) or die('Error: ' . mysql_error());
echo "Successful!";
mysql_close($con);
?>

 

Try that.

 

As far as your SQL, is your table name really "table?" If so, you need backticks around it: `table` because table is a keyword in SQL.

It is table, I meant to name it test but apparently I was thinking table and typed table. I can change it to test. I think I remember how...

 

But, I got this error. Something's wrong on line 3 apparently:

 

Parse error: syntax error, unexpected T_STRING, expecting ']' in C:\wamp\www\test2\page2.php on line 3

I got it. And now it works. Yay! So, this is the basic thing I need to do on each page. So if there were a page 3, what woudl be different about it? Just this in the php tags with the right field names?

 

$_SESSION['firstname'] = htmlentities($_POST['firstname'],ENT_QUOTES); // you could do the htmlentities thing later if you wanted
$_SESSION['lastname'] = htmlentities($_POST['lastname'],ENT_QUOTES);

 

Is that is? And then on my script page, add these lines with the appropriate field names fromt he previous page within the insert query?:

 

'{$_SESSION['firstname']}','{$_SESSION['firstname']}',

 

It seems so simple! Why couldn't I get this so much easrlier?!? LOL Thanks for all your help!!!

One more question, when I get to the final page that will post to the database, how do I add the code for the additional pages because they will submit their results to different tables. Will that cause any types of problems? I'm assuming I would just basically repeat the same code, underneath the previous code and just change the fields and values of everything. Is that right?

Here is an example of the insert.php page I have so far with all the data from the first page to insert into the database:

 

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("smrpg", $con);$sql="INSERT INTO scout (identity, charactername, elementofinfluence, age, birthdatemonth, birthdateday, birthdateyear, heightfeet, heightinches, bloodtype, hobbies, favoritecolor, favoritegemstone, favoritefood, leastfavoritefood, favoriteschoolsubject, leastfavoriteschoolsubject, strengths, weaknesses, goal, biography)
VALUES ('{$_SESSION['identity']}', '{$_SESSION['charactername']}', '{$_POST['elementofinfluence']}', '{$_POST['age']}'), ('{$_SESSION['birthdatemonth']}', '{$_SESSION['birthdateday']}', '{$_POST['birthdateyear']}', '{$_POST['heightfeet']}'), ('{$_SESSION['heightinches']}', '{$_SESSION['bloodtype']}', '{$_POST['hobbies']}', '{$_POST['favoritecolor']}'), ('{$_SESSION['favoritegemstone']}', '{$_SESSION['favoritefood']}', '{$_POST['leastfavoritefood']}', '{$_POST['favoriteschoolsubject']}'), ('{$_SESSION['leastfavoriteschoolsubject']}', '{$_SESSION['strengths']}', '{$_POST['weaknesses']}', '{$_POST['goal']}'), ('{$_SESSION['biography']}')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Character successfully created! Redirecting to Home page...";mysql_close($con)
?>

 

Will this code at the bottom work? We used a different code on the other one.

 

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

 

I'm assuming it will still work, since it was the code that worked on the page before I changed it to the session thing. Now if I want to add another set of table values, would I just start off my insert query line after the above code and before it echo's saying the form was submitted successfully?

I didn't go over your code with a fine-tooth comb, but it should follow the same basic structure. Just remember that the first page will be the only one without a session, the last page will need to grab all of the session variables plus the post variables from the one previous page, and all the pages in between would follow your "page2.php" structure.

Here's a question:

 

The second page (right now is the one I'm working on) needs to pull the 'identity' from the previous page, and then submit all the data from it's page. How would I write this page if I want to test what i've got so far? I have this:

 

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("smrpg", $con);
  $sql="INSERT INTO scout VALUES ('{$_SESSION['identity']}', '{$_SESSION['charactername']}', '{$_POST['elementofinfluence']}', '{$_POST['age']}', ('{$_SESSION['birthdatemonth']}', '{$_SESSION['birthdateday']}', '{$_POST['birthdateyear']}', '{$_POST['heightfeet']}', ('{$_SESSION['heightinches']}', '{$_SESSION['bloodtype']}', '{$_POST['hobbies']}', '{$_POST['favoritecolor']}', ('{$_SESSION['favoritegemstone']}', '{$_SESSION['favoritefood']}', '{$_POST['leastfavoritefood']}', '{$_POST['favoriteschoolsubject']}', ('{$_SESSION['leastfavoriteschoolsubject']}', '{$_SESSION['strengths']}', '{$_POST['weaknesses']}', '{$_POST['goal']}', ('{$_SESSION['biography']}')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
  
$sql2 = "INSERT INTO stats VALUES ('{$_SESSION['identity']}','$_POST[body]','$_POST[mind]','$_POST[soul]')";
if (!mysql_query($sql2,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Character successfully created! Redirecting to Home page...";
mysql_close($con)
?>

 

On my insert page, but I'm getting this error:

 

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

What could be the problem?

I saw a way it can be done to add to mulitple tables:

 

$sql = "begin ";

$sql .="insert into table 1...";

$sql .="insert into table 2...";

$sql .="insert into table N...";

$sql .="end";

 

$result = mysql_query($sql);

 

I'll post a new post if I run into problems. :-)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.