PHP - 使用2个连接字段进行搜索

时间:2015-07-08 14:17:19

标签: php sql forms

我已在表单的下拉列表中连接了2个字段,其中2个字段为名字姓氏;这样我就可以显示一个人的全名。

问题在于,在数据库中,一个人的全名存储在两个单独的字段中;名字和姓氏。

我需要将此人的全名(从下拉列表中)与数据库中的名字和姓氏相匹配,但我正在努力实现这一目标。

连接表单列表的php代码是:

<td>
  <select name='IndivSurname'>";
        while($FullName_row = odbc_fetch_array($sql_run_FullName)){
            $IndivFirstName=$FullName_row['FirstName'];
            $IndivLastName=$FullName_row['LastName'];
            echo"<option value='$IndivIndivId' . '$IndivTenantNdx'> $IndivFirstName $IndivLastName</option>";
    }           
   </select>
</td>

虽然SQL语句是:

SELECT EventId, EventTime, Individual, Tenant, TenantName, DeviceName, Comment, 
InetDb.dbo.Individuals.FirstName, InetDb.dbo.Individuals.LastName
FROM taclogdata.dbo.Event
   LEFT JOIN InetDb.dbo.Tenants 
      ON taclogdata.dbo.Event.Tenant = InetDb.dbo.Tenants.TenantId
   LEFT JOIN InetDb.dbo.Individuals
      ON taclogdata.dbo.Event.Individual = InetDb.dbo.Individuals.IndivId 
      AND taclogdata.dbo.Event.Tenant = InetDb.dbo.Individuals.TenantNdx
   WHERE (taclogdata.dbo.Event.EventTime BETWEEN '00:00:00 05/26/2015' 
      AND  '09:00:00 05/26/2015' 
      AND (taclogdata.dbo.Event.Comment ='Reader entry' 
      OR taclogdata.dbo.Event.Comment='Reader exit') 
      AND (InetDb.dbo.Individuals.FirstName = '$IndivFirstName' 
      AND InetDb.dbo.Individuals.LastName = '$IndivLastName')";

非常感谢提前

3 个答案:

答案 0 :(得分:1)

最简单的方法是在空格上分割字符串。但是,如果此人具有多个姓氏或姓氏,则会引入错误。这意味着在我们在拆分结果中有3个或更多元素的情况下,我们必须将中间元素添加到两个匹配中;因为我们不知道它是姓氏还是中间名 这就是为什么我建议在value属性中为胶水使用不同的字符,一个从未在名称中使用的字符。 #就是这样一个角色。

这将为您提供以下代码生成表单:

// CF: Added HTML-escaping to prevent XSS-attacks.
$IndivFirstName=htmlspecialchars ($FullName_row['FirstName']);
$IndivLastName=htmlspecialchars ($FullName_row['LastName']);

echo"<option value='{$IndivFirstName}#{$IndivLastName}'> $IndivFirstName $IndivLastName</option>";

在SQL语句中,您可以执行以下操作(使用预准备语句):

$stmt = $db->prepare ("SELECT EventId, EventTime, Individual, Tenant, TenantName, DeviceName, Comment, InetDb.dbo.Individuals.FirstName, InetDb.dbo.Individuals.LastName
FROM taclogdata.dbo.Event
 LEFT JOIN InetDb.dbo.Tenants 
  ON taclogdata.dbo.Event.Tenant = InetDb.dbo.Tenants.TenantId
LEFT JOIN InetDb.dbo.Individuals
  ON taclogdata.dbo.Event.Individual = InetDb.dbo.Individuals.IndivId 
  AND taclogdata.dbo.Event.Tenant = InetDb.dbo.Individuals.TenantNdx
WHERE (taclogdata.dbo.Event.EventTime BETWEEN '00:00:00 05/26/2015' AND '09:00:00 05/26/2015' 
  AND (taclogdata.dbo.Event.Comment ='Reader entry' OR taclogdata.dbo.Event.Comment='Reader exit') 
  AND (InetDb.dbo.Individuals.FirstName = :firstname AND InetDb.dbo.Individuals.LastName = :lastname)");
$name = explode ("#", $_POST['IndivSurname']);
$stmt->exec (array (":firstname" => $name[0], ":lastname" => $name[1]));

答案 1 :(得分:1)

Splitting on spaces is not going to be reliable given the number of De Aches and Mc Pains in this world.

The really sure way of doing this is to save the first name and last name in a hidden table within your form (in the same order as your pulldown).

A possible alternative is to concatenate using a not a space but one of the other white space characters like NO BREAK SPACE "\u00A0" which you can then reliably split later.

答案 2 :(得分:0)

好的我通过单独张贴用户的PK而不是他的名字和姓氏来解决我的问题,并使用这些特征进行搜索。
但是,我按照以下内容引入了 explode 函数,以按照以下方式分隔连接值:

SQL查询选择 IndivId TenntNdx ,个人的名字姓氏,但是只有用户的名字和姓氏显示在表单的下拉列表中。使用以下代码发布匹配的 IndivId TenantNdx

$sql_FullName = "SELECT IndivId, TenantNdx, FirstName, LastName FROM InetDb.dbo.Individuals
                    ORDER BY FirstName ASC";
    $sql_run_FullName = odbc_exec($conn_general, $sql_FullName);    
while($FullName_row = odbc_fetch_array($sql_run_FullName)){
                            $IndivIndivId = $FullName_row['IndivId'];
                            $IndivTenantNdx = $FullName_row['TenantNdx'];
                            $IndivFirstName=$FullName_row['FirstName'];
                            $IndivLastName=$FullName_row['LastName'];
                            echo"<option value='$IndivIndivId $IndivTenantNdx'> $IndivFirstName $IndivLastName</option>";

上面的代码会将 IndivId TenantNdx (形成个人表的复合主键)发布到包含下面代码的页面,然后将该代码分开2个字段并通过PK搜索而不是个人姓名:

$IndivSurname = $_POST['IndivSurname'];
                $explode_sql = explode(" ", $IndivSurname, 2);
                    $ListIndivId = $explode_sql['0'];
                    $ListTenantId = $explode_sql['1'];