Jump to content

Inserting form data into database without defining variables


davidhemphill

Recommended Posts

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?
Link to comment
Share on other sites

[!--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]
<?php

if (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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.