dongshi6529 2015-02-13 22:10
浏览 46
已采纳

PHP MySql无法正确检索数据

I'm trying to retrieve a table from MySql database, but my code returns incorrect data. here is my code below

<?php 
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "net_trade";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (mysqli_connect_errno()) {
    die("Connection failed: " . mysqli_connect_error());
}


echo "<link rel='stylesheet' type='text/css' href='style.css'>";

echo "<h1>NET TRADE</h1>";
echo "<div align='center'>";
echo "<ul>";
echo "<li><a class='nav' href='inventory.php'>INVENTORY</a></li>";
echo "<li><a class='nav' href='supplier.php'>SUPPLIER</a></li>";
echo "<li><a class='nav' href='customer.php'>CUSTOMER</a></li>";
echo "<li><a class='nav' href='sales.php'>SALES</a></li>";
echo "</ul>";
echo "</div>";

echo "<form method='POST' action='create_supplier.html'>
      <input type='SUBMIT' class='style19' name='new_item' value='Add New Item'></form>";



echo "<table class='TFtable'>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Quantity</th>";
echo "<th>Name</th>";
echo "<th>Brand</th>";
echo "<th>Model</th>";
echo "<th>Serial</th>";
echo "<th>Date Supplied</th>";
echo "<th>Supplier</th>";
echo "</tr>";


$result = mysqli_query($conn, "SELECT * FROM inventory");

if (mysqli_num_rows($result) > 0) {


   while($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
        echo "<tr>";
        echo "<td>".$row['0']."</td>";
        echo "<td>".$row['1']."</td>";
        echo "<td><a href='edit.php?id=".$row['0'].">".$row['2']."</a></td>";
        echo "<td>".$row['3']."</td>";
        echo "<td>".$row['4']."</td>";
        echo "<td>".$row['5']."</td>";
        echo "<td>".$row['6']."</td>";
        echo "<td>".$row['7']."</td>";
        echo "</tr>";
    }
mysqli_free_result($result);
}else {
    echo "0 results";
}
mysqli_close($conn);
echo "</table>";



?>

the result should be this...

enter image description here

but my code returns something like this..

enter image description here

the result skips a row from the database table.. thats my main prob

  • 写回答

1条回答 默认 最新

  • dongsu3138 2015-02-13 22:30
    关注

    I believe this line is goofed up in your code.

      echo "<td><a href='edit.php?id=".$row['0'].">".$row['2']."</a></td>";
    

    You need, noticing the matched double quotes in the href, something this this.

    <td><a href="edit.php?id=12345">asd</a></td>
    

    What you're getting, I think, is this.

      <td><a href='edit.php?id=12345>asd</a></td>
    

    Notice your href is started with a single quote (which web browsers tolerate, but is non standard), and isn't ended.

    You probably want code looking something like this. String concatenation is not easy to read, so sprintf() can be superior for making up this kind of link tag.

     echo sprintf ('<td><a href="edit.php?id=%d>%s</a></td>', $row[0], $row[2]);
    

    Pro tip: Always do view source when your results are bizarre.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案