从链接中的id获取数据库数据的问题

时间:2016-11-24 17:53:06

标签: php html mysqli

我目前有这段代码,我的目标是从数据库中的数组(oppdrID)中获取与URL中的id相对应的数据(blabla.php?id = 8)。现在,当我运行此代码时,没有任何显示,没有任何错误。

//更新了代码。错误消息“(!)致命错误:不能在第39行的C:\ wamp64 \ www \ prosjekt \ endre.php中使用mysqli_result类型的对象作为数组”

<!doctype html>
<html>
<head>
<link rel='stylesheet' type='text/css' href='stilsett.php' />
<meta charset="utf-8">
<title>Endre oppdrag // Prosjekt - PHP</title>
</head>
<body>

<?php
include "funksjoner.inc.php";
echo "<div id='header'>";

echo navigasjon();

echo "</div>";

echo "<div id='innhold'>";

$db = kobleTil();

if (isset($_GET['oppdrID']) && is_numeric($_GET['oppdrID']) &&       $_GET['oppdrID'] > 0) {

$id = $_GET['oppdrID'];
$sql = "SELECT * FROM oppdrag WHERE oppdrID = ?";

$stmt = $db->stmt_init();
if ($stmt->prepare($sql))
$stmt->bind_param("i", $id);
$stmt->execute();

while ($nesteRad = $stmt->get_result()){

echo "<hr />";

    echo "<table id='resultat'>";
    echo "<tr><th>Navn</th><th>Type</th><th>Startdato</th><th>Sluttdato</th>  <th>Antall timer</th><th>Aktiv</th></tr>";
    echo "<tr>";
    echo "<td>" . $nesteRad['navn'] . "</td>"; 
    echo "<td>" . $nesteRad['type'] . "</td>";
    echo "<td>" . $nesteRad['startDato'] . "</td>";
    echo "<td>" . $nesteRad['sluttDato'] . "</td>";
    echo "<td>" . $nesteRad['timer'] . "</td>";
    echo "<td>" . $nesteRad['aktiv'] . "</td>";
    echo "</tr></table>";

    $stmt->close();


    echo "<hr />";

    }
}

echo    "<form action='kjoer2.php' method='post'>";
echo    "<table id='leggInn'><tr><td>
    <label for='startTid'>Starttid</label></td><td><input type='datetime-  local' name='startTid' id='skjemaLeggInn'></td></tr>
    <tr><td>
    <label for='slutTid'>Sluttid</label></td><td><input type='datetime-  local' name='slutTid' id='skjemaLeggInn'></td></tr>
    <tr><td>
    <label for='merknad'>Merknad</label></td><td><textarea name='merknad'   rows='10' cols='30'  id='skjemaLeggInn'></textarea></td></tr>
    <tr><td>
    <label for='antTimer'>Antall Timer</label></td><td><input type='text'    name='antTimer' id='skjemaLeggInn'>
    </td></tr>
    <tr><td><input type='submit' value='Legg inn'>
    </td></tr>
    </form>";

echo "</div>";
?>


</body>
</html>   

1 个答案:

答案 0 :(得分:0)

您的网址应该是blabla.php?oppdrID=8

然后修复代码以获取正确的参数,并将其添加到SQL:

$id = $_GET['oppdrID'];
$sql = "SELECT * FROM oppdrag WHERE oppdrID = ?";

if ($statement = $db->prepare($sql)) {
    $statement->bind_param("i", $id);
    $statement->execute();
...