Jump to content


Photo

Inserting form data into database without defining variables


  • Please log in to reply
4 replies to this topic

#1 davidhemphill

davidhemphill
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 19 June 2006 - 06:01 PM

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?

#2 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 19 June 2006 - 06:10 PM

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.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 19 June 2006 - 06:14 PM

[!--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) View Post[/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:
<?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);
}

?>


may need some tweaking for your situation, but it gives you the basic principle.

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#4 davidhemphill

davidhemphill
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 19 June 2006 - 07:41 PM

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.

#5 davidhemphill

davidhemphill
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 19 June 2006 - 09:56 PM

Awesome! Got it to work by changing some lines and adding some addslashes() action! Thank you very much.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users