dthhlf1777 2013-03-20 23:53
浏览 70
已采纳

PHP MySQL如何在一个表行中关联两个字段?

If I could talk to my database table, I would say "if client_id and salesrep_id are in the same row, do this!

Most of the conditional logic I've got. It's the "in the same row" part I'm struggling with.

Essentially, I'm building a system that allows all sales reps to view the entire data table but will permit each rep to update only their own specific entries.

The idea is to disable everybody else's "EDIT" links in the display.

Like so ...

    <?php if ($row_listClients['client_id'] && $row_listClients['salesrep_id']) { ?>
    <td><a href="edit_clients.php?client_id=<?php echo $row_listClients['client_id']; ?>">Edit</a>
    </td>
    <?php } else { ?>
    <td>Edit</td>
    <?php } ?>

Obviously the initial condition doesn't work ( && will always be true ) but everything else works great. It's just this one bit I'm stuck on.

Hope that makes sense, I know it's probably pretty basic stuff.

Thanks a bunch guys, any pointers would be really appreciated.

--

Here's the query :

    $query_listClients = "SELECT client_id, user_id, repname, date, client_type, company,
    contact_firstname, contact_lastname, phone, email, property, address, city, province,
    repnotes FROM clientdata ORDER BY company ASC";
    $listClients = mysql_query($query_listClients, $connAdmin) or die(mysql_error());
    $row_listClients = mysql_fetch_assoc($listClients);

Thanks again ...

--

Sample of database data - which I entered as a test only. The first two number are client_id and user_id respectively:

    19;"33"; "Ronald Wharton"; "March 20 2013"; "Commercial"; "Bonkers Inc."; "Brett";
    "Bonkers"; "987   654 3211"; "brentbonkers@bonkersinc.com"; "Retail Outlet"; 
    "12765 34 Ave"; "Lost Soul City"; "Sask"; "Brett wants to meet with Keith and Dean 
    before the end of the week.  Please call before Friday."        

Don't know if that's any help, but there ya go, thanks.

  • 写回答

2条回答 默认 最新

  • dongshan1396 2013-03-21 20:22
    关注

    I reread your question. It sounds as if you're querying to get all records, but only want to show edit links for items a particular sales rep should be allowed to edit.

    Since you want all records, this shouldn't be done via SQL, and it sounds like all you need to do is change:

    <?php if ($row_listClients['client_id'] && $row_listClients['salesrep_id']) { ?>
    

    To something like:

    <?php if ($row_listClients['salesrep_id'] == $currentSalesRepId) { ?>
    

    You'll obviously need to use the appropriate variable for the $currentSalesRepId.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法