bluegray Posted January 13, 2010 Share Posted January 13, 2010 Hello Everyone, I'm new to these forums, but am new to php as well, so I may be sticking around for a while. Hope my newbness doesn't cause too many problems. On to the point. I'm building a site with several lengthy forms, and while I know how to get the submitted data into their corresponding variables individually, I was looking for a way to automate the entire process and then automate the insertion of each piece of data into the database. The tool I deferred to was an array and a foreach loop, but for some reason it's not working; I think it might be a problem in the MySQL query statement, but I'll let you be the judge of that. foreach ($_POST ['listdock'] as $ld) { if (trim ($ld == "")) { $ld = null ; } $query = "INSERT INTO docks (1,2,3,4,5, etc) VALUES (\"$ld\")" ; $result = mysql_query($query) ; echo "$ld"; if ($result) { ?> <h3>Your dock has been added to our database!</h3> <p>Check the listings page to find your dock.</p> <?php ; } else { ?> <h3>We could not add your dock to our database at the moment.</h3> <?php ; } } I see the problem...or at least one of them. It's where the table names goes "(1,2,3,4,5,etc);" Any help appropriating this to work would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/188376-automating-form-to-database-and-back-data-handling/ Share on other sites More sharing options...
mikesta707 Posted January 13, 2010 Share Posted January 13, 2010 The problem is $ld is a loop variable, and get overwritten everytime the foreach happens. What you probably want to do (assuming that the order of the array elements corresponds to the order of the columns in your insert statement) is to make a string $insert = "" foreach ($_POST ['listdock'] as $ld) { $insert .= "'"$ld."',"; } $insert = rtrim($insert, ",");//trim the last comma off the string and then you could use the insert string like so $query = "INSERT into blah blah blah VALUES($insert)"; Quote Link to comment https://forums.phpfreaks.com/topic/188376-automating-form-to-database-and-back-data-handling/#findComment-994458 Share on other sites More sharing options...
Andy-H Posted January 13, 2010 Share Posted January 13, 2010 I think what you need to do is this: $num = 1; $tables = array(); $values = array(); foreach( $_POST['listdock'] as $ld ) { if ( !trim($ld) ) $ld = null; $tables[] = $num++; $values[] = $ld; } $query = "INSERT INTO docks ( " . implode(", ", $tables) . " ) VALUES ( '" . implode("', '", $values) . "' )"; // edit oops lol $result = mysql_query($query); if ($result) { echo '<h3>Your dock has successfully been added to our database!</h3>' . "\n\n"; echo '<p>Check the listings page to find your dock.</p>'; } else { trigger_error("Error: " . mysql_error(), E_USER_ERROR); } ??? Quote Link to comment https://forums.phpfreaks.com/topic/188376-automating-form-to-database-and-back-data-handling/#findComment-994463 Share on other sites More sharing options...
bluegray Posted January 14, 2010 Author Share Posted January 14, 2010 I think what you need to do is this: $num = 1; $tables = array(); $values = array(); foreach( $_POST['listdock'] as $ld ) { if ( !trim($ld) ) $ld = null; $tables[] = $num++; $values[] = $ld; } $query = "INSERT INTO docks ( " . implode(", ", $tables) . " ) VALUES ( '" . implode("', '", $values) . "' )"; // edit oops lol $result = mysql_query($query); if ($result) { echo '<h3>Your dock has successfully been added to our database!</h3>' . "\n\n"; echo '<p>Check the listings page to find your dock.</p>'; } else { trigger_error("Error: " . mysql_error(), E_USER_ERROR); } ??? I'm not sure what if ( !trim($ld) ) $ld = null; does and I 'm a little perplexed about what to do if I actually gave names to the tables in the database. The number vals works for the $tables var so long as it's actually a number that I assign to the name of the field, but what if i gave them names? Quote Link to comment https://forums.phpfreaks.com/topic/188376-automating-form-to-database-and-back-data-handling/#findComment-994617 Share on other sites More sharing options...
bluegray Posted January 14, 2010 Author Share Posted January 14, 2010 The problem is $ld is a loop variable, and get overwritten everytime the foreach happens. What you probably want to do (assuming that the order of the array elements corresponds to the order of the columns in your insert statement) is to make a string $insert = "" foreach ($_POST ['listdock'] as $ld) { $insert .= "'"$ld."',"; } $insert = rtrim($insert, ",");//trim the last comma off the string and then you could use the insert string like so $query = "INSERT into blah blah blah VALUES($insert)"; My main problem wasn't in figuring out how to get the correct values inserted, so much as it was getting a number of values inserted to the corresponding table fields; so when you say "blah blah blah" i think that's skipping one of the more crucial parts of my dilemma. do you know what I mean? Quote Link to comment https://forums.phpfreaks.com/topic/188376-automating-form-to-database-and-back-data-handling/#findComment-994619 Share on other sites More sharing options...
Andy-H Posted January 14, 2010 Share Posted January 14, 2010 The only other way I can think of is if you add your sql field names to the corresponding array key or the list dock form input. That way you can use: <input type="text" name="listdock[sqlfieldName1]" value="" > $tables = array(); $values = array(); foreach( $_POST['listdock'] as $field => $ld ) { if ( !trim($ld) ) $ld = null; // this just sets $ld var to null if trim($ld) evaluates to false, I.e. Empty / 0 $tables[] = $field; $values[] = $ld; } $query = "INSERT INTO docks ( " . implode(", ", $tables) . " ) VALUES ( '" . implode("', '", $values) . "' )"; $result = mysql_query($query); if ($result) { echo '<h3>Your dock has successfully been added to our database!</h3>' . "\n\n"; echo '<p>Check the listings page to find your dock.</p>'; } else { trigger_error("Error: " . mysql_error(), E_USER_ERROR); } Quote Link to comment https://forums.phpfreaks.com/topic/188376-automating-form-to-database-and-back-data-handling/#findComment-995082 Share on other sites More sharing options...
bluegray Posted January 14, 2010 Author Share Posted January 14, 2010 The only other way I can think of is if you add your sql field names to the corresponding array key or the list dock form input. That way you can use: <input type="text" name="listdock[sqlfieldName1]" value="" > $tables = array(); $values = array(); foreach( $_POST['listdock'] as $field => $ld ) { if ( !trim($ld) ) $ld = null; // this just sets $ld var to null if trim($ld) evaluates to false, I.e. Empty / 0 $tables[] = $field; $values[] = $ld; } $query = "INSERT INTO docks ( " . implode(", ", $tables) . " ) VALUES ( '" . implode("', '", $values) . "' )"; $result = mysql_query($query); if ($result) { echo '<h3>Your dock has successfully been added to our database!</h3>' . "\n\n"; echo '<p>Check the listings page to find your dock.</p>'; } else { trigger_error("Error: " . mysql_error(), E_USER_ERROR); } I believe this is what i needed. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/188376-automating-form-to-database-and-back-data-handling/#findComment-995087 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.