Forum Moderators: coopster
At the moment, a system requires records added to the database, one at a time, via a page with a form, and then a processing page.
The system has grown, and more records are being added, and I need to know whether it is possible to say, have a page with 10 forms on, and all them be inserted into the database in one go, but it not insert the forms which haven't been entered into..(say for example only 8 forms/records were needed to be inserted)
Anyone have any ideas on how this would work?
Language: PHP
Database: MySQL
Platform: Apache
Regards,
William.
EXAMPLE
<form action="update_mysql.php" method="post">
<input type="text" name="field1[]" />
<input type="text" name="field2[]" />
<input type="text" name="field3[]" /><input type="text" name="field1[]" />
<input type="text" name="field2[]" />
<input type="text" name="field3[]" /><input type="text" name="field1[]" />
<input type="text" name="field2[]" />
<input type="text" name="field3[]" /><input type="submit" />
</form>The PHP:
for($i=0;$i<count($field1);$i++){
if($field1[$i] == "") break;
mysql_query("INSERT INTO table...");
}
I think that will work?
The system that this is to be built into uses a central config file for the admins (who use this form). And they all use the GET method for handling the forms.
My current insert statement looks like this:
} elseif ($action == add) {
$job = addslashes($HTTP_GET_VARS['job']);
$name = addslashes($HTTP_GET_VARS['name']);
$series = addslashes($HTTP_GET_VARS['series']);
$colour = addslashes($HTTP_GET_VARS['colour']);
$fef = addslashes($HTTP_GET_VARS['fef']);
$ozone = addslashes($HTTP_GET_VARS['ozone']);
$nl = addslashes($HTTP_GET_VARS['nl']);
$ta = addslashes($HTTP_GET_VARS['ta']);
$cover = addslashes($HTTP_GET_VARS['cover']);
$dealer = addslashes($HTTP_GET_VARS['dealer']);
$status = addslashes($HTTP_GET_VARS['status']);
$available = addslashes($HTTP_GET_VARS['available']);$sql = " INSERT INTO stock (job, name, series, colour, fef, ozone, nl, ta, cover, dealer, status, available) ";
$sql = $sql . " VALUES ('$job','$name','$series','$colour','$fef','$ozone','$nl','$ta','$cover','$dealer','$status','$available')"; $result = mysql_query($sql);
print "Your request to add to the system was successful";
}
What do you suggest is the best way to use this method, with your array forms on the submit page >> The form and PHP insert cant be on the same page.
TIA,
wruk999
Not sure I am following you here. The form and PHP script will be in two different locations. For example "form.html" and "process_form.php". This would be reflected in your form action ="process_form.php" (Actually, you could include the processing scripts on the same "page", but they would remain invisible to the user since they never leave the server.)
All the PHP processing is done server side. All the user ever sees is plain old HTML coding. (This statement assumes you use the php extension and not something like .inc)
The only thing that your users would see with the above script, even with "view source" would be, "Your request to add to the system was successful"
WBF
For forms, I prefer to do everything in one .php page. Especially if I want to give users feedback if there was an input error. I set the FORM to post back to itself when Submit is pressed.
Consider this simplification:
<?
if (this is the result of a POST) {
check the posted input
if (one or more errors in input) {
stick errors in $errors
} else {
input OK, redirect to step 2, thanks page, etc.
}
} else if (this is a GET, or there are $errors) {
display the form, show $errors if necessary
}
?>
If someone has a better method, I'd love to hear it.
if(!$_POST()){
display_form();
}else if(errors()){
show_errors();
display_form()
}else{
process_form();
show_thanks();
}
Do your form, error checks and thanks as functions. Set your form values to the $_POST(). (If they aren't there, the form will display as blank the first time). Set the form action to call itself. This keeps the logic seperate from the display elements, which always helps in keeping the code portable.
WBF
Not necessarily but redirecting to a different URL is still useful. It prevents the user from POSTing the form again when she reloads the browser window. While this safe guard should not keep one from implementing some measure on the server side as well it is useful when larger data were POSTed.
>>keeps the logic seperate from the display elements
I donīt see why this is not the case with grifterīs approach? Using a template engine keeps PHP [php.net] code and html code separate.
Andreas
Nevertheless implementing a server side solution that safe guards against multiple submissions of the same form data is to prevent manipulation of the data on the server. This will help against people trying to flood your system.
Andreas