2014-04-02 80 views
-2

嗨我正在嘗試創建一個動態的PHP頁面,該頁面隨着數據庫行的ID而改變。帶數據庫ID的動態PHP頁面

但到現在我得到了這一點,但:-( 曾經試圖找到從以前的問題,一些答案它並不顯示任何東西,但沒有發現任何幫助我的問題。

任何人有任何好的和簡單提示如何做到這一點??

<?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代碼, nd不是我的數據庫連接。它的工作原理,我已經測試過它:-)
+0

你試着把'$ q'改成另一個名字,看看結果是什麼。並試試 $ q =「SELECT * FROM opskriftreg WHERE id ='$ term'LIMIT 1」; – user3454436

+0

打開錯誤報告的開始......您似乎不太瞭解該語言。 –

+0

您正在使用'$ term',但似乎並未在此上下文中定義。 – seong

回答

0

您有變量一塌糊塗

條件使用後$_GET['id']

你ARR使用$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 
+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;"; 

Notice: Undefined variable: term in D:\tmp\test.php on line 3
SELECT * FROM opskriftreg WHERE id = '' LIMIT 1;

更糟糕的是,你只有在第三個變量存在運行查詢:

if (isset($_GET['id'])) { 
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

+0

我得到一個錯誤: 致命錯誤:調用成員函數prepare() -object在第14行上的/home/www/.dk/work/dynamic_page.php '$ stmt = $ connection-> prepare($ sql);' –

+0

您需要將$ connection變量更改爲您的MYSQLi連接變量 –

+0

回到原來的一個:P現在它在dynamic_page.php沒有再顯示任何東西?id = 1 –

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 */ 
    } 
}