函数在 PHP $_GET[] 中不起作用
Function not working in PHP $_GET[]
我正在制作一个小型目录,它将在 SQL 数据库中获取产品列表并将它们列在 catalogue.php 页面上。当您点击某个产品时,您将被带到包含该特定产品的页面。所以我有一个 productbuild.php,它是构建目录和单个产品的 class。单个产品位于 productview.php。我可以在目录页面上查看产品,但不能在产品视图页面上查看。
这是我的 class:
require_once('connect.php');
class Catalogue {
public $ksdb = '';
public function __construct() {
$this->ksdb = new Database;
}
//This function gets all the products on the main catalogue page, it works fine
public function getProds() {
$id = 0;
//This is the prod array that is not being passed
$prod = $return = array();
$querystr = $this->ksdb->db_connect->prepare("SELECT * FROM products");
try {
$querystr->execute();
for($i=0; $row = $querystr->fetch(); $i++) {
$return[$i] = array();
foreach($row as $rowi => $rowitem) {
$return[$i][$rowi] = $rowitem;
}
}
} catch (PDOException $e) {
echo $e->getMessage();
}
return $return;
//$prod = $return;
}
//This is the one that is not working on the productview.php page
public function viewProd($prodID) {
$prod = $return = array();
$querystr = $this->ksdb->db_connect->prepare("SELECT * FROM products WHERE ID= ?");
try {
$querystr->execute(array($id));
for ($i = 0; $row = $querystr->fetch(); $i++) {
$return[$i] = array();
foreach($row as $rowi => $rowitem) {
$return[$i][$rowi] = $rowitem;
}
}
} catch(PDOException $e) {
echo $e->getMessage();
}
$prod = $return;
$prod[0]['Description'] = $prod[0]['Description'];
}
}
这是我的 productview.php 页面:
//Include the class
<?php include_once('/productbuild.php');
//Create an object of catalogue
$viewproduct = new Catalogue;
//if there is something in productview.php?ID= viewProd function is called
if(!empty($_GET['ID'])) {
$prod = $viewproduct->viewProd($_GET['ID']);
}
else {
echo "No Products";
}
?>
//Echo the product name (from the DB) and Description
<h3><?php echo htmlspecialchars($prod['Name']); ?></h3>
<?php echo htmlspecialchars($prod['Description']); ?>
如何更改您的 viewProd 函数
$querystr->execute(array($id));
到
$querystr->execute(array($prodID))
如果这能解决您的问题,请告诉我。
我正在制作一个小型目录,它将在 SQL 数据库中获取产品列表并将它们列在 catalogue.php 页面上。当您点击某个产品时,您将被带到包含该特定产品的页面。所以我有一个 productbuild.php,它是构建目录和单个产品的 class。单个产品位于 productview.php。我可以在目录页面上查看产品,但不能在产品视图页面上查看。
这是我的 class:
require_once('connect.php');
class Catalogue {
public $ksdb = '';
public function __construct() {
$this->ksdb = new Database;
}
//This function gets all the products on the main catalogue page, it works fine
public function getProds() {
$id = 0;
//This is the prod array that is not being passed
$prod = $return = array();
$querystr = $this->ksdb->db_connect->prepare("SELECT * FROM products");
try {
$querystr->execute();
for($i=0; $row = $querystr->fetch(); $i++) {
$return[$i] = array();
foreach($row as $rowi => $rowitem) {
$return[$i][$rowi] = $rowitem;
}
}
} catch (PDOException $e) {
echo $e->getMessage();
}
return $return;
//$prod = $return;
}
//This is the one that is not working on the productview.php page
public function viewProd($prodID) {
$prod = $return = array();
$querystr = $this->ksdb->db_connect->prepare("SELECT * FROM products WHERE ID= ?");
try {
$querystr->execute(array($id));
for ($i = 0; $row = $querystr->fetch(); $i++) {
$return[$i] = array();
foreach($row as $rowi => $rowitem) {
$return[$i][$rowi] = $rowitem;
}
}
} catch(PDOException $e) {
echo $e->getMessage();
}
$prod = $return;
$prod[0]['Description'] = $prod[0]['Description'];
}
}
这是我的 productview.php 页面:
//Include the class
<?php include_once('/productbuild.php');
//Create an object of catalogue
$viewproduct = new Catalogue;
//if there is something in productview.php?ID= viewProd function is called
if(!empty($_GET['ID'])) {
$prod = $viewproduct->viewProd($_GET['ID']);
}
else {
echo "No Products";
}
?>
//Echo the product name (from the DB) and Description
<h3><?php echo htmlspecialchars($prod['Name']); ?></h3>
<?php echo htmlspecialchars($prod['Description']); ?>
如何更改您的 viewProd 函数
$querystr->execute(array($id));
到
$querystr->execute(array($prodID))
如果这能解决您的问题,请告诉我。