White_Lily Posted October 25, 2012 Share Posted October 25, 2012 Hi, I have been asked to sort out a problem with related pages where the CMS lets you add pages even if there some already set. I have written a little validation where it checks if the page selected has already got related pages on it... however the script skips the validation and goes and inserts the new related pages anyway... if(isset($_POST['addTest'])) { // Get posted values $category = mysql_real_escape_string($_POST['catSel']); $product_category = mysql_real_escape_string($_POST['product_category']); $getMultiple = select("sidebar_featured_pages", "*", "category = '$category'"); $check = mysql_fetch_assoc($getMultiple); if($category == $check["catSel"]){ $msg .= generateMsg("Sidebar Pages creation failed, the page you selected already has pages set."); }else{ $addTest = insert("sidebar_featured_pages", array("", $category, $product_category)); if($addTest){ $msg .= generateMsg("Sidebar Pages created successfully.", "success"); }else{ $msg .= generateMsg("Sidebar Pages creation failed, unable to access the database."); } } } Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/269892-comparing-values/ Share on other sites More sharing options...
trq Posted October 25, 2012 Share Posted October 25, 2012 Why aren't you using a where clause in your initial select query in the first place? Quote Link to comment https://forums.phpfreaks.com/topic/269892-comparing-values/#findComment-1387637 Share on other sites More sharing options...
White_Lily Posted October 25, 2012 Author Share Posted October 25, 2012 select() is custom function: function select($table, $rows = "*", $where = NULL, $order = NULL, $limit = NULL, $offset = NULL) { $queryString = "SELECT ".$rows." FROM ".$table; if($where != null) $queryString .= " WHERE ".$where; if($order != null) $queryString .= " ORDER BY ".$order; if($limit != null) $queryString .= " LIMIT ".$limit; if($offset != null) $queryString .= " OFFSET ".$offset; $query = mysql_query($queryString); // if query is successful if($query) return $query; else return false; } Quote Link to comment https://forums.phpfreaks.com/topic/269892-comparing-values/#findComment-1387641 Share on other sites More sharing options...
Barand Posted October 25, 2012 Share Posted October 25, 2012 Are $category and $row['catSel'] string values or numeric? Quote Link to comment https://forums.phpfreaks.com/topic/269892-comparing-values/#findComment-1387645 Share on other sites More sharing options...
White_Lily Posted October 25, 2012 Author Share Posted October 25, 2012 they are numeric Quote Link to comment https://forums.phpfreaks.com/topic/269892-comparing-values/#findComment-1387649 Share on other sites More sharing options...
PFMaBiSmAd Posted October 25, 2012 Share Posted October 25, 2012 I'm going to guess your `catSel` column and your `category` column don't hold the same value. That's what your query and your php logic are testing for. Quote Link to comment https://forums.phpfreaks.com/topic/269892-comparing-values/#findComment-1387661 Share on other sites More sharing options...
White_Lily Posted October 25, 2012 Author Share Posted October 25, 2012 Basically if the value of say... 10 is in catSel AND category, then I don't want them to be able to have related pages, but if the value isn't equal then they can have related pages. It's like saying if someone registers to a website and they pick a username that has already been taken, then you wouldn't want them to have that username you would want them to pick something different - that's something similar to what i am trying to achieve. Quote Link to comment https://forums.phpfreaks.com/topic/269892-comparing-values/#findComment-1387662 Share on other sites More sharing options...
Barand Posted October 25, 2012 Share Posted October 25, 2012 You don't need $category = mysql_real_escape_string($_POST['catSel']); for a numeric input. Use $category = intval($_POST['catSel']) Quote Link to comment https://forums.phpfreaks.com/topic/269892-comparing-values/#findComment-1387715 Share on other sites More sharing options...
White_Lily Posted October 26, 2012 Author Share Posted October 26, 2012 That doesn't solve my issue though - it's still skipping it. Quote Link to comment https://forums.phpfreaks.com/topic/269892-comparing-values/#findComment-1387849 Share on other sites More sharing options...
Barand Posted October 26, 2012 Share Posted October 26, 2012 I'm going to guess your `catSel` column and your `category` column don't hold the same value. That's what your query and your php logic are testing for. Have you tried var_dump() on those two values to check what's in them? Quote Link to comment https://forums.phpfreaks.com/topic/269892-comparing-values/#findComment-1387855 Share on other sites More sharing options...
White_Lily Posted October 26, 2012 Author Share Posted October 26, 2012 yeah i have and they have returned the correct values (I checked them against the database). I really can't figure out what's wrong with it. Quote Link to comment https://forums.phpfreaks.com/topic/269892-comparing-values/#findComment-1387856 Share on other sites More sharing options...
White_Lily Posted October 26, 2012 Author Share Posted October 26, 2012 Right, I managed to find something that has finally worked!! The code that gets the id's that have pages already set: $allRel = select("sidebar_featured_pages", "*"); while($allRelRes = mysql_fetch_array($allRel)){ $usedCats[$allRelRes["category"]] = true; } The code that displays categories that haven't been used: <select name="catSel" style="width: 300px;"> <? $getCategories = select("pages", "id, name", "(page_switch = 1) AND (type = 2 OR type = 4 OR type = 5)"); while ($categories = mysql_fetch_array($getCategories)) { if(!isset($usedCats[$categories['id']])){ echo "<option value='".$categories['id']."'>".$categories['name']."</option>"; } } ?> </select> Thank for the help you all gave! x Quote Link to comment https://forums.phpfreaks.com/topic/269892-comparing-values/#findComment-1387858 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.