Reputation: 51
I am working on a delete a Brand query. If the Brand has any products in it it will not delete else the brand will get deleted. When working on a Brand without product, it is getting deleted but when working with Brand with product it shows the error message that it cannot be deleted but the data related to that brand on the delete page is not showing after I click on delete button. All variables is not getting printed or echoing anything, just the error message is there with Delete button and other HTML code.
<?php
require_once('../auth.php');
require_once('../constants.php');
$errors = array();
$id = $_GET['id'];
$select = mysqli_query($conn,"SELECT * FROM brands Where `id` = '".$id."'");
$row = mysqli_fetch_array($select);
$brandname = $row['brand_name'];
if(!isset($_GET['id']))
{
header("Location: http://localhost/php/brands/index.php");
}
if(isset($_POST['submit'])){
$selectProducts = mysqli_query($conn,"SELECT * FROM `products` Where `brand_id` = '".$id."'");
$select = mysqli_query($conn,"SELECT * FROM brands Where `id` = '".$id."'");
$selectrow = mysqli_fetch_array($select);
$brandName = $selectrow['brand_name'];
if($row = mysqli_num_rows($selectProducts) > 0)
{
$errors[] = "Cannot Delete this brand, it has products in it.";
}
else{
$id = $_GET['id'];
$sql ="DELETE FROM `brands` WHERE `brands`.`id` = '".$id."'";
$result = mysqli_query($conn,$sql) or die(mysqli_error($conn));
if($result){
echo $msg = $id." is deleted.";
header("Location: http://localhost/wsp/php/brands/index.php");
}
else{
$msg = $id." cannot be deleted.";
}
}
}
?>
the form is
<form method="post" action="delete.php?id=<?php echo $id; ?>">
<h2 class="panel-title">Delete <?php echo $brandname; ?>?</h2>
<div class="messages">
<?php if($errors){
foreach ($errors as $key => $value) {
echo '<div class="alert alert-warning" role="alert">
<i class="glyphicon glyphicon-exclamation-sign"></i>
'.$value.'</div>';
}
}?>
</div>
<img src="<?php echo ASSETS;?><?php echo $row['brand_logo']; ?>" style="max-width:195px"/>
<h2><?php echo strtoupper($row['brand_name']); ?></h2>
<br><?php echo $row['brand_type']; ?><br><?php echo $row['status']; ?>
<button type="submit" id="submit" name="submit" class="btn btn-danger">Delete</button>
<button name="reset" type="reset" class="btn btn-default">Cancel</button>
</form>
I would like to know why all variables in $row[''] are showing empty after the query says it cannot delete the brand? Why is it not showing variables?
Upvotes: 0
Views: 39
Reputation: 33533
There is lot of (bad) stuff happening in your code, but I think your problem boils down to this:
if ($row = mysqli_num_rows($selectProducts) > 0)
There, you assign true
or false
to $row
, instead of the row data. You should change that into something like
if (mysqli_num_rows($selectProducts) > 0) {
} else {
$row = mysqli_fetch_array($selectProducts);
}
On the bad stuff:
.
operator.Upvotes: 1