简体   繁体   English

PHP动态页面不起作用

[英]PHP dynamic page not working

I have a page that takes an SKU from a database and creates a page. 我有一个从数据库中获取SKU并创建页面的页面。 Example URL: http://example.com/index.php?sku=1234567 示例网址: http//example.com/index.php?sku = 1234567

When I load a URL like this, it shows nothing - not even the table which I output with echo . 当我加载这样的URL时,它什么也没有显示 - 甚至不是我用echo输出的表。 Below is my code: 以下是我的代码:

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

I have connected to my database and have the <?php and ?> tags in there. 我已连接到我的数据库,并在那里有<?php?>标签。 I have noticed while playing around with it that if I remove this line: 我在玩它时注意到,如果我删除这一行:

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

and also remove the closing } , it works but does not display any data - just the table. 并删除关闭} ,它可以工作,但不显示任何数据 - 只是表格。 I am not sure what is going on here. 我不确定这里发生了什么。

Simple. 简单。 your mysqli_query call returns no records. 你的mysqli_query调用没有返回任何记录。 Either no records are found, or there is an error. 没有找到记录,或者有错误。 Make your code a little more robust. 使您的代码更健壮。

$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();
}

(As a side note, please use parametrised queries, you are opening yourself up to SQL injection now. MySQLi is no magic bullet against this, you still have to validate / sanitize input.) (作为旁注,请使用参数化查询,您现在可以自己打开SQL注入.MySQLi对此没有魔力,您仍需要验证/清理输入。)

Display mysqli error on fault: 显示故障时的mysqli错误:

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

Put $sku inside quotes. 把$ 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>';
    }
    ?>

I have managed to solve the issues that i have been having i had to remove the i from mysqli, but i have used the same piece of code on another site so it may be something to do with the server or database maybe. 我已经设法解决了我不得不从mysqli中删除i的问题,但我在另一个站点上使用了相同的代码,因此可能与服务器或数据库有关。 here is the code though' 这是代码虽然'

<?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