Welcome to WebmasterWorld Guest from 54.147.0.174

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Multiple Inserts with one page

can anyone advise?

     

wruk999

12:11 pm on Apr 12, 2003 (gmt 0)

10+ Year Member



Hi there -0 I hope someone can help,

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.

Birdman

2:55 pm on Apr 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I do it with arrays and one form.

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?

wruk999

4:57 pm on Apr 13, 2003 (gmt 0)

10+ Year Member



Hi Birdman,

Thank you for your reply. I will have a go with that over the next day or so and let you know how I get on.

Thank You.
wruk999

wruk999

6:58 pm on Apr 15, 2003 (gmt 0)

10+ Year Member



Hi,

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

willybfriendly

9:14 pm on Apr 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"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."

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

grifter

5:47 am on Apr 16, 2003 (gmt 0)

10+ Year Member



wruk, like willy alluded to, they can be on the same page.

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.

willybfriendly

3:52 pm on Apr 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey grifter, you don't really need to redirect to a thank you page.

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

andreasfriedrich

4:29 pm on Apr 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




>>you don't really need to redirect to a thank you page

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

willybfriendly

4:44 pm on Apr 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"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. "

Do you suggest this due to server load issues, or security issues?

WBF

andreasfriedrich

7:01 pm on Apr 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Mainly usability issues: People are used to it that reloading a page does just that. Reload a page. They do not understand that reloading actually performs the request again. Most likely they will be confused that reloading a resource that was returned by a server as the result of a POSTed request will post that data again.

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

 

Featured Threads

Hot Threads This Week

Hot Threads This Month