2015-06-11 30 views
0

我新的PHP和我在我的網址作爲參考使用ID試圖從我的DB呼應某一特定領域從MySQL呼應。但是,不要回顯它回顯ID的值。PHP-有問題,使用GET

我究竟做錯了什麼?有沒有更好的方法來做到這一點?提前致謝。

我一個試圖從我在我的網址ID呼應dateCreated會現場。 URL是www.example.com/client_profile.php?id=1001

<div class="thumb-info mb-md"> 
    <img src="assets/images/!logged-user.jpg" class="rounded img-responsive" alt="John Doe"> 
    <div class="thumb-info-title"> 
     <?php 
      echo $_GET['id']; //Output: myquery 
      $result = mysql_query("SELECT * FROM client WHERE id = '" . $_GET['id'] . "'"); 
      while($row = mysql_fetch_array($result)){ 
       echo "<span class='thumb-info-inner'>$row.['datecreated'].</span>"; 
      } 
     ?> 
    </div> 
</div> 

更新的代碼

<?php 
mysql_connect("localhost", "my user", "my pass") or 
die("Could not connect: " . mysql_error()); 
mysql_select_db("my db"); 

$id = (int)$_GET['id']; //Output: myquery 
echo $id; 
$result = mysql_query("SELECT * FROM client WHERE id = $id"); 
while($row = mysql_fetch_array($result)){ 
    echo "<span class='thumb-info-inner'>" . $row['datecreated'] . "</span>"; 
}?> 

呼應的兩個編號,並從DB

+0

,您在該線路上'回聲說 「<跨度類= '拇指信息-內'> $行[ 'dateCreated會']。」;}?>'它輸出1001? – Leeish

+0

@Leeish是標識beeing 1001 – Mathew

+0

'1001'從'回聲的$ id來;'。沒有辦法來自'$ row ['datecreated']' – Barmar

回答

0

價值嘗試這個。

<div class="thumb-info mb-md"> <img src="assets/images/!logged-user.jpg" class="rounded img-responsive" alt="John Doe"> 
    <div class="thumb-info-title"> 
     <?php 
      $id = (int)$_GET['id']; //Output: myquery 
      echo $id; 
      $result = mysql_query("SELECT * FROM client WHERE id = $id"); 
      while($row = mysql_fetch_array($result)){ 
       echo "<span class='thumb-info-inner'>" . $row['datecreated'] . "</span>"; 
      }?> 
    </div> 
</div> 

在您當前的代碼中,您的串聯已關閉以顯示$row數據。你也開放SQL注入。爲了解決SQL注入問題,我把你的id作爲一個整型,這是關於SQL Injection Protection - Cast from string to int的一個線程。你也可以使用mysql_real_string_escape(字的順序可能在那裏)。 你應該當你雖然可以,How can I prevent SQL injection in PHP?切換到PDO和參數化查詢。

+0

謝謝。所以我複製/粘貼你的代碼,我仍然得到相同的結果。 – Mathew

+0

我想先連接數據庫嗎? – Mathew

+0

您現在正在將'Reference ##'作爲輸出嗎?是的,你應該連接到數據庫。有關示例,請參閱手冊http://php.net/manual/en/function.mysql-fetch-array.php。 – chris85