dongze8698 2018-02-11 07:13
浏览 102
已采纳

加入2表,其中customer_id = $ username [重复]

I'm developing a laundry service system, using php. I have 2 table, table customer and table ticket (with customer_id as the foreign key here).

so i'm trying to display a list of ticket after the user logged in, consists of data from table customer and table ticket.

i'm trying to join 2 table with customer_id as the link join.. for the logged in username(using $session to store username for logged in customer). here is my code:

$customerusername = $_SESSION['customerusername'];
$result = mysqli_query($conn,"SELECT * from TICKET INNER JOIN CUSTOMER ON TICKET.CUSTOMER_ID=CUSTOMER.CUSTOMER_ID 
                                        WHERE CUSTOMER_ID=(SELECT CUSTOMER_ID from CUSTOMER WHERE CUSTOMER_USER='$customerusername')");

however i got this error Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in this first line below:

while($row = mysqli_fetch_array($result)){   //Creates a loop to loop through results
    $found_row = true;
    echo " <tr>
            <td>".$row['TICKET_ID']."</td>
            <td>".$row['TICKET_PICKUP']."</td>
            <td>".$row['TICKET_PTIME']."</td>
            <td>".$row['TICKET_DELIVERY']."</td>
            <td>".$row['TICKET_DTIME']."</td>
            <td>".$row['TICKET_STATUS']."</td>
            <td>RM ".$row['TICKET_PRICE']."</td>
            <td>".$row['PAYMENT_STATUS']."</td>
            </tr>";
    }
    if ($found_row == false) {
            echo "No Ticket Submitted";
        }

what went wrong?

</div>
  • 写回答

3条回答 默认 最新

  • dongtan3306 2018-02-11 07:30
    关注

    You have to use aliases on tables to specify columns that have same names and avoid ambiguous errors.

    Your CUSTOMER_ID is present in your two tables. So MySQL cannot know which column you are talking about.

    You could try something like this:

    $customerusername = $_SESSION['customerusername'];
    $result = mysqli_query($conn,
        "SELECT * from TICKET t 
         INNER JOIN CUSTOMER c ON t.CUSTOMER_ID=c.CUSTOMER_ID 
         WHERE t.CUSTOMER_ID=(SELECT CUSTOMER_ID from CUSTOMER WHERE CUSTOMER_USER='$customerusername')");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示