SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2008
    Location
    Bristol ,England
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help with in_array

    Hi i am trying to query a database and retrieve data and see if it matches data entered in the form, if it matches the data it will cause an error but this isnt happening.its not throwing an error when it should.
    Any help would be grateful.
    Code PHP:
    if (isset($_POST['submit'])) {
    		$query_check = "SELECT * FROM database.products";
    		$test = mysql_query($query_check);
    		$rows = mysql_fetch_array($test);
    			if(in_array($_POST['productname'],$rows)){
    			$formerror = true ;
    			}
    			else{
    			$formerror = false ;
     
    			}

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    That is because $rows is a multidimensional array.

    It might just be best to loop through $rows and if you run into the product set the error variable and break. Otherwise you would need to reformat the array then check. Either way you need to loop through $rows, but the former avoids a unnecessary call to in_array().

    Code:
    $formerror = false;
    foreach($rows as $value) {
      if(strcmp($value['name'],$_POST['productname'])==0) {
        $formerror = true;
        break;
      }
    }

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Surely the better solution is to use a where clause in your select statement
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Yeah, a filter is probably the best solution for the intended result.


Bookmarks

Posting Permissions

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