【问题标题】:Two many records showing when h_id is identified显示 h_id 何时被识别的两条多条记录
【发布时间】:2016-11-12 12:51:11
【问题描述】:

我正在尝试使用 PHP 过滤一个 mysql 表,我的目标是当 url 为 History.php?h_id=1 时,它只显示 h_id 中有一个的行(H_id 不是唯一的数字)

我的代码如下。

    <html>
<head>
<title></title>
</head>
<body >

<?php
mysql_connect('localhost', 'root', 'matl0ck') or die(mysql_error());
mysql_select_db("kedb") or die(mysql_error());

$h_id = (int)$_GET['h_id'];
$query = mysql_query("SELECT * FROM Hist WHERE H_ID = '$h_id'") or die(mysql_error());

if(mysql_num_rows($query)=1){
    while($row = mysql_fetch_array($query)) {
        $id = $row['ID'];
        $name = $row['Name'];
        $datemod = $row['DateMod'];
        $h_id = $row['H_ID'];
    }
?>
<?php
    $con=mysqli_connect("localhost","root","matl0ck","kedb");
    // Check connection
    if (mysqli_connect_errno()){
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT * FROM Hist");

    echo "<table border='1'>
            <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Date</th>
            <th>H_ID</th>
            </tr>";

    while($row = mysqli_fetch_array($result))
    {
        echo "<tr>";
        echo "<td>" . $row['ID'] . "</td>";
        echo "<td>" . $row['Name'] . "</td>";
        echo "<td>" . $row['DateMod'] . "</td>";
        echo "<td>" . $row['H_ID'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";

    mysqli_close($con);
?>
<?php
}else{
    echo 'No entry found. <a href="javascript:history.back()">Go back</a>';
}
?>
</body>
</html>

当我尝试使用它时,它会显示所有在 h_id 中有数字的记录,当我删除此列中的数字时,它会显示错误。

我的表格布局如下。

谢谢

【问题讨论】:

  • 什么错误????
  • 每次在新代码中使用the mysql_ 数据库扩展a Kitten is strangled somewhere in the world 时,它都已被弃用,并且已经存在多年,并且在 PHP7 中永远消失了。如果您只是学习 PHP,请花精力学习 PDOmysqli 数据库扩展。 Start here
  • 你为什么同时使用 MYSQL_ 和 MYSQLI_ 数据库扩展???
  • @RiggsFolly 但我不喜欢猫 ;-)
  • @Dagon 但是你想让他们的血沾到你的手上吗?

标签: php html mysql web


【解决方案1】:

这是你的语法错误陈述

if(mysql_num_rows($query)=1){

使用== 完成测试,= 是赋值

if(mysql_num_rows($query) == 1){
//------------------------^^
    while($row = mysql_fetch_array($query)) {
        $id = $row['ID'];
        $name = $row['Name'];
        $datemod = $row['DateMod'];
        $h_id = $row['H_ID'];
    }

还有

您的脚本有SQL Injection Attack 的风险 看看Little Bobby TablesEven 发生了什么 if you are escaping inputs, its not safe! 使用prepared parameterized statements,因此坚持使用mysqli_PDO 数据库扩展

您的通用代码似乎有点混乱,您从查询 "SELECT * FROM Hist" 中获取数据,而您似乎从未使用过。

在您实际使用并输出第一个查询的结果之前,while 循环也已终止。

我还修改了代码以使用参数化和准备好的查询,并删除了 PHP7 中不再存在的 mysql_ 的使用

<?php
// Use one connection for all script, and make it MYSQLI or PDO
$con=mysqli_connect("localhost","root","matl0ck","kedb");
if (mysqli_connect_errno()){
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    // if connection fails there is no point doing anything else
    exit;
}

//$h_id = (int)$_GET['h_id'];

// prepare and bind values to make the code safe from SQL Injection
// also only select the rows you want
$sql = "SELECT ID, Name, DateMod, H_ID FROM Hist WHERE H_ID = ?";
$stmt = $con->prepare($sql);
if ( ! $stmt ) {
    echo $con->error;
    exit;
}

$stmt->bind_param("i", $_GET['h_id']);

$stmt->execute();

if ( ! $stmt ) {
    echo $con->error;
    exit;
}

// bind the query results 4 columns to local variable
$stmt->bind_result($ID, $Name, $DateMod, $H_ID);

echo "<table border='1'>
      <tr><th>ID</th><th>Name</th><th>Date</th><th>H_ID</th></tr>";

if($con->affected_rows > 0){

    echo "<table border='1'>
          <tr><th>ID</th><th>Name</th><th>Date</th><th>H_ID</th></tr>";

    while($stmt->fetch()) {

        while($row = $stmt->fetch_array()) {
            echo "<tr>";
            echo "<td>$ID</td>";
            echo "<td>$Name</td>";
            echo "<td>$DateMod</td>";
            echo "<td>$H_ID</td>";
            echo "</tr>";
    }
    echo "</table>";

}else{
    echo 'No entry found. <a href="javascript:history.back()">Go back</a>';
}
?>

【讨论】:

    猜你喜欢
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    相关资源
    最近更新 更多