Jump to content

inserting multipage form into mysql


LegionSmith

Recommended Posts

Hi Everyone!

 

I'm hoping someone here can help me. I have a 5 page form with roughly 175

fields/checkboxes that I'm trying to get inserted into a mysql database.

My deadline on this whole project was only 3 days so I had to figure out a quick method of doing things. Rather than using sessions and/or standard php arrays I found a snippet of code that takes all $_POST data from a previous page and creates hidden fields in the next page. That works just fine for this project.

here's that bit of code:

<?php foreach ($_POST as $key => $val) {
  echo '<input type="hidden" name="' . $key . '" value="' 
    . htmlentities($val, ENT_QUOTES) . '" />' . "\r\n";
} 
?>

 

Ok, so this as I said is spread out through pages 2 through 5 and each page posts to the next page. Then page 5 posts to process.php which is supposed to take all the previous fields and insert into a database. But I can't get it to work. here's what I have in process.php

 

<?php include("../Connections/wtrcapp.php"); ?>
<?php foreach ($_POST as $key => $val) {
  echo '<input type="hidden" name="' . $key . '" value="' 
    . htmlentities($val, ENT_QUOTES) . '" />' . "\r\n";
}
foreach($key as $val) {
$insert="INSERT INTO westoco6_app (".implode(",", array_keys($key)).") VALUES ('".implode("','", array_values($val))."')";
}
mysql_query($insert) OR die(mysql_error())
?>

Now I don't know that I'm even close to the mark on this but i think I am...most of my errors originate from line 6, the second foreach statement.

If anyone can give me a clue as to what I'm doing wrong here or point me in a better direction I'd be very appreciative. My deadline ends in the morning.

 

Thanks much for your time,

Michael Smith

 

Link to comment
https://forums.phpfreaks.com/topic/137485-inserting-multipage-form-into-mysql/
Share on other sites

Actually, I just got it working.

 

For future viewers of this post, here is all the working code I used for passing form values through several pages and then finally posting into a database after completion of form:

 

Use this code in all form pages except your first form page:

 

<?php foreach ($_POST as $key => $val) {
  echo '<input type="hidden" name="' . $key . '" value="'
    . htmlentities($val, ENT_QUOTES) . '" />' . "\r\n";
}
?> 

 

place the code above just inside your form begin tag. example:

<form method="POST" action="employment3.php" enctype="application/x-www-form-urlencoded" name="employment_form" id="employment_form">
   <?php foreach ($_POST as $key => $val) {
  echo '<input type="hidden" name="' . $key . '" value="'
    . htmlentities($val, ENT_QUOTES) . '" />' . "\r\n";
}
?>

 

Then in your final processing script use the code below. change the database values to match your own:

<?php
mysql_connect("myhost", "myusername", "mypass") or die(mysql_error());
mysql_select_db("mydatabase") or die(mysql_error());

if(!empty($_POST))
    {

        $first="";
        $second="";
        $insert="INSERT INTO mydbtable (";
        foreach ($_POST as $key => $var)
        {
            if($key!="submit")
            {
                $first.=$key.",";  //    <-----   use ` in field names if you want to make this better
                $second.="'". mysql_real_escape_string($var)."',";   //   <-----  use ' on values.

                echo '<input type="hidden" name="' . $key . '" value="'    .  htmlentities($var, ENT_QUOTES)   . '" />' . "\r\n";
            }
        }
        $query=$insert.$first.") VALUES (".$second.");";
         $query=str_replace(",)",")", $query) ;
        mysql_query($query) OR die(mysql_error()) ;
    }
?> 

 

And that's exactly what worked for me. it would be best to separate the connection in an include file, and you'll probably also need to redirect to a success page in this script...but the above will allow you to pass form values somewhat securely between several pages and insert them into mysql at the end of all things.

The purpose of this was to pass values without having to manually create sessions or variables for each and every form value. Quite handy for long forms such as mine. Note that the values will be visible in the source code of every page. But, it's a quick and dirty way to get the job done.

 

Special thanks goes to "icandothat", PappaJohn" and "djjjozsi" for helping me figure this out.

 

Thanks much!

 

Michael Smith

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.