dongme8388 2019-04-04 18:09
浏览 103
已采纳

使用GET进行MySQL查询

Hi below is a sql query that does not function correctly. This query below should show 1 invoice of the current selected customer. But somehow I can not manage to make this query work like shown below.

<?php
$id = (ISSET($_GET['id'])) ? intval($_GET['id']):0;
$clientnumber= (ISSET($_GET['clientnumber'])) ? intval($_GET['clientnumber']):0;

$sql = "SELECT * 
        FROM customers AS a 
            inner join invoices AS b on a . '$clientnumber' = b . '$clientnumber'
                and a . '$id' != b . '$id'"; 

$result = $conn->query($sql) or die(mysql_error());
$query=getenv(QUERY_STRING);
parse_str($query);

?>

<?php while ($row = $result->fetch_assoc()) {?>

Echo sql results:

SELECT * 
FROM customers AS a 
    inner join invoices AS b on a.'0' = b.'0' 
        and a.'4' != b.'4'
  • 写回答

1条回答 默认 最新

  • dsf6778 2019-04-04 18:21
    关注

    Although you should use prepared statements, I think the problem is that you need to check the column in the customers table matches the columns in the invoices table and not the value your looking for. The names of the columns I'm not sure about, but your matching the invoice is for the client and then in the WHERE clause you check that the client number is the one your looking for...

     SELECT * 
        FROM customers AS a 
        inner join invoices AS b on a.clientnumber = b.clientnumber
                and b.invoiceid = $id
        where a.clientnumber = $clientnumber
    

    To work without the ID, then try

     SELECT * 
        FROM customers AS a 
        inner join invoices AS b on a.clientnumber = b.clientnumber
        where a.clientnumber = $clientnumber
    

    If your clientnumber column is a character field...

        where a.clientnumber = '$clientnumber'
    

    If this still isn't returning rows, try changing the inner join to...

    left join invoices AS b on a.clientnumber = b.clientnumber
    

    This allows it to find customers where there isn't a matching invoice.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示