繁体   English   中英

如果mysql值包含另一个表中的另一个mysql值,请回显

[英]If mysql value contains another mysql value from a different table, echo

我有两个表,“客户”和“用户”。

在客户表中,我有一列名为“名称”的列。 这具有所有客户名称。

在我的用户表中,我有一列名为“管理”。 它具有某些客户名称。

我想,如果“管理” 包含 “名”客户的任何显示内容。

这是一个例子

“名称”包含客户“ applebees”,“ johnny carinos”和“ pizza小屋”。

用户rick是“管理”“ applebees”和“披萨小屋”。 在他的行/列中,它看起来像是“ applebees,pizza小屋”(我使用的是implode函数,这就是为什么有一个,”)

因此,在我的客户页面上,我希望rick看到applebees和披萨小屋。 我不想让他见到约翰尼·卡里诺斯,因为他没有管理。

我怎样才能做到这一点? 我尝试过preg match&strpos,但失败了。 (我对php还是很陌生的)。 如果是单个值,这会容易得多,所以我可以只设置value = value。 不幸的是,拥有多个用于“管理”的值确实让我感到困惑!

希望这有道理。 谢谢你的帮助!

这就是我现在拥有的(是的,我知道,这可能是非常错误的。)

<?php


$sql = "SELECT * FROM customers";
$result = mysql_query($sql);
        $thecust = $_SESSION['user']['managing'];
    $thecustomers = htmlentities($row['name']);


    $check = strprk(wing, $thecust);
    if ($check) {

while ($row = mysql_fetch_array($result)) {
?>

    <tr> 
        <td><?php echo "<a href='customer.php?id=" . $row['id'] . "'>" . $row['name'] . "</a>"?></td> 
        <td><?php echo htmlentities($row['contact_name'], ENT_QUOTES, 'UTF-8'); ?></td> 
        <td><?php echo htmlentities($row['contact_number'], ENT_QUOTES, 'UTF-8'); ?></td> 
        <td><?php echo htmlentities($row['contact_email'], ENT_QUOTES, 'UTF-8'); ?></td> 
    </tr> 

<? } ?>

答案在于您的数据库模式和查询方式。 您并没有真正进入数据库表结构,但我建议您使用类似这样的内容

users
-------
user_id INT, autoincrement, PRIMARY KEY
user_name VARCHAR
[other user-specific fields]

users_to_customers
-------
user_id INT
customer_id INT
[compound primary key across both fields]

customers
-------
customer_id INT, autoincrement, PRIMARY KEY
customer_name VARCHAR
[other customer specific fields]

注意我这里有三个表。 这使您可以将任意数量的用户与任意数量的客户相关联。 现在获取要查找的信息,假设您知道查看客户页面的用户的user_id 您的查询可能看起来像

SELECT c.customer_id, c.customer_name, [whatever other fields you need]
FROM users AS u
INNER JOIN users_to_customers AS uc
INNER JOIN customers AS c
WHERE u.user_id = ? [the user_id of the user viewing the page]

这将为您提供与该用户相关的那些客户的客户信息。


您也不应该使用mysql_*函数,因为它们已被弃用(请注意PHP.net文档中的红色大警告)。 也许使用mysqli_ *函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM