搜索多个值(如果存在)

时间:2017-02-23 15:45:36

标签: php sql

抱歉,我的英语很弱...... 我如何从db SQL中搜索多个值,以便有任何。 我可以搜索名称&&一家人在一起 我希望当用户搜索姓名和家人留空时正确返回结果 我怎么写这个

if (isset($_POST['searchname']) || isset($_POST['searchfamily'])) {
   $sql = "select * from myinfo WHERE name='{$_POST['searchname']}' && family='{$_POST['searchfamily']}' ORDER BY id DESC";
}
else {
   $sql = "select * from myinfo ORDER BY id DESC";
}

2 个答案:

答案 0 :(得分:0)

如果您想同时搜索两者,那么您还需要更改if。并在查询中将SELECT [Project5].[ID] AS [ID], [Columns left out for brevity...] [Project5].[Reference] AS [Reference] FROM ( SELECT [Extent1].[ID] AS [ID], [Extent1].[Claim_ID] AS [Claim_ID], [Extent1].[ClaimBatch_ID] AS [ClaimBatch_ID], [Extent1].[PurchasePrice] AS [PurchasePrice], [Join4].[Id1] AS [ID1], [Join4].[ClaimType_ID] AS [ClaimType_ID], [Join4].[Debtor_ID] AS [Debtor_ID], [Join4].[CustomerContractRevision_ID] AS [CustomerContractRevision_ID], [Join4].[Date] AS [Date], [Join4].[GrossAmount] AS [GrossAmount], [Join4].[OpenAmount] AS [OpenAmount], [Join4].[CreatedOn] AS [CreatedOn], [Join4].[IsProcessedByOpenAmountCalculator] AS [IsProcessedByOpenAmountCalculator], CASE WHEN((NOT(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL))) AND (NOT(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL))) AND (NOT(([Join4].[C13] = 1) AND ([Join4].[C13] IS NOT NULL))) AND (NOT(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)))) THEN '2X' WHEN(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)) THEN '2X0X' WHEN(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL)) THEN '2X1X' WHEN(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL)) THEN '2X2X' ELSE '2X3X' END AS [C1], CASE WHEN((NOT(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL))) AND (NOT(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL))) AND (NOT(([Join4].[C13] = 1) AND ([Join4].[C13] IS NOT NULL))) AND (NOT(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)))) THEN CAST(NULL AS BIT) WHEN(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)) THEN [Join4].[IsAppeared] WHEN(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL)) THEN CAST(NULL AS BIT) WHEN(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL)) THEN CAST(NULL AS BIT) END AS [C2], CASE WHEN((NOT(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL))) AND (NOT(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL))) AND (NOT(([Join4].[C13] = 1) AND ([Join4].[C13] IS NOT NULL))) AND (NOT(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)))) THEN CAST(NULL AS TINYINT) WHEN(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)) THEN CAST(NULL AS TINYINT) WHEN(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL)) THEN [Join4].[AdjustmentClaimReason_ID] WHEN(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL)) THEN CAST(NULL AS TINYINT) END AS [C3], CASE WHEN((NOT(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL))) AND (NOT(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL))) AND (NOT(([Join4].[C13] = 1) AND ([Join4].[C13] IS NOT NULL))) AND (NOT(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)))) THEN CAST(NULL AS INT) WHEN(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)) THEN CAST(NULL AS INT) WHEN(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL)) THEN [Join4].[User_ID] WHEN(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL)) THEN CAST(NULL AS INT) END AS [C4], CASE WHEN((NOT(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL))) AND (NOT(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL))) AND (NOT(([Join4].[C13] = 1) AND ([Join4].[C13] IS NOT NULL))) AND (NOT(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)))) THEN CAST(NULL AS INT) WHEN(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)) THEN CAST(NULL AS INT) WHEN(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL)) THEN CAST(NULL AS INT) WHEN(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL)) THEN [Join4].[CostClaimAnnouncement_ID] END AS [C5], CASE WHEN((NOT(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL))) AND (NOT(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL))) AND (NOT(([Join4].[C13] = 1) AND ([Join4].[C13] IS NOT NULL))) AND (NOT(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)))) THEN CAST(NULL AS DECIMAL(19, 4)) WHEN(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)) THEN CAST(NULL AS DECIMAL(19, 4)) WHEN(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL)) THEN CAST(NULL AS DECIMAL(19, 4)) WHEN(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL)) THEN [Join4].[DiscountFactor] END AS [C6], CASE WHEN((NOT(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL))) AND (NOT(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL))) AND (NOT(([Join4].[C13] = 1) AND ([Join4].[C13] IS NOT NULL))) AND (NOT(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)))) THEN CAST(NULL AS DATETIME2) WHEN(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)) THEN CAST(NULL AS DATETIME2) WHEN(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL)) THEN CAST(NULL AS DATETIME2) WHEN(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL)) THEN [Join4].[DiscountValidTo] END AS [C7], CASE WHEN((NOT(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL))) AND (NOT(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL))) AND (NOT(([Join4].[C13] = 1) AND ([Join4].[C13] IS NOT NULL))) AND (NOT(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)))) THEN CAST(NULL AS INT) WHEN(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)) THEN CAST(NULL AS INT) WHEN(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL)) THEN CAST(NULL AS INT) WHEN(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL)) THEN [Join4].[AppliedDiscountAdjustmentClaim_ID] END AS [C8], CASE WHEN((NOT(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL))) AND (NOT(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL))) AND (NOT(([Join4].[C13] = 1) AND ([Join4].[C13] IS NOT NULL))) AND (NOT(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)))) THEN CAST(NULL AS INT) WHEN(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)) THEN CAST(NULL AS INT) WHEN(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL)) THEN CAST(NULL AS INT) WHEN(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL)) THEN [Join4].[ExpiredDiscountAdjustmentClaim_ID] END AS [C9], CASE WHEN((NOT(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL))) AND (NOT(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL))) AND (NOT(([Join4].[C13] = 1) AND ([Join4].[C13] IS NOT NULL))) AND (NOT(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)))) THEN CAST(NULL AS VARCHAR(1)) WHEN(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)) THEN CAST(NULL AS VARCHAR(1)) WHEN(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL)) THEN CAST(NULL AS VARCHAR(1)) WHEN(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL)) THEN CAST(NULL AS VARCHAR(1)) ELSE [Join4].[Reference] END AS [C10], CASE WHEN((NOT(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL))) AND (NOT(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL))) AND (NOT(([Join4].[C13] = 1) AND ([Join4].[C13] IS NOT NULL))) AND (NOT(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)))) THEN CAST(NULL AS DATETIME2) WHEN(([Join4].[C14] = 1) AND ([Join4].[C14] IS NOT NULL)) THEN CAST(NULL AS DATETIME2) WHEN(([Join4].[C12] = 1) AND ([Join4].[C12] IS NOT NULL)) THEN CAST(NULL AS DATETIME2) WHEN(([Join4].[C11] = 1) AND ([Join4].[C11] IS NOT NULL)) THEN CAST(NULL AS DATETIME2) ELSE [Join4].[DueDate] END AS [C11], [Extent7].[Reference] AS [Reference] FROM [dbo].[ClaimBatchLine] AS [Extent1] INNER JOIN ( SELECT [Extent2].[Id] AS [Id1], [Columns left out for brevity...] FROM [dbo].[Claim] AS [Extent2] LEFT OUTER JOIN ( SELECT [Extent3].[Id] AS [Id], [Extent3].[CostClaimAnnouncement_ID] AS [CostClaimAnnouncement_ID], [Extent3].[DiscountFactor] AS [DiscountFactor], [Extent3].[DiscountValidTo] AS [DiscountValidTo], [Extent3].[AppliedDiscountAdjustmentClaim_ID] AS [AppliedDiscountAdjustmentClaim_ID], [Extent3].[ExpiredDiscountAdjustmentClaim_ID] AS [ExpiredDiscountAdjustmentClaim_ID], CAST(1 AS BIT) AS [C1] FROM [dbo].[CostClaim] AS [Extent3] ) AS [Project1] ON [Extent2].[Id] = [Project1].[Id] LEFT OUTER JOIN ( SELECT [Extent4].[Id] AS [Id], [Extent4].[IsAppeared] AS [IsAppeared], CAST(1 AS BIT) AS [C1] FROM [dbo].[InterestClaim] AS [Extent4] ) AS [Project2] ON [Extent2].[Id] = [Project2].[Id] LEFT OUTER JOIN ( SELECT [Extent5].[Id] AS [Id], [Extent5].[AdjustmentClaimReason_ID] AS [AdjustmentClaimReason_ID], [Extent5].[User_ID] AS [User_ID], CAST(1 AS BIT) AS [C1] FROM [dbo].[AdjustmentClaim] AS [Extent5] ) AS [Project3] ON [Extent2].[Id] = [Project3].[Id] LEFT OUTER JOIN ( SELECT [Extent6].[Id] AS [Id], [Extent6].[Reference] AS [Reference], [Extent6].[DueDate] AS [DueDate], CAST(1 AS BIT) AS [C1] FROM [dbo].[InvoiceClaim] AS [Extent6] ) AS [Project4] ON [Extent2].[Id] = [Project4].[Id] ) AS [Join4] ON [Extent1].[Claim_ID] = [Join4].[Id1] LEFT OUTER JOIN [dbo].[InvoiceClaim] AS [Extent7] ON [Join4].[Id1] = [Extent7].[Id] WHERE 1 = [Extent1].[ClaimBatch_ID] ) AS [Project5] ORDER BY [Project5].[ID] ASC OFFSET 0 ROWS FETCH NEXT 15000 ROWS ONLY; 更改为&&

