|
Author
|
Topic: comparing array contents
|
zcrar70 Member
|
posted July 19, 2000 11:20 AM
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 |
Chris Pickett Member
|
posted July 19, 2000 12:07 PM
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 |
ledjon Member
|
posted July 19, 2000 12:56 PM
You should be able to setup the database so that it automatically puts NULL in if nothing is entered. |
Chris Pickett Member
|
posted July 19, 2000 01:05 PM
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 |
ledjon Member
|
posted July 19, 2000 01:34 PM
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. |
zcrar70 Member
|
posted July 20, 2000 05:27 AM
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 |