2013-07-17 32 views
0

我有一個從數據庫獲取SKU並創建頁面的頁面。示例網址:http://example.com/index.php?sku=1234567PHP動態頁面無法正常工作

當我加載一個這樣的URL時,它什麼都不顯示 - 甚至連我用echo輸出的表都沒有。下面是我的代碼:

$sku = $_GET['sku']; 
$result = mysqli_query($conn, "SELECT productname, price, producturl, productimg, productdesc, sku FROM table WHERE sku=" . $sku); 
while ($row = mysqli_fetch_array($result)) { 
echo '<h3>test</h3>'; 

      echo '<table width="100%" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td><h4>'.$row["sku"].'</h4></td> 
    <td><h3>'.$row["productname"].'</h3></td> 
    <td rowspan="2"><img src="'.$row["productimg"].'" width="100%" alt="productimg"/></td> 
    </tr> 
    <tr> 
    <td colspan="2" rowspan="2"><p>'.$row["productdesc"].'</p></td> 
    </tr> 
    <tr> 
    <td><a class="button" href="'.$row["producturl"].'">View Product</a> <a class="alert button" href="">No Match</a> <a class="alert button" href="">Match</a></td> 
    </tr> 
</table>'; 
} 

我已經連接到我的數據庫,並具有<?php?>標籤在那裏。 - 只是表

while ($row = mysqli_fetch_array($result)) { 

,也拆除收盤},它的工作原理,但不顯示任何數據:我雖然玩了,如果我刪除此行注意。我不確定這裏發生了什麼事。

+0

你可以試試這個: $結果= mysqli_query($康恩,「選擇產品名稱,價格,PRODUCTURL,productimg,productdesc,SKU FROM表WHERE SKU = '$ SKU'); – Maximus2012

+0

echo mysqli_num_rows($ result)。「記錄發現...「; – steven

+2

您的代碼易受** SQL注入**,請閱讀此:http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php – Mansfield

回答

2

簡單。您的mysqli_query調用不會返回任何記錄。沒有找到記錄,或者有錯誤。讓你的代碼更健壯一點。

$sku = $_GET['sku']; 
if ($result = mysqli_query($conn, ...)) { 
    if (mysqli_num_rows($result) == 0) { 
     echo "no skus found"; 
    } else { 
     while ($row = mysqli_fetch_array($result)) { 
      echo '<h3>test</h3>'; 
      ... 
     } 
    } 
} else { 
    echo "something went wrong: ".mysqli_error(); 
} 

(作爲一個方面說明,請使用parametrised查詢,您要打開自己到現在SQL注入。MySQLi的是沒有靈丹妙藥針對這一點,你還是要驗證/淨化輸入。)

+0

您混合了面向對象的風格和程序風格 – steven

+0

@steven感謝您的注意;固定的。 –

+0

嗨使用你的代碼似乎沒有做任何事情 –

0

在故障時顯示mysqli錯誤:

if (!mysqli_query($link, $sql)) { 
    printf("Errormessage: %s\n", mysqli_error($link)); 
} 
+0

這是一個好主意,但它不能解決問題。它只會提供有關該問題的更多信息。 – pattyd

0

將$ sku放在引號內。

<?php 
    $sku = $_GET['sku']; 
    $result = mysqli_query($conn, "SELECT productname, price, producturl, productimg, productdesc, sku FROM table WHERE sku = $sku"); 
    while ($row = mysqli_fetch_array($result)) { 
    echo '<h3>test</h3> 
<table width="100%" border="0" cellspacing="0" cellpadding="0"> 
     <tr> 
     <td><h4>'.$row["sku"].'</h4></td> 
     <td><h3>'.$row["productname"].'</h3></td> 
     <td rowspan="2"><img src="'.$row["productimg"].'" width="100%" alt="productimg"/></td> 
     </tr> 
     <tr> 
     <td colspan="2" rowspan="2"><p>'.$row["productdesc"].'</p></td> 
     </tr> 
     <tr> 
     <td><a class="button" href="'.$row["producturl"].'">View Product</a> <a class="alert button" href="">No Match</a> <a class="alert button" href="">Match</a></td> 
     </tr> 
    </table>'; 
    } 
    ?> 
0

我已成功地解決了,我一直有,我不得不刪除從庫MySQLi的我的問題,但我已經使用了同一段代碼在另一個網站,所以它可能是與服務器或數據庫也許。這裏是代碼雖然」

<?php 
    $sku = $_GET['sku']; 
     $objConnect = mysql_connect("host address","username","password") or die(mysql_error() . 'this is true death...'); 
    $objDB = mysql_select_db("database"); 
    $result = 'SELECT sku, productname, price, producturl, productimg, productdesc FROM table1 WHERE sku="' . $sku . '"'; 
$result = mysql_query($result); 
while ($row = mysql_fetch_array($result)) { 
    echo '<table width="100%" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td><h4>'.$row["sku"].'</h4></td> 
    <td><h3>'.$row["productname"].'</h3></td> 
    <td rowspan="2" width="30%"><img src="'.$row["productimg"].'" width="100%" alt="productimg"/></td> 
    </tr> 
    <tr> 
    <td colspan="2" rowspan="2"><p>'.$row["productdesc"].'</p></td> 
    </tr> 
    <tr> 
    <td><a class="button" href="'.$row["producturl"].'">View Product</a> <a class="alert button" href="">No Match</a> <a class="alert button" href="">Match</a></td> 
    </tr> 
</table>'; 
} 
?>