#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2

    Changing all values for multiple field on one string


    Hey I have a piece of code where I'm wanting to make the values say something else if the current value of that item is empty, like if someone didn't enter anything in the form field. And the reason I'm doing this is so I don't put any null values into the database because I can't figure out how to update a null or 0 value.

    example

    PHP Code:
    if ($name=='' || $class=='' || $part_number=='' || $price=='' || $shipping=='' || $quantity=='' || $description=='' || $keywords=='') {
                
    set all the values to 'empty' for example.
            } 
    is there a way of doing all of them at once instead of doing one at a time like this

    PHP Code:
    if ($name=='') {
       
    $name 'empty';

    would doing something like this affect the way the data is submitted incase someone put information in some fields but not all, would this code above change all the fields to empty or just not work properly? Thanks for your time.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,996
    Rep Power
    375
    depends on if you have an array where you get these things from? i.e. $_POST or something.. if so then you can loop through those and check and set them to empty
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    If you kept all these values in an array instead of as separate variables you could do this with a loop (or even an array_map call), but if they're all separate variables they have to be handled separately.

    Also:
    $a = empty($a) ? 'empty' : $a;

    That syntax is a shorthand version of your IF above.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    The idea is to not put any null, 0 or empty values into the db and instead put a word in there instead so it can be updated later. so I just used the word 'empty' as an example I don't really want to change the value to an empty, 0, null value. Yes the values are being posted so, if (isset($_POST['name']...)) and then running checks within that IF statement and one of the checks would be to see if their is an empty, 0, null value being inputed and changing it to a word or number other than 0. I had started a thread a few days ago showing that I had trouble updating (UPDATE) 0, null, empty values within the db however updating values that contained a word or a number other than 0 were a breeze. But I haven't been able to find any answers on updating a null value in the db other than just putting a value other than null in there so it can be updated later. If there is a way to updating a null, 0 value, please don't hesitate to let me know it would be greatly appreciated! Thanks for your help so far.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    You say you do not wish to enter a 0, NULL, or empty value so you can update it later. Ummm... What does this have to do with anything? A 0, NULL, or empty column will update no different than a column holding the word empty. If the values are being posted, you DO want a blank or NULL value in the column. Unless I am just way overlooking something here... :-S

    Comments on this post

    • dmittner agrees : Not sure why there's the thought that you can't update columns with those values...
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    Here, I posted a link to the other thread I started with code for from a couple of pages that are supposed to update information and post it to the db. Code . So this code and form work fine for updating when there is data existing in the db other than 0. If there is a 0 value in the db, the form doesn't work, nothing gets updated. So I'm really not sure what's going on there, I'm not a php expert but any help, advice, direction will really help me to not make the same mistake twice and to act as a reference. If could help me figure this out that would be great. I checked my code to see if there was any spelling mistakes and I couldn't find any, the code works when there are no 0 values in the db, there are no errors outputted when the code is submitted and I have error reporting turned on. So like if there is someone who knows more about php that could maybe look over my code and see whats preventing updating that would be awesome! Thanks for your time and help. I'll post back here if I find a solution.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,996
    Rep Power
    375
    maybe somehow/somewhere php is taking 0 to mean false?

    you need to go through the code where the user input is sent to db and see what is happening (or post that code here). similarly look at the code to see what is being returned from db
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    I think I found the issue with help from some posts on another one of my threads and it had to do with the check to see if there was any empty values and then the form wouldn't submit. Hopefully that is the solution but if I run into this problem again and that wasn't the cause of the issue I'll open a new thread. Thanks for your time!
  16. #9
  17. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    His code in the other thread prevents updates from working when fields are "empty," by using the empty() function. empty() returns TRUE for zeroes, which was his problem.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    Ya I still haven't got around to figuring out how to update user fields that are null to begin with and no check to make sure all fields are required preventing the form from submitting but at least I solved one problem with string of code so far. Here's the link to the user information issue I was referring to Code. So I'm just going to start working on that issue in a bit and then post back there if I come up with a solution or not. Thanks!
  20. #11
  21. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    I still haven't got around to figuring out how to update user fields that are null to begin with
    The same way you update any other field. NULL shouldn't be important to your code.

    no check to make sure all fields are required preventing the form from submitting
    If all your fields are required why are any of them null to begin with?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    Some fields in the user table are automatically null because when a user registers I have is set up so there are other fields they can fill out later like a second phone number or they can put in their address. The issue there is that when trying to update the null fields they don't update but the fields that already have input do get updated. So I'm thinking that I have to INSERT any new values into the users row but I'm going to look at that more when I get a chance and post back to that thread if I run into that issue still. Thanks again for your help.
  24. #13
  25. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by andy1212
    Some fields in the user table are automatically null because when a user registers I have is set up so there are other fields they can fill out later like a second phone number or they can put in their address. The issue there is that when trying to update the null fields they don't update but the fields that already have input do get updated. So I'm thinking that I have to INSERT any new values into the users row but I'm going to look at that more when I get a chance and post back to that thread if I run into that issue still. Thanks again for your help.
    The value that exists already does not affect the updating of that value.

    The only time your code needs to care about NULL or something else, is that you don't want to encapsulate the value "NULL" in quotes when putting that into the table, and then prepared statements are probably exempt from that.

IMN logo majestic logo threadwatch logo seochat tools logo