Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2

    Can a 0 value in a db table be converted to words using the UPDATE sql statement?


    Lets say I have a table with a column called product_price. I inserted data and decided not to put a price yet so just put 0 and submitted the data. Then I want to go back and update the price to something like $5.00. Can the UPDATE statement do that? I've been trying figure out why the 0'ed out columns won't update any data yet if I had entered anything other than 0, the data can be updated perfectly fine. I tried looking on Google for answers and couldn't come up with any solutions. Thanks for your time.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    you've already been told that we need to see your code.

    UPDATEs don't care about the previous value. If certain rows don't get updated, it's an issue of your code.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    Ok here's code from a couple pages where I'm trying to update fields and it won't update them

    in the db,

    1 id int(11) AUTO_INCREMENT
    2 class varchar(200) utf8_general_ci
    3 name longtext utf8_general_ci
    4 part_number varchar(200) utf8_general_ci
    5 price float
    6 shipping float
    7 quantity int(11)
    8 date datetime
    9 description longtext utf8_general_ci
    10 keywords longtext utf8_general_ci
    11 ext varchar(4) utf8_general_ci
    12 url longtext utf8_general_ci

    upload.func.php
    PHP Code:
    function update_parts($id$name$part_number$price$shipping$quantity$description$keywords) {
        
    $id = (int)$id;
        
    $name mysql_real_escape_string($name);
        
    $part_number mysql_real_escape_string($part_number);
        
    $price mysql_real_escape_string($price);
        
    $shipping mysql_real_escape_string($shipping);
        
    $quantity mysql_real_escape_string($quantity);
        
    $description mysql_real_escape_string($description);
        
    $keywords mysql_real_escape_string($keywords);
        
        
    mysql_query("UPDATE `parts` SET `name`='$name', `part_number`='$part_number', `price`='$price', `shipping`='$shipping', `quantity`='$quantity', `description`='$description', `keywords`='$keywords' WHERE `id`='$id'");

    PAedit.php
    PHP Code:
    <div style="float: left; width: 100%;">
    <?
    $id 
    $_GET['id'];
        
    $data data2($id'name''part_number''price''shipping''quantity''description''keywords');
        
        if (isset(
    $_POST['name'], $_POST['part_number'], $_POST['shipping'], $_POST['quantity'], $_POST['price'], $_POST['description'], $_POST['keywords'])) {
        
            
    $name $_POST['name'];
            
    $part_number $_POST['part_number'];
            
    $price $_POST['price'];
            
    $shipping $_POST['shipping'];
            
    $quantity $_POST['quantity'];
            
    $description $_POST['description'];
            
    $keywords $_POST['keywords'];
            
            
    $errors = array();
        
            
            if (empty(
    $name) || empty($part_number) || empty($price) || empty($shipping) || empty($quantity) || empty($description) || empty($keywords)) {
                
    $errors[] = 'All fields required!';
            }
        
            if (!empty(
    $errors)) {
                foreach (
    $errors as $error) {
                    echo 
    $error'<br />';
                }
            } else {
                
    update_parts($id$name$part_number$price$shipping$quantity$description$keywords);
                
    header('Location: addpart.php');
                exit();
            }
        }
    }
    ?>
    </div><br /><br />
    Code:
    <div style="float: left; width: 100%;">
    <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
        Name:<br /><input type="text" name="name" value="<? echo stripslashes($data['name']); ?>" /><br />
        Part/Accessory Number:<br /><input type="text" name="part_number" value="<? echo stripslashes($data['part_number']); ?>" /><br />
        Keywords:<br /><input type="text" name="keywords" value="<? echo stripslashes($data['keywords']); ?>" /><br />
        Price:<br /><input type="text" name="price" value="<? echo stripslashes($data['price']); ?>" /><br /><br />
        Shipping:<br /><input type="text" name="shipping" value="<? echo stripslashes($data['shipping']); ?>" /><br />
        Quantity:<br /><input type="text" name="quantity" value="<? echo stripslashes($data['quantity']); ?>" /><br />
        Description:<br />
        <div id="myControls">
        <input type="button" name="bold" title="<b></b>" value="b"/>
        <input type="button" name="italic" title="<i></i>" value="i"/>
        <input type="button" name="underline" title="<u></u>" value="u"/>
        <input type="button" name="quotes" title="<q></q>" value="quotes"/>
        <input type="button" name="url" title='<a href="url goes in here">link name here</a>' value="url"/>
        </div>
        <div style="border: dotted 1px #666; width: 500px; height: 300px;">
        <textarea id="myArea" rows="15" cols="50" style="border: none; margin: 10px;" name="description"><? echo nl2br(stripslashes($data['description'])); ?></textarea><br /><br />
        </div><br />
        <input id="clearSelection" type="button" value="Clear Description" /><br /><br />
        <input type="hidden" name="APC_UPLOAD_PROGRESS" id="progress_key" value="<?php echo $up_id; ?>"/><br />
        <input type="submit" value="Edit Part/Accessory" />
    </form>
    </div>
    Thanks for your time and help, if you need to see any other code just let me know. Any help is greatly appreciated as I've been trying to figure this out for a while now.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    Can you run an echo or such of how your query string builds itself after filling in the variables? Is this the string your intending on seeing? Or is something missing once you print it to screen?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    This string of code works great when there are values that were already inputed into all the fields that available to be edited and updated. But when there is a value entered such as 0 or just plain empty value inserted, and then I go back to edit that field with the 0 in it, the whole form won't submit. So maybe I am missing something but also could it be that no 0 values should be entered into the db to begin with? Maybe just change a 0 value to a word or other number before submission? Or could it be that there is something wrong with my code, I don't really know but I do know that it works for editing values that aren't null and doesn't work if a value is null. I'm not a php expert but I'm trying to teach myself along the way and any advice or help I can get really helps me to prevent from making the same mistake again and to act as a reference for myself if I forget how to do something in php. I'm sure there is a way to edit a 0 value or null value but I really don't know how, I've been trying different things and the only thing I can think of is to change the 0 value to a word so it doesn't make it null. Thanks for your time and help
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    Post an output of an UPDATE statement before you run it. Let's see how it looks before it hits MySQL.
  12. #7
  13. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    You say "the whole form won't submit." What does that sentence mean? Nothing happens when you click the button? The page reloads and is blank? The page reloads and appears to work but the database isn't updated? The computer catches fire? What exactly is happening?
    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.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    Ok how would I do that if you don't mind me asking Triple_Nothing?
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    The page just reloads and the values in the db don't change at all ManiacDan
  18. #10
  19. 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
    Ok how would I do that if you don't mind me asking Triple_Nothing?
    Right before or after your query:
    PHP Code:
    print "UPDATE `parts` SET `name`='$name', `part_number`='$part_number', `price`='$price', `shipping`='$shipping', `quantity`='$quantity', `description`='$description', `keywords`='$keywords' WHERE `id`='$id'\n"
    And see what's printed out.

    You might also change your column to be decimal 10,2. I don't think it'll change the behavior, but that's better than a float for storing monetary values.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    ok thanks i'll try that right now and post back here with my results
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    In your function, comment out the mysql_query. Then above/below it, write an echo with the items inside your mysql_query...

    PHP Code:
    //mysql_query("UPDATE `parts` SET `name`='$name', `part_number`='$part_number', `price`='$price', `shipping`='$shipping', `quantity`='$quantity', `description`='$description', `keywords`='$keywords' WHERE `id`='$id'");
    echo "UPDATE `parts` SET `name`='$name', `part_number`='$part_number', `price`='$price', `shipping`='$shipping', `quantity`='$quantity', `description`='$description', `keywords`='$keywords' WHERE `id`='$id'"
    Then, since content is now output, u should be able to skip this step, but just in case, comment out your header...

    PHP Code:
    //header('Location: addpart.php'); 
    EDIT: demittner beat me too it...
    Last edited by Triple_Nothing; July 2nd, 2013 at 11:32 AM.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    Ok so I think it has something to do with the check where if there are any empty fields an error message pops up saying that all fields are required, because when printing out the results it shows that the updates went through and if putting a 0 the updates are not updated and when I took out the check to see if there were empty fields if I put a 0 it would change the db value to 0 and when changing it to something else it worked fine. so thats what the issue was I think. Thanks for your help!
  26. #14
  27. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    That was quite a sentence in your last post.

    empty() uses PHP's loosely typed comparisons. Zero, empty strings, null, and empty arrays/objects will all register as "empty."
    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.
  28. #15
  29. 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 ManiacDan
    That was quite a sentence in your last post.

    empty() uses PHP's loosely typed comparisons. Zero, empty strings, null, and empty arrays/objects will all register as "empty."
    Just FYI:
    5.0.0 Objects with no properties are no longer considered empty.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo