簡體   English   中英

PHP動態頁面不起作用

[英]PHP dynamic page not working

我有一個從數據庫中獲取SKU並創建頁面的頁面。 示例網址: http//example.com/index.php?sku = 1234567

當我加載這樣的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)) {

並刪除關閉} ,它可以工作,但不顯示任何數據 - 只是表格。 我不確定這里發生了什么。

簡單。 你的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();
}

(作為旁注,請使用參數化查詢,您現在可以自己打開SQL注入.MySQLi對此沒有魔力,您仍需要驗證/清理輸入。)

顯示故障時的mysqli錯誤:

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

把$ 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>';
    }
    ?>

我已經設法解決了我不得不從mysqli中刪除i的問題,但我在另一個站點上使用了相同的代碼,因此可能與服務器或數據庫有關。 這是代碼雖然'

<?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>';
}
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM