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.
it depends entirely on your functions RemoveItem, UpdateItem and AddItem, these functions are expecting numbers so you have to rewrite them aswell.
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):
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.
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, 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.
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:
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.
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.
$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):
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.
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.