0

Hello guys, Here i am having another error.
Error is undefined index on my products.php page i tried everything i can to define it but if i define it code doesn't work
so here's the code
products.php:

<?php
    require_once $_SERVER['DOCUMENT_ROOT'].'/Online Store/core/init.php';           //Including database path stored in init.php
    include 'includes/head.php';                                                    //Including header
    include 'includes/navigation.php';                                              //Including Navigation bar

    $sql = "SELECT * FROM products WHERE deleted = 0";
    $presults = $db->query($sql);
    $product = mysqli_fetch_assoc($presults);

    // Featured product
    if (isset($_GET['featured'])) {
        $id = (int)$_GET['id'];                    //this where i am getting error
        $featured = (int)$_GET['featured'];
        $featuredSql = "UPDATE `products` SET `featured` = '$featured' WHERE `products`.`id` = '$product[id]' ";
        $db->query($featuredSql);

    }
?>

<h2 class="text-center">Products</h2><hr />

<table class="table table-bordered table-condensed table-striped">
    <thead>
        <th></th>
        <th>Product</th>
        <th>Price</th>
        <th>Category</th>
        <th>Featured</th>
        <th>Sold</th>
    </thead>
    <tbody>
        <?php while($product = mysqli_fetch_assoc($presults)): ?>
            <tr>
                <td>
                    <a href="products.php?edit=<?= $product['id']; ?>" class="btn btn-xs btn-default"><span class= " glyphicon glyphicon-pencil"></span></a>
                    <a href="products.php?delete=<?= $product['id']; ?>" class="btn btn-xs btn-default"><span class= " glyphicon glyphicon-remove-sign"></span></a>

                </td>
                <td><?= $product['title']; ?></td>
                <td><?= money($product['price']) ?></td>
                <td></td>
                <td><a href="products.php?featured=<?= (($product['featured'] == 0)?'1':'0'); ?>&id =<?= $product['id']; ?>" class=" btn btn-sx btn-default">
                    <span class=" glyphicon glyphicon-<?= (($product['featured'] == 1)?'minus':'plus'); ?>"></span>

                </a>&nbsp <?= (($product['featured'] == 1)?'Featured':''); ?></td>
                <td></td>
            </tr>
        <?php endwhile; ?>   
    </tbody>
</table>

<?php
    include 'includes/footer.php';                                                  //Including footer
?>

And this is how i tried to define it :

// This is 1st try

if (isset($_GET['featured']) && isset($_GET['id'])) {
        $id = (int)$_GET['id'];
        $featured = (int)$_GET['featured'];
        $featuredSql = "UPDATE `products` SET `featured` = '$featured' WHERE `products`.`id` = '$product[id]' ";
        $db->query($featuredSql);

    }


// This is 2nd try
$id = '';
if (isset($_GET['featured'])) {
        $id = (int)$_GET['id'];
        $featured = (int)$_GET['featured'];
        $featuredSql = "UPDATE `products` SET `featured` = '$featured' WHERE `products`.`id` = '$product[id]' ";
        $db->query($featuredSql);

    }

In the first try error disappered but featured didn't update in database
i also tried to echo $id and var_dump$id but that didn't show anything

3
Contributors
7
Replies
37
Views
1 Year
Discussion Span
Last Post by pritaeas
0

In your first attempt the error would have gone away because the code in the loop wasn't getting called.
isset($_GET['id']) failed and so nothing else happened.
Double check the URL parameters you are passing in, id isn't one of them.

-1

I don't find any errors in URL it looks fine to me i am passing id too $product['id'] could you illustrate what you mean

0

Var_dump $_GET['id'] and you'll see it isn't a value.
What is the URL you are calling? Can you at least out the parameters here?

0

var_dump ($_GET ['id']); is giving null and so is the var_dump ($_GET['featured']) but features works fine only id doesn't work and the URL i am calling is checking featured and getting id

<a href="products.php?featured=<?= (($product['featured'] == 0)?'1':'0'); ?>&id =<?= $product['id']; ?>" class=" btn btn-sx btn-default">
                    <span class=" glyphicon glyphicon-<?= (($product['featured'] == 1)?'minus':'plus'); ?>"></span>

                </a>

is something wrong in url and why doesn't id work if featured can. please illustrate what you are saying and thank you in advance

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.