and

修改

根据你的评论,试试这个:

if (isset($_POST['searchname']) && isset($_POST['searchfamily'])) {

$sql = "select * from myinfo WHERE `name`='{$_POST['searchname']}' AND family='{$_POST['searchfamily']}' ORDER BY id DESC";
    }
else {

$sql = "select * from myinfo ORDER BY id DESC";

}

答案 1 :(得分:0)

这里有3个主要问题..

第一个是WHERE name=现在.. name已经被mysql使用了,所以你不应该使用它。如果你使用它,就这样运行:

WHERE `name`=

您应该始终反复使用数据库表和列,以便从长远来看使生活更轻松。

第二个问题是您使用的&&应该是AND

第三个是你不应该把你的变量直接放到你的查询中,因为你打开了SQL注入。

现在我假设您使用$mysqli作为变量,但是,可能需要调整以适应您正在使用的正确变量:

if (isset($_POST['searchname']) || isset($_POST['searchfamily'])) {

    $searchName = $_POST['searchname'];
    $family = $_POST['searchfamily'];

    $sql = $mysqli->prepare("select * from `myinfo` WHERE `name` = ? OR `family`= ? ORDER BY `id` DESC");
    $sql->execute([$searchName, $family]);
} else {

    $sql = $mysqli->prepare("select * from `myinfo` ORDER BY `id` DESC");
    $sql->execute();

}