Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Aug 2005
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts

    loop inserting optional data?

    Hey guys, I have a question. I am trying to loop for inserting "optional" datas into database.

    For instance, I have a form that has 3 input boxes: Job, Education, Address, and those fields/input boxes are optional, which means the user does not have to enter all of them. If they want to enter all, that's great. But if they want to just enter education, and leave other 2 blanks, that's cool too and so forth.

    I am wondering if there's a best way to handle this by inserting the data in to the database, like using loop? I don't want to just use a bunch of if statements in my code. Because there are more than just 3 input boxes.

    Thanks

  • #2
    New Coder
    Join Date
    Jul 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Depending on the method you use for the form, you can try something like this:

    PHP Code:
    $invalidkeys = array('submit'); //Add form field names that you do not want

    $qry "INSERT INTO database (";
    foreach(
    $_GET as $key => $value)
    {
        if (!
    in_array($key,$invalidkeys))
        {
           
    $keys .= "`$key`, ";
           
    $values .= "'$value', ";
         }
    }
    $keys trim($keys,', ');
    $values trim($values,', ');

    $qry .= " $keys) VALUES ($values)"
    Of course you want to make sure that your form field names are exactly the same name and case as your table fields.

    I'm sure there's other solutions out there but this is one I actually just implemented today at work.

    Good luck!

  • #3
    Regular Coder
    Join Date
    Aug 2005
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by martialtiger View Post
    Depending on the method you use for the form, you can try something like this:

    PHP Code:
    $invalidkeys = array('submit'); //Add form field names that you do not want
    What does this line meanI dont understand why its there?

    foreach(
    $_GET as $key => $value)
    also this linewha tis key and what is valueI am very bad at array stuff
    Good luck!
    Thanks for the help , my form should look like this:

    <form method=POST action=path.php>
    <input box type=text name=job>
    <input box type=text name=edu>
    <input box type=text name=address>
    <input type=sbumit name=submit value=submit>

  • #4
    New Coder
    Join Date
    Jul 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In your case then you would do this:
    PHP Code:
    $invalidkeys = array('submit'); //Add form field names that you do not want

    $qry "INSERT INTO database (";
    foreach(
    $_POST as $key => $value)
    {
        if (!
    in_array($key,$invalidkeys))
        {
           
    $keys .= "`$key`, ";
           
    $values .= "'$value', ";
         }
    }
    $keys trim($keys,', ');
    $values trim($values,', ');

    $qry .= " $keys) VALUES ($values)"
    $invalidkeys is basically an array of whatever form field names you don't want to include in your query like the 'submit' field.

    As for the foreach loop, $_POST is an array and you can access the values like so $value = $_POST['key']. So you're basically just getting the keys (which again should be the table field names) and the form value.

    Hope that clarifies it for you.

  • #5
    Regular Coder
    Join Date
    Aug 2005
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I got this error

    Code:
    Notice: Undefined variable: keys in C:\AppServ\www\php\test_array.php on line 13
    
    Notice: Undefined variable: values in C:\AppServ\www\php\test_array.php on line 14
    `test1`, `test2`'Test', 'Test2'
    Here is my sample html file

    <HTML>
    <HEAD>
    <TITLE>New Document</TITLE>
    </HEAD>
    <BODY>
    <form method=POST action=test_array.php>
    <input type=text name=test1>
    <input type=text name=test2>
    <input type=submit name=submit value=submit>
    </form>
    </BODY>
    </HTML>
    Last edited by cgibie; 02-06-2007 at 07:37 PM.

  • #6
    Regular Coder
    Join Date
    Aug 2005
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oh I went to see my db, and those values were saved correctly, but I dont know why I got those 2 lines message error?

  • #7
    Regular Coder
    Join Date
    Sep 2004
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    yea you will get errors since you are trying to concatenate those two variables as such

    PHP Code:
           $keys .= "`$key`, ";
           
    $values .= "'$value', "
    on the first pass its looking to add the values to the existing value of a non-existing variable...

    add this after the $invalidkeys line
    PHP Code:
    var $keys;
    var 
    $values
    -or-
    PHP Code:
    $keys="";
    $values=""

  • #8
    Regular Coder
    Join Date
    Aug 2005
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you. It works as expected now

  • #9
    Regular Coder
    Join Date
    Aug 2005
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oops, it works with my test script, and when I tried to implement with my real script, I got this error after 1 data has been entered into the db:

    Code:
    ErrorDuplicate entry '' for key 1


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •