Get Function using Characters rather than numbers

Discussion in 'PHP' started by MattBOnline, Jul 21, 2011.

  1. #1
    Hi,

    I am changing URL links from www.site.com/product.php?product=1

    to www.site.com/product.php?product=productname

    I am aware that now I am using characters rather than numbers the get function needs altering - so I have added " and ' to the get function to account for the problems in the Select Query below.

    $prod_info = mysql_fetch_array(mysql_query("SELECT * FROM products WHERE productid = '".$_GET['product']."'"));


    My problem is with:

    Switch($_GET["action"])
    {
    case "add_item":
    {
    AddItem($_GET["product"], $_GET["quantity"]);
    ShowCart();
    break;
    }
    case "update_item":
    {
    UpdateItem($_GET["product"], $_GET["quantity"]);
    ShowCart();
    break;
    }
    case "remove_item":
    {
    RemoveItem($_GET["product"]);
    ShowCart();
    break;
    }
    default:
    {
    ShowCart();
    }
    }

    How should I alter these get functions? If I add the same ' and " as i did with the Select Query this produces an error. Obviously, when not in a Select Query Characters affect the get function differently but I do not know how!

    Does anyone know what code i should be using now that i have changed from numbers to characters?

    Matt.
     
    MattBOnline, Jul 21, 2011 IP
  2. nomanic

    nomanic Member

    Messages:
    5
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    38
    #2
    it depends entirely on your functions RemoveItem, UpdateItem and AddItem, these functions are expecting numbers so you have to rewrite them aswell.
     
    nomanic, Jul 21, 2011 IP
  3. dthoai

    dthoai Member

    Messages:
    106
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    38
    #3
    You write a function productName2Id. At begining of page, add following line:

    
    $_GET["product"] = productName2Id($_GET["product"]);
    
    Code (markup):
    productName2Id can be:

    
    function productName2Id($name) {
      $row = mysql_fetch_array(mysql_query("SELECT productid FROM products WHERE productname = '".$name."'"));
      return $row['productid'];
    }
    
    Code (markup):
     
    dthoai, Jul 22, 2011 IP
  4. MattBOnline

    MattBOnline Member

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #4
    I know they need rewriting. But if I simply change them to '".$_GET['product']."'" it does not work. Why is it different? And what code should I use because I do not know!

    Matt.
     
    MattBOnline, Jul 22, 2011 IP
  5. dthoai

    dthoai Member

    Messages:
    106
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    38
    #5
    At the begining of page, you change $_GET['product'] from product name to product id, so you don't have to change any codes.
     
    dthoai, Jul 22, 2011 IP
  6. MattBOnline

    MattBOnline Member

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #6
    dthoai,

    How does $_GET["product"] relate to $name?

    And with the code '".$name."'" are you implying that editing the code for characters is best done within Select Queries? ie. In my switch statement above you would not worry about the characters there, but, instead, account for them in the select query?

    Thanks,

    Matt.
     
    MattBOnline, Jul 22, 2011 IP
  7. dthoai

    dthoai Member

    Messages:
    106
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    38
    #7
    The problem you have faced with is current code see $_GET['product'] as product id but $_GET['product'] you receive from url is product name now. The easiest way to fix this problem is replacing $_GET['product'] by value of product id at begining of page. That's reason of code line:
     
    dthoai, Jul 22, 2011 IP
  8. MattBOnline

    MattBOnline Member

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #8
    OK - But if you have the id why do you write '".$name."'" in your second bit of code? If I have got the ID instead of a character-based name then I would not need to use the extra ' and " in this bit of code. Is there a good reason why you are using the " and ' as well as getting the ID instead?

    Matt.
     
    MattBOnline, Jul 22, 2011 IP
  9. dthoai

    dthoai Member

    Messages:
    106
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    38
    #9
    In my second bit of code, $name is value of product name, not product id.
     
    dthoai, Jul 22, 2011 IP
  10. MattBOnline

    MattBOnline Member

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #10
    dthoai,

    Are you suggesting this method because it is impossible to edit the Get Functions in my Swtich Cases above in my first post? As I said I successfully edited the Get Function within a Select Query but the same same editing did not work in my Switch Cases.

    I am guessing the editing only works with Select Queries. Is this true?

    Matt.
     
    MattBOnline, Jul 25, 2011 IP
  11. ketting00

    ketting00 Well-Known Member

    Messages:
    782
    Likes Received:
    28
    Best Answers:
    3
    Trophy Points:
    128
    #11
    $productid = $_GET['product'];
    $prod_info = mysql_fetch_array(mysql_query("SELECT productid, productname FROM products WHERE productid = '$productid'"));
    Code (markup):
    then
     $row['productname'] = $productname;
    Code (markup):
    and then
    <?php echo "<a href='www.site.com/product.php?product=" .$productname. "'>$productname</a>"; ?>
    Code (markup):
     
    ketting00, Jul 25, 2011 IP
  12. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #12
    if you need a quick fix without changing every code all the way through from the product chosen all the way through into the SQL and database code , just use a handler file .txt
    and have a conversion line of code in your page to grab the handler file.
    and covert the product name to number.

    Or long form it to covert the name to number
    $product_name = $_GET['product_name'];
    if($product_name == "Tshirt") {
    $product_num = "1";
    }
    elseif($product_name = "Pants") {
    $product_num = "2";
    }
    elseif($product_name = "Shorts") {
    $product_num = "3";
    }
    else ($product_name = NULL){
    $product_name = NULL;
    }that way all your code to database stays the same, and youre converting before its sent.
     
    Last edited: Jul 25, 2011
    ezprint2008, Jul 25, 2011 IP
  13. MattBOnline

    MattBOnline Member

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #13
    dthoai seems to have the best answer. ie. get the product id (numbers) for the product name (characters) and work with that instead. I am suprised no-one knows how to use the Get Function when using characters! I have got it working in a Select Query but not in my Switch Cases (see first post above).

    Can someone confirm it is not possible to use the Get Function with my Switch Cases above (when using characters - i have it working with numbers only), please?

    Matt.
     
    MattBOnline, Jul 26, 2011 IP