繁体   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