Page 1 of 1

PHP Error - Willing to pay

Posted: Tue Mar 03, 2009 3:34 pm
by srednatonmi
I'm having a problem with the PHP on one of my sites. The site is sustainabilitynh.com and the piece of code that isnt working is supposed to allow an admin to add a featured product from the database of products.

Here is the error:
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in D:\Html\Users\userweb10977\Html\admin\products\featuredproducts\add.php on line 15
And here is the code:

Code: Select all

<?php
if (!isset($connection)) {
    exit;
}
 
$description = '';
$product = '';
$errors = array();
 
$query = "SELECT * FROM `products` ORDER BY `product_id`, `id` ASC";
$result = mysql_query($query);
$products = array();
$validProducts = array();
 
while ($data = mysql_fetch_object($result)) {
    $products[] = $data;
    $validProducts[] = $data->id;
}
 
if (!empty($_POST)) {
    $product = isset($_POST['product']) ? (int)$_POST['product'] : 0;
    $description = isset($_POST['description']) ? $_POST['description'] : '';
    
    if (!empty($_POST['product']) && !empty($_POST['description'])) {
        if (!in_array($product, $validProducts)) {
            $errors[] = 'An invalid product has been chosen.';
        } else {
            $query = "INSERT INTO `featured_products` SET `description` = '"
                   . mysql_real_escape_string($description)
                   . "', `product_id` = "
                   . (int)$product;
            mysql_query($query);
            echo 'Featured product successfully added.';
            $success = true;
        }
    } else {
        if (empty($description)) {
            $errors[] = 'The description is empty.';
        }
        
        if (empty($_POST['product'])) {
            $errors[] = 'The product was not given.';
        }
    }
}
 
if (!isset($success)) {
    $errors = buildErrors($errors);
    $categories = array();
    $subcategories = array();
    $productDropdown = '';
    
    foreach ($products as $product) {
        if (!isset($subcategories[$product->subcategory_id])) {
            $query = "SELECT * FROM `product_subcategories` WHERE `id` = "
                   . (int)$product->subcategory_id;
            $result = mysql_query($query, $connection);
            $subcategories[$product->subcategory_id] = mysql_fetch_object($result);
        }
        
        if (!isset($categories[$subcategories[$product->subcategory_id]->category_id])) {
            $query = "SELECT * FROM `product_categories` WHERE `id` = "
                   . (int)$subcategories[$product->subcategory_id]->category_id;
            $result = mysql_query($query, $connection);
            $categories[$subcategories[$product->subcategory_id]->category_id] = mysql_fetch_object($result);
        }
        
        $productDropdown .= '<option value="'
                          . $product->id
                          . '">'
                          . $categories[$subcategories[$product->subcategory_id]->category_id]->title
                          . ' &raquo; '
                          . $subcategories[$product->subcategory_id]->title
                          . ' &raquo; '
                          . $product->title
                          . '</option>';
    }
    
    echo <<<FORM
<form action="#" method="post">
    {$errors}
    <h3>Add a new featured product</h3>
    <label for="product">Product</label>
    <select name="product">
        {$productDropdown}
    </select>
    <label for="description">Description</label>
    <textarea name="description">{$description}</textarea>
    <button type="submit">Add</button>
</form>
FORM;
}

Re: PHP Error - Willing to pay

Posted: Tue Mar 03, 2009 3:40 pm
by user333
Insert this

Code: Select all

 
$result = mysql_query($query) or die(mysql_error());
 
instead of that

Code: Select all

 
$result = mysql_query($query);
 
and tell us what is printed?

Re: PHP Error - Willing to pay

Posted: Tue Mar 03, 2009 3:46 pm
by srednatonmi
Unknown column 'product_id' in 'order clause'
Is my new error.

I had this whole thing developed about a year ago and all of it is functional except for this adding featured product option.

Re: PHP Error - Willing to pay

Posted: Tue Mar 03, 2009 3:50 pm
by semlar
It means there's no column named product_id in the table you're accessing.

You could try just removing the whole "order by" business.

Re: PHP Error - Willing to pay

Posted: Tue Mar 03, 2009 3:56 pm
by srednatonmi
Can you tell me more or can I hire you to do this for me? I don't have the PHP knowledge to really understand this problem enough to fix it.