#1
  1. Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    49
    Rep Power
    15
    hi,

    i'm having a small problem with getting information into a database. Basically, i've got a form with some elements in it, such as:
    name
    phone
    address

    for example. I pass these values to a function which finds out whether the action is associateds with the form is to insert, update or delete info from/to the database (using an HTML "hidden" field.)

    The field names in my database are the same as in my form. (name, phone etc...). Using mysql_fetch-field, I have an array with the field names in it. I read the values passed from the form into an array called $values with the form fields as keys ? I would like to set the entry as 'NULL' for each field in which there is no data to enter. I thought the best way to do this was by comparing the contents of both arrays, and where there was a key in $values corresponding to the fieldname, somehow assign the contents of $values to that fieldname in the query....

    sorry this is quite specific and' i'm sure it's been answered before, except i can't find where.....does anyone have any ideas ?

    cheers,
    nick
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    59
    Rep Power
    15
    Ok, if I am understanding correctly you want to take Name, Phone, etc., from an HTML form, and if it is empty put NULL in the database, otherwise, put what they enter?

    Try something like this.

    <?
    if($name == " ")
    {
    $name = "NULL";
    }
    elseif($email == " ")
    {
    $email = "NULL";
    }
    etc.

    then just insert, update like normal.

    If I misunderstood just yell at me
    Chris
  4. #3
  5. No Profile Picture
    ledjon
    Guest
    Devshed Newbie (0 - 499 posts)
    You should be able to setup the database so that it automatically puts NULL in if nothing is entered.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    59
    Rep Power
    15
    I think a text field automatically returns something, I had trouble when I was making a databse program, and trying to search, it would always come back with everything, because the empty text field would return a " ", but you can try that, if it works, it would be the easier solution.

    Chris
  8. #5
  9. No Profile Picture
    ledjon
    Guest
    Devshed Newbie (0 - 499 posts)
    Oh, I see.. this is calling info from the DB then? (I didn't pay much attention). Then what chris suggested is the best thing to do.
  10. #6
  11. Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    49
    Rep Power
    15
    hi,

    thanks a lot for your answers - unfortunately that's not quite what i'm trying to do. I'm actualy trying to build a function / class to dynamically enter info from a form into a database, independent of the form or the database used or of the action (insert, update etc.,..).

    As such I though that if I entered the info from the form into an array and compared the keys to the 'name' key in the array returned by mysql_fetch_fields, I could enter the info from the form in the corresponding field in the db insert 'null' to the rest. (I actually need to send NULL in the query, so that auto_increment or timestamp fields automatically set.)

    Any ideas ?

    Cheers,

    nick

Similar Threads

  1. return contents of directory in array
    By jacen6678 in forum C Programming
    Replies: 1
    Last Post: January 26th, 2004, 04:32 PM
  2. returning contents of a directory as an array
    By c0ldshadow in forum C Programming
    Replies: 4
    Last Post: January 15th, 2004, 09:51 PM
  3. retain array contents in a loop
    By papajohns in forum Perl Programming
    Replies: 5
    Last Post: January 7th, 2004, 09:09 PM
  4. Replies: 3
    Last Post: December 24th, 2003, 04:23 PM
  5. multi dimensional arrays - Can't grasp how to?
    By Kaleina in forum PHP Development
    Replies: 17
    Last Post: November 5th, 2003, 04:18 PM

IMN logo majestic logo threadwatch logo seochat tools logo