PHP / MySQL Help Needed [Forms]

Hi All,

here’s the scenario.

I have a form, 7pages, multi-page.
Running them off txt file as “id name type pattern”
using Ajax for validation

I have everything setup, save for the upload from Form to MySQL database.

How do I go about doing this?

I know of the normal insert into table statement, but I have over 300 fields.
Is there a way to do this using a function?

I am pretty sure this is a noob question, but my programmer is having exams and so i decided to take off from where he left off.

<php foreach ($_POST as $key => $value) { > <?php } ?>

How do I go around doing a nested loop to insert the the form data into MySQL database?

Thanking you all in advance,

Daniel

just use a loop:
[php]//u should use some validation:
$fields=array(‘fieldname1’,‘fieldname2’, /* … */ ‘fieldname300’);

$sql=‘INSERT INTO table SET ‘;
$sqlvalues=array();
foreach ($_POST as $key => $value)
{
if(in_array($key,$fields))
$sqlvalues = ‘'.$key.'="’.mysql_real_escape_string($value).’"’;
}
$sql.=implode(’, ',$sqlvalues);[/php]

for somthing that short i woudn’t use a function.

Thanks for the code,

I will get that up shortly,

but before trying that, I would like to find out what this means…

THis is the code on my index.php

Form Action:

 <form method="post" action="<?php echo $nextpage ? $_SERVER['PHP_SELF'] : 'submit.php'; ?>" onsubmit="return validate($('form_registration'));" id="theForm">

Submit Button:

<input type="submit" id="theSubmit" value="Submit Form Part <?php echo $page; ?>" onclick="this.disabled=true; $('theSubmit_error').style.display = 'none'; $('theSubmit_spinner').style.display = 'inline'; return ajaxMassValidation($('theForm'), <?php echo $page - 1; ?>, true);" />

Then, on the submit.php page,
the only line of code is

print_r($_POST);

I guess the sql connection and insert command should be put at the submit.php?

there is a boolian that tells u whether there is form (page) left.
if there is that form is displayed with the same file ($_SERVER[‘PHP_SELF’] )
otherewise the form is submited to submit.php.

btw: seems that ur code is missing a serverside validation.

this is doing the JS validation.

this is for testin purpose only (outputing all posted vars)

yes.
but the connect is often done inside an includefile (that has to be incuded in submit.php), thats up to u.

Hi,

Sorry to disturb again… I have been trying for 2 days now, but somehow, i think the values are not carried forward from one page to the other… because the print_r($_POST) only outputs:

Connected to MySQL Array ( [page] => 2 )

Values were not printed, and database was not updated…

Should it not be showing the entire dump including the vars used?
I’m not sure, but I guess something was left out in the index or submit file… Here’s the code for both, please let me know where it went wrong.

Index.php

[code]<?php
require(‘functions.php’);
$page = isset($_POST[‘page’]) ? $_POST[‘page’] : 0;
if (isset($GET[‘page’])) $page = $GET[‘page’];
$data = validator_get_data('rules
’ . $page . ‘.txt’);
$page++;
$nextpage = file_exists('rules
’ . $page . ‘.txt’);
?>

Dynamic Validation

Registration Form: Page <?php echo $page; ?>

<?php foreach ($_POST as $key => $value) { if ($key == 'page') continue; echo ' ' . "n"; } ?> <?php echo validator_build_form($data, $page - 1); ?>

Validating... Error! The form is not filled correctly. Please fix all errors before proceeding. OK The form is filled correctly. Please submit the form and proceed to part <?php echo $page; ?>.

Valid XHTML 1.0 Strict, Valid CSS!

[/code]

Submit.php

[code]<?php
$username = “test”;
$password = “test”;
$hostname = “localhost”;
$dbh = mysql_connect($hostname, $username, $password)
or die(“Unable to connect to MySQL”);
print “Connected to MySQL
”;
$selected = mysql_select_db(“reg”,$dbh)
or die(“Could not select reg”);
mysql_close($dbh);
$table = “sturegtest”;
$fields=array(‘name’,‘nameFirst’,‘nameLast’,‘nameChurch’,‘addressChurch’,‘regularlyAttend’,‘memberChurch’);

$sql=‘INSERT INTO sturegtest SET ‘;
$sqlvalues=array();
foreach ($_POST as $key => $value)
{
if(in_array($key,$fields))
$sqlvalues = ‘'.$key.'="’.mysql_real_escape_string($value).’"’;
}
$sql.=implode(’, ',$sqlvalues);
print_r($_POST);
?> [/code]

ur right, they arn’t submitted, but i don’t know why.

for debuging:

  • put print_r($_POST); on the index.php page as well to see at wich point the post-vars get lost.
  • use “view html-source” in ur browser an look at the <input type="hidden" fields, maybe there’s something wrong.

tell us what u found.

Hi Bros,

I Did, and it fixed the error, however, the submit to database didn’t quite work, but i used the manual way (since i’m just debugging on 2 pages (total of 5-6 fields), it won’t hurt to do manually for those 300 fields of course…

But, I have a major problem:
It works super fine on mozilla firefox (goes to next page, submits value to database) = perfect

But on IE, it doesn’t even go to the next page.

The error is on this chunk:

<script type="text/javascript"> //<![CDATA[ ajaxValidationListener($('nameLast'), 0); //]]> </script>

Any idea on how I could go about fixing it? I’m so sorry but i’m aware that this is not php but javascript…

link: http://dp.nitro3.net/shift/ [in I.E, there is error, but in firefox, works like a charm]

[/url]

i’m using linux (no IE to test it)

whats the JS-error-message?

Hi Bro,

Lots happened in this past 30mins, and really, quite funny too…

  1. Fixed the database submit using normal method… loop didn’t work for some reason (i’ll get back to that when i have time)

  2. Worked in Firefox, didn’t work in other browsers with error on Ajax side with "object or property … "

  3. Fixed that, and it worked fine in IE till the submission to database, when I encountered the "incorrect integer value at Column… " error

  4. Fixed that too…

Here’s what I did.

First the JS error:
Original Code in Index.php: <input type="submit" id="theSubmit" value="Submit Form Part <?php echo $page; ?>" onclick="this.disabled=true; $('theSubmit_error').style.display = 'none'; $('theSubmit_spinner').style.display = 'inline'; return ajaxMassValidation($('theForm'), <?php echo $page - 1; ?>, true);" />

Modified That to <input type="submit" id="theSubmit" value="Submit Form Part <?php echo $page; ?>" return ajaxMassValidation($('theForm'), <?php echo $page - 1; ?>, true);" />

So, i removed the "onclick="this.disabled=true; $(‘theSubmit_error’).style.display = ‘none’; $(‘theSubmit_spinner’).style.display = ‘inline’; " part, and at least, it allowed access to the next page (though on IE, still shows error)

While submitting, i Had that incorrect integer value at column error… Searched google, and found that I had to comment this line on sql.
sql-mode=“STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION”

Weird… well, now it submits both in Firefix & IE.
IE still has an object error with that submit line shown above (but no problem with submission)

So I guess, its time to optimize and try it on my 300 field form now that the 6 field works :P

Pls do contribute if there is any way to optimize things and all that… learnt alot by just checking things for myself and playing around with PHP.

you guys have great knowledge.

Something went wrong… It’s very very weird.

I added print_r(_POST) back on the index to check out what went wrong…

It Doesn’t post the vars when I click on the “Submit” button,

BUT

When I click back on the browser, then “enter” or click submit again, it posts the vars…

What could be wrong here??

This thing looks like it has a mind on its own…

Everything was going smooth, and I have NO idea what went wrong…

The demo page is here:
http://dp.nitro3.net/shift/

Could someone help me out?

thanks!!

Summary of error:

when i hit submit: Array ( [page] => 1 ) When i go back on browser, and resubmit: Array ( [page] => 1 [nameFirst] => FASDF [nameLast] => ASDFASDFASDF )

this is an JavaScript/Ajax error.

the ajax seems to be very unstable.

this is what i would do:

  • keep the check for every field and the messages. they are realy nice.
  • remove the check vor form-onsubmit and input-submit-onclick. they are “useless” cause its a clientsite validation that can be bypassed anyway.
  • do a validation for the $_POST vars in PHP. this should be very esay as the Ajax-validation is done in php already (validate.php).
  • and then just use $page-- if the validation failed.

it need a littlebit of rewriting the script but would make it: more secure, more stable and it shuold add suppoted for none JS-browsers.

hope this helps

Sponsor our Newsletter | Privacy Policy | Terms of Service