2017-02-28 65 views
0

我的類別名稱是我的導航欄。我想在categories.php頁面上顯示「categoryname」,同時點擊任何導航項目。categories.php頁面不是從DATABASE收集數據

  1. 頁面與數據庫連接
  2. 它正在創建動態鏈接
  3. 我檢查的SQL查詢,其工作

//this is categories.php file 
 

 
<?php require ('includes/dbconnect.php');?> 
 
    
 
<?php 
 
if (!isset($_GET['categoryid'])){ 
 

 
header ("location:index.php"); 
 
} 
 

 

 
$cat_sqli ="SELECT products.productid,products.productname,products.unit,products.price,products.brand,products.productdetails,categories.categoryid,categories.categoryname 
 
FROM products 
 
INNER JOIN categories ON (products.categoryid=categories.categoryid) 
 
FROM products 
 
INNER JOIN categories 
 
ON (products.categoryid=categories.categoryid) 
 
WHERE products.categoryid=".$_GET['categoryid']; 
 
if ($cat_query = mysqli_query ($dbconnect,$cat_sqli)){ 
 

 
$cat_rs=mysqli_fetch_assoc($cat_query); 
 

 
} 
 

 
?> 
 

 
<body> 
 
<h1>Selected category | <?php echo $cat_rs['categoryname'];?></h1> 
 

 

 
</body>

+0

要解決這個問題請提供全部代碼與數據庫然後我會檢查本地結束 –

回答

1

scaisEdge答案,我想從SQL注入漏洞修復您的查詢,並使用準備好的語句如下:

//this is categories.php file 

<?php require ('includes/dbconnect.php');?> 

<?php 
if (!isset($_GET['categoryid'])){ 
    header ("location:index.php"); 
} 

$array_of_result = []; 

$cat_sqli ="SELECT 
products.productid 
,products.productname 
,products.unit 
,products.price 
,products.brand 
,products.productdetails 
,categories.categoryid 
,categories.categoryname 
FROM products 
INNER JOIN categories ON products.categoryid=categories.categoryid 
WHERE products.categoryid=?"; 

$stmt = $dbconnect->prepare($cat_sqli); 

if($stmt){ 
    $stmt->bind_param('i', filter_input(INPUT_GET, 'categoryid', FILTER_VALIDATE_INT)); 
    if($stmt->execute()){ 
     $result = $stmt->get_result(); 
     $num_of_rows = $result->num_rows; 

     if($num_of_rows>0){ 
      while ($row = $result->fetch_assoc()) { 
       $array_of_result[] = $row; 
      } 
     } 
     $stmt->free_result(); 
     $stmt->close(); 
    } 
} 
?> 

<body> 
<h1>Selected category | 
<?php 
if(!empty($array_of_result)){ 
    foreach($array_of_result as $r){ 
     echo $r['categoryname']; 
     break; 
    } 
} 
</h1> 
</body> 
+0

其工作..非常感謝#brother – sajid

0

要加入的表兩次這是錯誤的 (刪除秒OND從並加入除條款及條件)

$cat_sqli ="SELECT 
    products.productid 
    ,products.productname 
    ,products.unit 
    ,products.price 
    ,products.brand 
    ,products.productdetails 
    ,categories.categoryid 
    ,categories.categoryname 
FROM products 
INNER JOIN categories ON products.categoryid=categories.categoryid 
WHERE products.categoryid=".$_GET['categoryid'];