Why isn't this simple PHP/MySQL code working?
Posted
by Sammy
on Stack Overflow
See other posts from Stack Overflow
or by Sammy
Published on 2010-03-28T02:47:47Z
Indexed on
2010/03/28
2:53 UTC
Read the original article
Hit count: 329
I am very new to php/mysql and this is causing me to loose hairs, I am trying to build a multi level site navigation. In this part of my script I am readying the sub and parent categories coming from a form for insertion into the database:
// get child categories
$catFields = $_POST['categories'];
if (is_array($catFields)) {
$categories = $categories;
for ($i=0; $i<count($catFields); $i++) {
$categories = $categories . $catFields[$i]";
}
}
// get parent category
$select = mysql_query ("SELECT parent FROM categories WHERE id = $categories");
while ($return = mysql_fetch_assoc($select)) {
$parentId = $return['parent'];
}
The first part of my script works fine, it grabs all the categories that the user has chosen to assign a post by checking the checkboxes in a form and readies it for insertion into the database.
But the second part does not work and I can't understand why. I am trying to match a category with a parent that is stored in it's own table, but it returns nothing even though the categories all have parents. Can anyone tell me why this is?
p.s. The $categories
variable contains the sub category id.
© Stack Overflow or respective owner