我在页面的不同部分的一个页面上有这两个代码块,每当我尝试在代码的另一个部分上使用$product_id
变量时,它就不会被识别。看一看。
<?php
$cart_table = mysql_query("select * from order_details where memberID='$ses_id' and status='Delivered' and addon_status=''") or die(mysql_error());
$cart_count = mysql_num_rows($cart_table);
while ($cart_row = mysql_fetch_array($cart_table)) {
$order_id = $cart_row['orderid'];
$status = $cart_row['status'];
$product_id = $cart_row['productID'];
$orderdate = $cart_row['ordertime'];
//Query Product table
$product_query = mysql_query("select * from tb_products where productID='$product_id'") or die(mysql_error());
$product_row = mysql_fetch_array($product_query);
//Query treat_period table
}
?>
我想在下一个php代码块上使用$product_id
变量,但它根本没有被识别。有人请帮助我。
<?php
require_once'session.php';
$query=mysql_query("SELECT * FROM product_addon WHERE memberid='$ses_id' && productID='$product_id'");
while($row=mysql_fetch_array($query))
{
$sta=$row['treat_kind'];
}
?>
答案 0 :(得分:0)
PHP允许您在整个文件中使用变量。未定义product_id的原因可能是因为您的查询未返回任何结果或product_id为null或空白。
在查询之前尝试:var_dump($ cart_row),看看产品ID是否存在且不为空
答案 1 :(得分:-1)
如果您发布错误的屏幕截图会更好。
我认为会话会解决,也许你可以试试这个:
<?php
session_start();
$cart_table = mysql_query("select * from order_details where memberID='$ses_id' and status='Delivered' and addon_status=''") or die(mysql_error());
$cart_count = mysql_num_rows($cart_table);
while ($cart_row = mysql_fetch_array($cart_table)) {
$order_id = $cart_row['orderid'];
$status = $cart_row['status'];
$product_id = $cart_row['productID'];
$orderdate = $cart_row['ordertime'];
//Query Product table
$product_query = mysql_query("select * from tb_products where productID='$product_id'") or die(mysql_error());
$product_row = mysql_fetch_array($product_query);
//Query treat_period table
}
$_SESSION['productid'] = $product_id;
?>
另一个文件:
<?php
session_start();
require_once'session.php';
if(isset($_SESSION['productid'])) {
$theproductid = $_SESSION['productid'];
unset($_SESSION['productid']);
} else {
// do something if product_id not found
}
$query=mysql_query("SELECT * FROM product_addon WHERE memberid='$ses_id' && productID='".$theproductid."'");
while($row=mysql_fetch_array($query)) {
$sta=$row['treat_kind'];
}
?>