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.
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
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?
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: