带有数据库ID的动态php页面

时间:2014-04-02 08:05:08

标签: php mysql

您好我正在尝试创建一个动态的php页面,该页面随着数据库行的ID而变化。

但到现在为止我得到了这个,但它没有显示任何东西:-( 试图从以前的问题中找到一些答案,但没有发现任何有助于我的问题。

任何人都有任何好的和简单的提示如何做到这一点??

<?php

    if (isset($_GET['id'])) {
        $id = $mysqli->real_escape_string($_POST['id']);
        $q = "SELECT * FROM `opskriftreg` WHERE `id` = '$term' LIMIT 1;";
        $q = $mysqli->query($q);
        if (mysqli_num_rows($q) > 0) {
            $result = mysqli_fetch_assoc($q);
            echo    "<div class=\"article\">".
                "<div class=\"title\">".$result['title']."</div>".
                "<div class=\"body\">".$result['description']."</div>".
                "<div class=\"desc\">"."Kort beskrivelse: ".$result['description']."</div>".
                "<div class=\"ingredients\">"."ingredienser: ".$result['ingredients']."</div>".
                "<div class=\"amount\">"."amount: ".$result['amount']."</div>".
                "<div class=\"guidance\">"."guidance: ".$result['guidance']."</div>".
                "<div class=\"hour\">"."hour: ".$result['hour']."</div>".
                "<div class=\"minutes\">"."minutes: ".$result['minutes']."</div>".
                "<div class=\"laktose\">"."laktose: ".$result['laktose']."</div>".
                "<div class=\"okologisk\">"."okologisk: ".$result['okologisk']."</div>".
            "</div>";
        }
        else {
            /* Article not found */
        }
    }
?>

如果有更简单的方法,我很乐意学习另一种方法。

  • 我只包含了php代码,而不是我的数据库连接。它有效,我测试了它: - )

4 个答案:

答案 0 :(得分:1)

你有这个:

$id = $mysqli->real_escape_string($_POST['id']);
$q = "SELECT * FROM `opskriftreg` WHERE `id` = '$term' LIMIT 1;";

所以我想你的查询只返回零行,因为你丢弃$_POST['id']并且可能用文字'$term'字符串搜索一个空字符串。

echo "SELECT * FROM `opskriftreg` WHERE `id` = '$term' LIMIT 1;";
  

注意:未定义的变量:第3行的D:\ tmp \ test.php中的术语
  SELECT * FROM opskriftreg WHERE id =&#39;&#39;限制1;

更糟糕的是,如果存在第三个变量,则只运行查询:

if (isset($_GET['id'])) {

答案 1 :(得分:0)

<?php

if (isset($_GET['id'])) {
    $id = $mysqli->real_escape_string($_GET['id']);
    $sql = "SELECT * FROM `opskriftreg` WHERE `id` = ? LIMIT 1;";
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param('s', $id);
    $stmt->execute();
    $res = $stmt->get_result();
    if (mysqli_num_rows($res) > 0) {
        while ($result = $res->fetch_array(MYSQLI_ASSOC)) { 
        echo    "<div class=\"article\">".
            "<div class=\"title\">".$result['title']."</div>".
            "<div class=\"body\">".$result['description']."</div>".
            "<div class=\"desc\">"."Kort beskrivelse: ".$result['description']."</div>".
            "<div class=\"ingredients\">"."ingredienser: ".$result['ingredients']."</div>".
            "<div class=\"amount\">"."amount: ".$result['amount']."</div>".
            "<div class=\"guidance\">"."guidance: ".$result['guidance']."</div>".
            "<div class=\"hour\">"."hour: ".$result['hour']."</div>".
            "<div class=\"minutes\">"."minutes: ".$result['minutes']."</div>".
            "<div class=\"laktose\">"."laktose: ".$result['laktose']."</div>".
            "<div class=\"okologisk\">"."okologisk: ".$result['okologisk']."</div>".
        "</div>";
        }
    }
    else {
        /* Article not found */
    }
}
?>

我假设您要使用$_GET变量而不是$term$_POST

答案 2 :(得分:0)

我没有测试过这个,我更喜欢PDO到mysqli,但它应该让你更接近。

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

$id = isset($_GET['id']) ? $_GET['id'] : (isset($_POST['id']) ? $_POST['id'] : false);
if ($id) {
    $stmt = $mysqli->prepare("SELECT * FROM `opskriftreg` WHERE `id` = ? LIMIT 1");
    $stmt->bind_param('i', $id);
    $stmt->execute();
    $result = $stmt->get_result()->fetch_array(MYSQLI_ASSOC);
    if ($result) {
        echo    "<div class=\"article\">".
            "<div class=\"title\">".$result['title']."</div>".
            "<div class=\"body\">".$result['description']."</div>".
            "<div class=\"desc\">"."Kort beskrivelse: ".$result['description']."</div>".
            "<div class=\"ingredients\">"."ingredienser: ".$result['ingredients']."</div>".
            "<div class=\"amount\">"."amount: ".$result['amount']."</div>".
            "<div class=\"guidance\">"."guidance: ".$result['guidance']."</div>".
            "<div class=\"hour\">"."hour: ".$result['hour']."</div>".
            "<div class=\"minutes\">"."minutes: ".$result['minutes']."</div>".
            "<div class=\"laktose\">"."laktose: ".$result['laktose']."</div>".
            "<div class=\"okologisk\">"."okologisk: ".$result['okologisk']."</div>".
        "</div>";
    }
    else {
        /* Article not found */
    }
}

答案 3 :(得分:0)

你完全搞乱变量

条件使用$_GET['id']

之后你使用$id = .... $_POST['id']

在SQL中你有id = '$term'

尝试使用变量保持一致。我可以假设您需要将代码更新为

if (isset($_GET['id'])) {

     $statement = $mysqli->prepare("SELECT * FROM `opskriftreg` WHERE `id` = ? LIMIT 1"); 
     $statement->bind_param('i', $_GET['id']); //Replace ? above with our input, specifying ‘i’ meaning that we are passing one integer.
     $statement->execute(); //Run the query
     $result = $statement->get_result()->fetch_assoc();

     // here goes your code