Choose a category for your product-What's wrong with this?...INT...

Discussion in 'PHP' started by Hannaspice, Dec 1, 2008.

  1. #1
    Hi everybody, there are some problems with me in choosing categories.

    After I have submitted 1, 2, 3 in "products" table on database and a, b, c in "categories" table on database. Then in "products" I have a field named "category". It's type is INT. In this field I would like to choose "categories" id. I didnot manage to do this.

    For example: a product belongs to milk category (it's id=1), so in "category" field of table "products", there is 1. Why didnot it work?

    if (isset($_POST['submit'])) {
    		require("./db.php");
    
    $category = $_POST['category'];
    		if ($category = "") $error[] = "You have not chosen CATEGORY";
    
    if (!isset($error)) {
    			
    			$sql = "SELECT * FROM categories ORDER BY catname ASC";
                for($i=0; $i<mysql_num_rows($sql); $i++) {
                 $cat_id = mysql_result($sql, $i, "id");
                 $cat_name = mysql_result($sql, $i, "catname");
                if ($cat_id == $category)
                     $categories_list.= "<option value='$cat_id' selected>".$cat_name."</option>\r\n";
                else
                     $categories_list.= "<option value='$cat_id'>".$cat_name."</option>\r\n";
    			}
    			
    			if (isset($error)) {
    				if (isset($_GET['id'])) {
    
                     $id = $_GET['id'];
    				 
                     $sql = "SELECT * FORM products WHERE `id`=".$id;
    				 $result = dbQuery($sql);
                     $row = dbFetchAssoc($result);
    
                     $category = $row['category'];
    				 
    				 $sql = "SELECT * FROM categories ORDER BY catname ASC";
                     for($i=0; $i<mysql_num_rows($sql); $i++) {
                     $cat_id = mysql_result($sql, $i, "id");
                     $cat_name = mysql_result($sql, $i, "catname");
                       if ($cat_id == $category)
                            $categories_list.= "<option value='$cat_id' selected>".$cat_name."</option>\r\n";
                       else
                            $categories_list.= "<option value='$cat_id'>".$cat_name."</option>\r\n";
                    }
    }
    }
    
    if (isset($_GET['id'])) {
    			           $id = $_GET['id'];
    			
    			           $sql = "UPDATE products SET `category`='".$category."' WHERE `id`=".$id;
    				       $result = dbQuery($sql);
    					   } else{
    		
    		                $sql = "INSERT INTO products VALUES ('$category')";
    		                $result = dbQuery($sql);
    					   }
    
    header("Location: ./new_product.php?success=");
    
    
    } else {
    
    if (isset($_GET['success'])) 
    
    $output.= "<p>Category<br /><select name=\"category\"><option value=\"\">choose...</option>".$categories_list."</select><a href=\"./new_category.php\">New Category</a></p>";
    $output.= "<p><input id=\"submit\" name=\"submit\" type=\"submit\" value=\"Post\" /></p>\n";
    
    print $output;
    
    
    PHP:

    OR Are there any easier mode?Thanks bros.
     
    Hannaspice, Dec 1, 2008 IP
  2. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #2
    can you tell me what is this "dbQuery"... this is your function...?
    cause this is not default function on PHP.
    and for line 10... before you call mysql_num_rows function..., you need mysql_query function.
    CMIIW
     
    misbah, Dec 1, 2008 IP
  3. Hannaspice

    Hannaspice Active Member

    Messages:
    77
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #3
    dbQuery is a function:

    function dbQuery($_sql)
    {
    return mysql_query($_sql);
    }

    then can I use the mysql_num_rows?Or do I have to a function for that?
     
    Hannaspice, Dec 2, 2008 IP
  4. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #4
    look at this line
                 $sql = "SELECT * FROM categories ORDER BY catname ASC";
                 for($i=0; $i<mysql_num_rows($sql); $i++) {
                 $cat_id = mysql_result($sql, $i, "id");
                 $cat_name = mysql_result($sql, $i, "catname");
    PHP:
    replace with
                 $sql = "SELECT * FROM categories ORDER BY catname ASC";
                 $result = dbQuery($sql);
                 for($i=0; $i<mysql_num_rows($result); $i++) {
                 $cat_id = mysql_result($result, $i, "id");
                 $cat_name = mysql_result($result, $i, "catname");
    PHP:
     
    misbah, Dec 2, 2008 IP
  5. Hannaspice

    Hannaspice Active Member

    Messages:
    77
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #5
    I've tried it.

    it doesnot work.

    Huhu,

    anyway, many thanks.
     
    Hannaspice, Dec 2, 2008 IP