[PHP-DB] Processing a web form / loop etc.
I have a web form that accepts up to 3 suggested categories. In the form they are part of I have named these variables $suggested_category_1 $suggested_category_2 $suggested_category_3 I have come up with this simple code to store the suggestion in a table: if ( $suggested_category_1 ) { #the user could leave the suggestion blank because what is already provided is sufficient mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( Unable to select database); $query = INSERT INTO table_name VALUES ( '$variable' , '$reference_number', '$suggested_category_1'); mysql_query($query); mysql_close(); } Is there any way to create a loop to check all three instead of me repeating this code twice more changing $suggested_category_1 to _2 and _3 ? Ron -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Processing a web form / loop etc.
Ron Piggott (PHP) wrote: I have a web form that accepts up to 3 suggested categories. In the form they are part of I have named these variables $suggested_category_1 $suggested_category_2 $suggested_category_3 I have come up with this simple code to store the suggestion in a table: if ( $suggested_category_1 ) { #the user could leave the suggestion blank because what is already provided is sufficient mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( Unable to select database); $query = INSERT INTO table_name VALUES ( '$variable' , '$reference_number', '$suggested_category_1'); mysql_query($query); mysql_close(); } Is there any way to create a loop to check all three instead of me repeating this code twice more changing $suggested_category_1 to _2 and _3 ? for ($num = 1; $num = 3; $num++) { $varname = 'suggested_category_'.$num; $val = $$varname; // Stick your code here replacing references to the $suggested_category_1 var with $val } Incidentally, I do hope that code was simplified for brevity. There should be extensive use of mysql_real_escape_string in there. -Stut -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Processing a web form / loop etc.
Hi Stutt Thanks. I hadn't included much of the code for easy reading. I just had to use the $_POST[$val] to get the actual values. Thanks for your help. Ron for ($num = 1; $num = 3; $num++) { $varname = 'suggested_category_'.$num; $val = $$varname; // Stick your code here replacing references to the $suggested_category_1 var with $val } Incidentally, I do hope that code was simplified for brevity. There should be extensive use of mysql_real_escape_string in there. -Stut -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php