2013-12-08 82 views
0

我試圖從我的產品表中使用另一個表中的外鍵輸出產品,因爲產品隨後保存到用戶帳戶中。使用外鍵從表中獲取數據PHP MySQL

關係表:

useraccount > savedproduct < products 

儘可能多的用戶可以有很多產品,很多產品可以有很多的用戶我做了一個表中只保存savedProductId,和的productId USER_ID的插圖中。

我想從僅保留特定用戶ID的已保存產品表中輸出產品數據(如果有意義的話)。

它幾乎工作,但顯示所有產品(重複兩次),而不是僅僅是用戶ID下保存的產品的人,我的代碼是在這裏:

注:請不要說關於注射什麼等等,我會執行後,我已經一切工作,謝謝。

$user_check=$_SESSION['login_user']; 

$sqlCommand = "(SELECT * FROM userAccount WHERE email='$user_check')"; 
$query = mysqli_query($con,$sqlCommand) or die("Error: ".mysqli_error($con)); 
$column = mysqli_fetch_array($query); 

if($column['admin'] != NULL){ 
    echo "<section class='userName'><h3>".$column['firstName']." ".$column['surname']."</h3></section>"; 
    echo "<section class='address'>".$column['addressLine1']."<br />".$column['addressLine2']."<br />".$column['county']."<br />".$column['country']."<br />".$column['postCode']."</section>"; 
    echo "<section class='email'><h3>".$column['email']."</h3></section>"; 
    echo "<section class='passwordUpdate'><a href='update.php?user_id=".$column['user_id']."'>Change Password</a></section>"; 
    echo "<section class='logout'><a href='extras/logoutProcess.php'>Logout</a></section>"; 
    echo "<hr />"; 
     </section>"; 

部分在那裏我試圖輸出與用戶ID相關聯的保存的產品,等:

}else{ 
    $userIdent=$column['user_id']; 
    $sqlCommand = "SELECT savedProduct.user_id, product.productName, product.productId, product.productImg, product.price FROM savedProduct, product WHERE savedProduct.user_id=$userIdent LIMIT 2"; 
    $query = mysqli_query($con,$sqlCommand) or die("Error: ".mysqli_error($con)); 

    echo "<section class='userName'><h3>".$column['firstName']." ".$column['surname']."</h3></section>"; 
    echo "<section class='address'>".$column['addressLine1']."<br />".$column['addressLine2']."<br />".$column['county']."<br />".$column['country']."<br />".$column['postCode']."</section>"; 
    echo "<section class='email'><h3>".$column['email']."</h3></section>"; 
    echo "<section class='passwordUpdate'><a href='update.php?user_id=".$column['user_id']."'>Change Password</a></section>"; 
    echo "<section class='logout'><a href='extras/logoutProcess.php'>Logout</a></section>"; 
    echo "<hr />"; 

    echo '<section style="overflow:auto;height:400px;"><table cellpadding="0" cellspacing="0" border="0"> 
          <tr> 
            <th></th> 
            <th>Product</th> 
            <th>Price</th> 
          </tr>'; 
    while($savedP = mysqli_fetch_array($query)){ 
     echo "<tr> 
       <td> 
        <a target='_self' href='fullProductInfo.php?productId=".$savedP['productId']."'> 
         <img src='http://www.littlepenguindesigns.co.uk/pages/CMX/images/products/".$savedP['productImg']."' alt='".$savedP['productName']."' width='180' height='150' border='0' /> 
        </a> 
       </td> 
       <td><a target='_self' href='fullProductInfo.php?productId=".$savedP['productId']."'>".$savedP['productName']."</a></td> 
       <td>&pound;".$savedP['price']."</td> 
      </tr>"; 
    } 
    echo "</table></section>"; 
} 

幫助每一位表示讚賞。

回答

0

如果我明白你的問題是正確的,那麼你只需要在這3個表格上使用right join

+0

已經看着這個,它似乎是正確的路要走,謝謝你幫助我沿着正確的道路:) –

+0

多數民衆贊成在罰款:)請投我的答案+1 :) – Stone

+0

我還不能投票,但已經接受了答案:) –

相關問題