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 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2003
    Location
    Kelowna, British Columbia
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Formatting Strings using strlen() and str_replace() for inserting into MySQL

    Well I'm certainly finding out about strings and form validation... but I'm finding it difficult to learn... I've been looking into classes as you suggested but also finding this difficult to absorb. Before I get into classes more to do formvalidation, I would like to code myself in order to establish a good understanding of them.

    As you suggested I'm using the strlen() to insert a "0" if either formfield for points or cash are left blank and these work great (thank you).

    Then I use the str_replace to replace commas and whitespace out of the user's input for points which works fine if I use this alone, but the minute I add the extra str_replace for the items_cash user input I get an error. I can't see what is so different about it from the points code. I want to do this in the first place in case the user inputs the "$" sign or commas or spaces.

    This is the code I'm trying to work with. I've looked at the functions at PHPnet and see if there is something else I can try but I'm once again getting stumped.

    PHP Code:
    if (strlen($_POST['item_points']) < 1) {

                    
    $item_points 0;

                    } else {

                    
    $item_points strip_tags(trim($_POST['item_points']));

                    }

                    

                if (
    strlen($_POST['item_cash']) < 1) {

                    
    $item_cash 0;

                    } else {

                    
    $item_cash strip_tags(trim($_POST['item_cash']));

                    }

                    

                    
    $markup_points = array(',''.'' ');

                    
    $item_points = (str_replace($markup_points""$item_points));

                    

                    
    $markup_cash = array(',''$'' ');

                    
    $item__cash = (str_replace($markup_cash""$item__cash));

                    

                    
    $item_code strip_tags(trim($item_code));

                    
    $item_desc strip_tags(trim($item_desc));

                    
    $item_desc htmlentities($item_descENT_QUOTES);

            

                    
    $add_item "INSERT INTO prod_items (prod_id, item_code, item_desc, item_points, item_cash)

                                VALUES ("
    .mysql_insert_id().", '$item_code', '$item_desc', $item_points, $item_cash)";

            

                    
    $resultitms mysql_query($add_item) or die ("Couldn't execute query."); 
    Thank you... I'm sorry if it looks like I'm not trying on my own... I've been trying for many, many hours and finding that I just can't seem to figure it out...
    Last edited by percept; 01-11-2004 at 10:43 AM.

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What error do you get?

    I've noticed that you set $item_cash, there is only onde underscrore. When you use it for the str_replace, there are two underscores --> this could be your problem.

    There isn't anything wrong with the replacecode itself (not that i can see anyway).

    Note: this is an all PHP problem so you'd better started this thread in the PHP forum. (don't start a new one for it now. Maybe a mod will move it)

    Note: just to avoid misunderstanding --> you only need to remove the "," or "." if your users know thay should only insert integers !! Like if they registre points or so. If they want to insert 4,000.23 then you should obviously only remove the ","

  • #3
    New Coder
    Join Date
    Dec 2003
    Location
    Kelowna, British Columbia
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks raf... that was exactly the problem (the extra underscore). I can't believe how many hours I spent on this one to find out it was merely this. I am using the SQL tab in PhpMyAdmin to test my queries but in this case my error was syntax and I just got blind to it.

    I do my code in Dreamweaver just because I am used to this program for my web design. Is there a program out there that you could suggest I use to code in and then move my code to Dreamweaver and set it into my templates? I guess I'm wondering if there is such a program that would make it easier to see these mistakes... ie. better debugging.

    Thanks again!

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks raf... that was exactly the problem (the extra underscore). I can't believe how many hours I spent on this one to find out it was merely this. I am using the SQL tab in PhpMyAdmin to test my queries but in this case my error was syntax and I just got blind to it.
    Yeah. We've all been there
    I do my code in Dreamweaver just because I am used to this program for my web design. Is there a program out there that you could suggest I use to code in and then move my code to Dreamweaver and set it into my templates? I guess I'm wondering if there is such a program that would make it easier to see these mistakes... ie. better debugging.
    I think that PHPEdit is quite popular and well received.
    http://www.phpedit.net/products/PHPEdit/

    But i don't think you'll need to copy it to Dreamweaver. If you create your template, then all formatting should be done through CSS. So you only would need to fill in the basic structure of your site. So inside dreamweaver (i supose it has a 'code' tab or so where you can edit the code) you then type in which files to include (at the top of the page), and where the content needs to come, you place some variables.

    Then inside the included files, you have your php, but instead of echoing the HTML, you store it in these variables. (the html should not contain layout --> CSS)

    This way, if you need to change your design, you only need to make sure that you can reuse all variables (or else you need to change the php to reorganise the variables)

    Here's a thread on some other editers
    http://www.codingforums.com/showthre...5&pagenumber=1


    About a debugger that can trap your error. Hmm. I don't know if it exists for PHP, but for ASP, you have <%option explicit%>
    This will check if you declared all variables that you use. When i write ASP, i always include it at the top of my pages, precisely to avoid such error.
    (the real reason to use option exp. is that the parser then immedeately, before starting the scriptexecution, reserves memoryspace for all declared variables. but apparently, PHP parsers don't need/support this)
    It should be doable to write a check like this yourself. I've already played with the idea to write some sort of scriptcleaner/optimizer/compliler but never found the time. Specially fer databasework, there would be a lott of advantages. Imagen for instance that you only need to specify "select * from users" and that the optimizer then checks your code to see which columns are actualy used from the recordset, and rewrites the selectstatement.

    Maybe someone knows of an existing app that does this (the option explicit thing or extra optimization)

    Maybe your best bet would be the Zend-products like Zend Studio but i never used any of them http://www.zend.com/store/products/zend-studio.php

  • #5
    New Coder
    Join Date
    Dec 2003
    Location
    Kelowna, British Columbia
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks raf... I'm seeing what you mean about not having to copy code over to Dreamweaver... I am self-taught all the way with web stuff because my small town doesn't have any schools teaching the stuff. But slowly I'm learning. I learned that using templates really helped in the same matter that if you change a design aspect, you only have to change one file. Now with PHP I can move to a new level and really start to rock and roll. I looked around and I can see that PHPedit is widely used. Thanks again.

    I'm finding that once I started working with formatting strings in PHP that it's not so bad for the basics but after the basics holy wow... I can see that classes will help out quite a bit and so that's on my plate to try and absorb.

  • #6
    New Coder
    Join Date
    Aug 2002
    Location
    PA
    Posts
    93
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i use winsyntax, its free and works great for me


  •  

    Posting Permissions

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