davidhemphill Posted June 19, 2006 Share Posted June 19, 2006 Okay so I'm creating a simple content management system and I've run into a problem.I've created functionality that will allow the user to have multiple weblogs and to specify their own data model. For instance, one weblog may have the fields: title, body, body_extended, and another might have just: title, and description, it all depends on the user's need. After submitting the information in the entry form I need to be able to take the variables that are post and stick them in their respective fields in the database without defining variables for them in the script.So if they enter data for title, body, and body_extended that information gets put in database fields of the same name.Does that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/12399-inserting-form-data-into-database-without-defining-variables/ Share on other sites More sharing options...
Ninjakreborn Posted June 19, 2006 Share Posted June 19, 2006 if you mean what I think you mean you should never use uninitialized variables, what you could do, is put what the person inputs into an array, and use it, or database the results, explain more what you mean. Quote Link to comment https://forums.phpfreaks.com/topic/12399-inserting-form-data-into-database-without-defining-variables/#findComment-47384 Share on other sites More sharing options...
obsidian Posted June 19, 2006 Share Posted June 19, 2006 [!--quoteo(post=385710:date=Jun 19 2006, 02:01 PM:name=David Hemphill)--][div class=\'quotetop\']QUOTE(David Hemphill @ Jun 19 2006, 02:01 PM) [snapback]385710[/snapback][/div][div class=\'quotemain\'][!--quotec--]Okay so I'm creating a simple content management system and I've run into a problem.I've created functionality that will allow the user to have multiple weblogs and to specify their own data model. For instance, one weblog may have the fields: title, body, body_extended, and another might have just: title, and description, it all depends on the user's need. After submitting the information in the entry form I need to be able to take the variables that are post and stick them in their respective fields in the database without defining variables for them in the script.So if they enter data for title, body, and body_extended that information gets put in database fields of the same name.Does that make sense?[/quote]well, if you name the fields of your form accordingly, you'll already have the names of those columns in your $_POST array for retrieval. for instance, to dynamically build an insert query based on only the fields that are submitted, i would do something like this:[code]<?phpif (isset($_POST['submit'])) { // form has been submitted, so get which fields are submitted: $fields = array(); $values = array(); foreach ($_POST as $key => $value) { $fields[] = $key; $values[] = trim($value); } // now, you've got them all, let's build our SQL query: $sql = "INSERT INTO tableName (" . implode(',', $fields) . ") VALUES ('" . implode("','", $values) . "')"; mysql_query($sql);}?>[/code]may need some tweaking for your situation, but it gives you the basic principle.hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/12399-inserting-form-data-into-database-without-defining-variables/#findComment-47388 Share on other sites More sharing options...
davidhemphill Posted June 19, 2006 Author Share Posted June 19, 2006 Thanks. That helps alot in giving me an idea of how to approach it. I will check back if I can get it to work or not. Quote Link to comment https://forums.phpfreaks.com/topic/12399-inserting-form-data-into-database-without-defining-variables/#findComment-47442 Share on other sites More sharing options...
davidhemphill Posted June 19, 2006 Author Share Posted June 19, 2006 Awesome! Got it to work by changing some lines and adding some addslashes() action! Thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/12399-inserting-form-data-into-database-without-defining-variables/#findComment-47478 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.