PDA

View Full Version : Error: undefined index



sahilmohile15
03-31-2016, 02:24 AM
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'].'/**/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

DyDr
03-31-2016, 11:56 AM
You need to find out why you are getting an error in order to fix what's causing it. Just adding code to make the error go away won't make the code work.

What does the 'featured' link look like? Does it contain an id=xxxx element?

Also, since you are altering data on the server, you should be using a method='post' form to submit the data, not a link, and the post method php processing code should be at the top of your file, before any code that's responsible for producing output on the page, so that any updated value will appear when the page gets (re)displayed.

Your code has some other problems, that will be partially fixed by rearranging it as I suggested. You are fetching the first row from the SELECT query result set before the start of your while(){} loop. This will mean that the first row won't be displayed. Your UPDATE query should be using the $id variable, not $product['id'].