Vishnu R Nair
Vishnu R Nair

Reputation: 317

My Code is not working as I expected

I am creating a web application to add products to corresponding categories. I am able to add the products to the corresponding category . I have two pages one page is for adding the product to the category(It's a design page) and I am passing the value using form to another page.

my first page is ,

 <?php include('secondpage.php') ?>
 <form action="secondpage.php" method="POST" enctype="multipart/form-data">
 <div class="div_style" style="position:relative; top:3cm;">
 <div class="form-group" >
  <label for="Product" class="label_style" >Product Name</label>
  <input type="text" class="form-control input_style" id="pdt_name" name="pdt_name" placeholder="Enter product name">
</div>
<div class="form-group" >
<label for="message" class="label_style" ></label>
<p style="color:#009933" id="msg"><?php 

$objs   = new category;
echo $objs->add_products();  
?></p>
</div>
<div class="submit_class"> <button type="submit" class="btn btn-default" name="pdt_add_btn" id="pdt_add_btn">Add Product</button></div>
</div>
</form>

and this is my second page ,

 class category extends db_connection {
  function add_products() {
    if (isset($_POST['pdt_add_btn'])) {
        if (!empty($_POST['pdt_name'])) {
            $pdt_name   = $_POST['pdt_name'];
            $cat_name   = $_POST['sel_cat'];
            $f_size     = ceil($_FILES['images']['size'] / 1024);
            if ($f_size < 200) {
                $image      = file_get_contents($_FILES['images']['tmp_name']);
            } else {
                echo "Please select an image size upto 200kb";  
            }
            $price      = $_POST['price'];
            $con        = $this->db_con();  
            $ins_pdt    = $con->prepare("insert into products (pdt_name,cat_name,image,Price) values(?,?,?,?)");
            $exe_ins    = $ins_pdt->execute(array($pdt_name,$cat_name,$image,$price));
            if ($exe_ins) {
                header("location:product_add.php");
                return $pdt_msg = "Product $pdt_name has been added to category $cat_name"; 
            }
        }
    }
  } 
}

Every thing works fine but , my problem is that

echo $objs->add_products(); not returning anything from the second page .

return empty result even though the condition true . Any help will be really appreciated.

Upvotes: 3

Views: 95

Answers (1)

jeroen
jeroen

Reputation: 91792

On success, you are using a header redirect to redirect to another page:

header("location:product_add.php");

So you will never see any output that you generate on the second page.

You need to remove the redirect or add a parameter to it so that you can show a message there.

So something like:

if ($exe_ins) {
    return "Product $pdt_name has been added to category $cat_name"; 
}

or:

if ($exe_ins) {
     header("location:product_add.php?message=success");
     exit;
}

Edit: To pass your message using the query string (if it is not too long of course...):

if ($exe_ins) {
     $msg = "Product $pdt_name has been added to category $cat_name";
     header("location:product_add.php?message=" . urlencode($msg));
     exit;
}

Note that you need to encode the message correctly to avoid getting only part of it if it contains for example a & character.

Upvotes: 4

Related Questions