concat两列

时间:2014-10-24 15:32:17

标签: php mysql sql

如何连接两列?我需要它让FIRSTNAME和LASTNAME等于$ Employee_Name我尝试了不同的组合,但仍然无法正常工作。另外我知道这有一个巨大的SQL注入问题。我还不知道如何纠正我正在查找的问题。

<?php

    header('Content-Type: application/json');$db_conx = mysqli_connect("localhost", "root", "systems399","employees_db");
    $Employee_Name=  $_POST["Employee_Name"];
     $sql="SELECT * FROM  names WHERE FIRSTNAME='$Employee_Name'   ";
    $query= mysqli_query($db_conx, $sql);
    $row = mysqli_fetch_array($query, MYSQLI_ASSOC);
    $rc= $row["EMPLOYEE_NUMBER"];
    echo json_encode ($rc);
     ?>

我试过这种方式并且它没有用。

<?php
    header('Content-Type: application/json');
    $db_conx = mysqli_connect("localhost", "root", "systems399", "employees_db");
    $Employee_Name=  $_POST["Employee_Name"];
     $sql="SELECT * FROM  names WHERE FIRSTNAME='$Employee_Name' AND LASTNAME='$Employee_Name'";
    $query= mysqli_query($db_conx, $sql);
    $row = mysqli_fetch_array($query, MYSQLI_ASSOC);
    $rc= $row["EMPLOYEE_NUMBER"];
    echo json_encode ($rc);
 ?>

这是我的Javascript代码

$(document).ready(function() {
    $("#Employee_Name").change(function() {
        var Employee_Name = $(this).val();
        if (Employee_Name != '') {
            $.ajax({
                type: "post",
                url: "insert.php",
                data: "Employee_Name=" + Employee_Name,
                datatype: "json",
                success: function(data) {
                    $("#Employee_Number").val(data);
                    $('#Employee_Number').css("background-color", "#B3CBD6")
                    $('#Employee_Number').animate({
                        backgroundColor: "#ffffff"
                    });
                },
                error: function(response) {
                    alert("error scripting")
                }

            });
        } else {
            $("#Employee_Number").val("");
        }

    });
});

2 个答案:

答案 0 :(得分:2)

有两个可能性:

1

$sql = "SELECT CONCAT(`FIRSTNAME`, ' ', `LASTNAME`) AS `EmployeeName`, * FROM `names` HAVING `EmployeeName` = '".$Employee_name."'";

2

$sql = "SELECT * FROM `names` WHERE CONCAT(`FIRSTNAME`, ' ', `LASTNAME`) = '".$Employee_name."'";

答案 1 :(得分:1)

这是一个例子 -

$sql = "SELECT CONCAT(`FIRSTNAME`, ' ', `LASTNAME`) AS `EmployeeName`, * FROM `names` WHERE `FIRSTNAME` = '" . $First_Name . "' AND `LASTNAME` = '" . $Last_Name' . "' ";