简体   繁体   English

从链接中的ID获取数据库数据时出现问题

[英]Problems getting database data from id in link

I've currently got this code, where my goal is to fetch the data from a array in the database (oppdrID) corresponding to the id in the URL (blabla.php?id=8). 我目前有此代码,我的目标是从数据库中与URL中的id(blabla.php?id = 8)对应的数组(oppdrID)中获取数据。 Right now when I run this code nothing shows up, no errors no nothing. 现在,当我运行此代码时,什么都没有显示,没有错误,没有任何错误。

// Updated code. //更新的代码。 Error message "( ! ) Fatal error: Cannot use object of type mysqli_result as array in C:\\wamp64\\www\\prosjekt\\endre.php on line 39" 错误消息“(!)致命错误:无法在第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>   

Your URL should be something like blabla.php?oppdrID=8 您的网址应类似于blabla.php?oppdrID=8

Then fix your code to get the right param, and add it to your SQL: 然后修复代码以获得正确的参数,并将其添加到SQL中:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM