我的代码无法正常工作

时间:2015-09-03 08:49:16

标签: php html

我正在创建一个Web应用程序,以将产品添加到相应的类别。 我可以将产品添加到相应的类别。我有两页,一页用于将产品添加到类别(这是一个设计页面),我将使用表单的值传递给另一页。

我的first page是,

 <?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>

这是我的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"; 
            }
        }
    }
  } 
}

每件事情都很好,但我的问题是

  

echo $ objs-&gt; add_products();   没有从第二页返回任何内容。

     

  即使条件为true,也返回空结果。   任何帮助将非常感激。

1 个答案:

答案 0 :(得分:4)

成功时,您使用标题重定向重定向到另一个页面:

header("location:product_add.php");

所以你永远不会看到你在第二页上生成的任何输出。

您需要删除重定向或向其添加参数,以便在那里显示消息。

类似于:

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

或:

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

编辑:使用查询字符串传递您的邮件(如果它不是太长,当然......):

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

请注意,您需要正确对消息进行编码,以避免仅包含部分消息(如果其中包含&个字符。

相关问题