2013-12-21 47 views
-1

我正在爲一個課程製作一個項目,我需要製作一個包含所有藝術家和電影的電影網站原型。我需要通過藝術家的名字顯示搜索結果,並且鏈接到頁面以獲得關於藝術家的更多詳細信息。對於搜索頁面的代碼 部分:

 elseif ($method=='artist') 
    { 
     $x=0; 
     $query = "SELECT artistId, image, firstname,lastname,dateOfBirth,otherInfo FROM $artists WHERE CONCAT(firstname, ' ', lastname) LIKE '%$kword%' OR lastname LIKE '%$kword%' or firstname like '%$kword%'"; 
     mysqli_select_db($connect, $mydb); 
     $result = mysqli_query($connect, $query); 

     if($result) 

     { 
      print '<table >'; 
      while ($sqlRow=mysqli_fetch_array($result, MYSQL_ASSOC)) 
      { 
      $x++; 
       echo "<tr>"; 
       echo "<td rowspan=\"3\" id=\"image\">"; 
       echo '<img src="' . $sqlRow['image'] . '" alt="alt">'; 
       echo"</td>"; 
       echo "<td id=\"title\"><strong><a href=artist_details.php?id=".$sqlRow['artistId'].">".$sqlRow['firstname']." ".$sqlRow['lastname']."</strong></td>"; 
       echo "</tr>"; 
       echo "<tr>"; 
       echo "<td id=\"date\"><strong>".$sqlRow['dateOfBirth']."</strong></td>"; 
       echo "</tr>"; 
       echo "<tr>"; 
       echo "<td id=\"desc\">".$sqlRow['otherInfo']."</td>"; 
       echo "</tr>"; 
      } 

      print '</table>'; 
      if ($x==0) 
      { 
      echo "<h4>"; 
      echo "No matches found"; 
      echo "</h4>"; 
      } 
     } 

    } 

的詳細信息頁面的代碼:

$query = "SELECT artistId, image, firstname,lastname,dateOfBirth,otherInfo FROM $artists WHERE artistId=".$_GET['artistId']; 
     mysqli_select_db($connect, $mydb); 
     $result = mysqli_query($connect, $query); 

     if($result) 

     { 
      print '<table >'; 
      while ($sqlRow=mysqli_fetch_array($result, MYSQL_ASSOC)) 
      { 
      $x++; 
       echo "<tr>"; 
       echo "<td rowspan=\"3\" id=\"image\">"; 
       echo '<img src="' . $sqlRow['image'] . '" alt="alt">'; 
       echo"</td>"; 
       echo "<td id=\"title\"><strong><a href=\"artist_detail.php?id=".$sqlRow['artistId']."\">".$sqlRow['firstname']." ".$sqlRow['lastname']."</strong></td>"; 
       echo "</tr>"; 
       echo "<tr>"; 
       echo "<td id=\"date\"><strong>".$sqlRow['dateOfBirth']."</strong></td>"; 
       echo "</tr>"; 
       echo "<tr>"; 
       echo "<td id=\"desc\">".$sqlRow['otherInfo']."</td>"; 
       echo "</tr>"; 
      } 

      print '</table>'; 
      } 

每次我得到同樣的錯誤:未定義指數:artistId。 我會很感激你的幫助! :)

+0

你在傳遞'artistId = '在URL中嗎?你是否知道你的代碼對SQL注入攻擊是開放的? – andrewsi

+0

'artistId'與'artistd –

+0

anrewsi不一樣,我知道GET不安全,但我不太瞭解任何其他解決方案,因爲我是PHP初學者:) – user3125917

回答

0

你似乎對artistId設置爲

id 

一個_GET值

echo "<td id=\"title\"><strong><a href=\"artist_detail.php?id=".$sqlRow['artistId']."\">".$sqlRow['firstname']." ".$sqlRow['lastname']."</strong></td>"; 

,但你看看

$_GET['artistId'] 

,而不必完全理解這個代碼可能會有所幫助:

echo "<td id=\"title\"><strong><a href=\"artist_detail.php?artistId=".$sqlRow['artistId']."\">".$sqlRow['firstname']." ".$sqlRow['lastname']."</strong></td>"; 
+0

WORKS,非常感謝你! – user3125917

+1

如果它適合您,請務必選擇此答案作爲您接受的答案。 – symlink

0

您需要使用isset結構,然後從外部值分配變量,例如$_GET$_POST

你需要檢查這樣的..

if(isset($_GET['artistId'])) 
{ 
$artistId = $_GET['artistId']; // filter this variable before passing directly to your SQL query, else you will be under the SQL Injection Scanner ! 
//do your query here 
} 
else 
{ 
echo "Artist Id was not passed"; 
} 

首先檢查您的網址是否經過artistId到您的詳細信息頁面。例如.. http://yourpage.com/artistDetail.php?artistId=3403

+0

它將值傳遞給URL,但仍然給我一個未定義的索引 – user3125917

+0

如果你實現了'isset'構造,你將無法獲得未定義的索引錯誤。 –

相關問題