我正在创建一个 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";
}
}
}
}
}
一切正常,但我的问题是
最佳答案
成功后,您将使用 header 重定向重定向到另一个页面:
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;
}
请注意,如果消息包含例如
&
字符,则您需要正确编码消息以避免仅获取其中的一部分。关于php - 我的代码没有按预期工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32371024/