2017-12-17 2 views
1

데이터베이스에 두 개의 테이블이 있습니다. 즉 food_dishfoodcat. 음식 접시는 음식을위한 것이고 foodcat은 음식의 범주입니다.테이블에 특정 데이터베이스 값 표시

dish_id | dish name | dish_cat 1 | Plain Rice | 1 2 | Pork Chop | 2 3 | Buttered Rice | 1

및 foodcat에 : 여기

내부 food_dish 무엇의 예 내 menurice.php 테이블에 표시 할

cat_id | cat_name 1 | Rice 2 | Pork

만 일반 쌀. 나는 그것은 단지 Rices의 이름을 보여줄 것 1CAT_ID 호출 할 때 나는 CAT_ID 그래서dish_cat를 연결할 수 있는지 확인하고 싶다. Rices를 만 표시해야 그것은 당신이 menurice.php?cat_id=1에 갈 때

<table class="table table-striped table-bordered table-hover" id="dataTables-example"> 
<thead> 
<tr>   
<th>ID</th> 
<th>Dish Name</th> 
<th> Actions </th> 
</tr> 
</thead> 
<tbody> 
<?php 
include 'menuactions/foodconnect.php'; 
$sql1= "SELECT * FROM `food_dish`"; 
$data= mysqli_query($conn,$sql1) or die("Connection Failed!"); 
while($row = mysqli_fetch_array($data, MYSQLI_ASSOC)){ 
?> 

<tr class="odd gradeX"> 
<td> <?php echo $row['dish_id']?> </td> 
<td> <?php echo $row['dish_name']?> </td>          
<td> 

<div class='btn-group'> 
<form action='deleterice.php' method='POST'> 
    <button class='btn btn-default' type='submit'><a class='fa fa-trash-o'> Delete</a></button> 
      <input type='hidden' value=" <?php echo $row['dish_id'];?>" name="iduse"> 
      </form> 
     </div> 
     </td> 
     </tr> 

     <?php 
    } 
    ?> 

답변

0

그래서 당신은 의미합니까?

이 쿼리는 SELECT * FROM food_dish WHERE dish_cat = ?을 사용하고 $_GET['cat_id']을 쿼리에 바인딩합니다. 예를 들어

:

$stmt = mysqli_prepare($conn, "SELECT dish_id, dish_name FROM food_dish WHERE dish_cat = ?"); 

mysqli_stmt_bind_param($stmt, "i", $_GET['cat_id']); 
mysqli_stmt_execute($stmt); 

mysqli_stmt_bind_result($stmt, $dish_id, $dish_name); 

while (mysqli_stmt_fetch($stmt)) { 
    ?> 

<tr class="odd gradeX"> 
    <td> <?php echo $dish_id; ?> </td> 
    <td> <?php echo $dish_name; ?> </td> 
    <td> 
    <div class='btn-group'> 
     <form action='deleterice.php' method='POST'> 
     <button class='btn btn-default' type='submit'><a class='fa fa-trash-o'> Delete</a></button> 
     <input type='hidden' value="<?php echo $dish_id; ?>" name="iduse"> 
     </form> 
    </div> 
    </td> 
</tr> 

    <?php 
} 
+0

나는 오류'mysqli_stmt_bind_param()가 예상 저에게 매개 변수 1이 될 :

는 위의 쿼리는 CAT_ID 1 쌀 이름을 출력 mysqli_stmt, boolean' –

+0

@EllisCristoph 실수로,'dish_cat' 대신'cat_id'를 쿼리에 사용했습니다. 업데이트 된 코드를 사용해보십시오. URL은 여전히'menurice.php? cat_id = 1'이고, 차이를 보려면 1을 2로 변경하십시오. –

+0

'syntax error, unexpected ''준비 오류 : ''(T_CONSTANT_ENCAPSED_STRING)'새로운 오류가 발생합니다. –

0

나는 당신이 food_dish 테이블에서 이름 대신 표시 foodcat 테이블의 이름을 사용한다고 가정합니다.

inner join 또는 union 명령을 사용할 수 있습니다. 추적의 SQL 명령이 작동합니다 :

SELECT food_dish.dish_id as id,foodcat.cat_name as name from food_dish inner join foodcat on instr(food_dish.dish_name,foodcat.cat_name)>0 

그것은 다음과 같은 결과를 얻을 것입니다 : ID 이름 1 쌀 2 돼지 고기 3 쌀

그것이 도움이되기를 바랍니다.

0

대신 사용해보십시오. 이 질의는 dish_cat과 cat_id를 연결하고 dish_cat과 cat_id가 같은 경우에만 접시 이름을 반환합니다.

SELECT food_dish.`dish name` FROM food_dish, foodcat 
WHERE cat_id = 1 AND (food_dish.dish_cat = foodcat.cat_id); 

여기서 원하는 cat_id 값을 전달하십시오.

요리 이름

일반 쌀

버터 라이스

+0

이것을 어떻게 부트 스트랩 테이블에 넣을 수 있습니까? 그것은 나에게 "연결 실패"오류를 제공합니다. –

+0

@EllisCristoph 코드에 쿼리를 넣고 요소로 묶인 echo 변수를 삽입하여 부트 스트랩 테이블에 표시하십시오. 아직 이해하지 못했다면 "연결 실패"오류에 대한 자세한 내용을 공유하십시오. –

관련 문제