字符串连接不起作用..!

时间:2015-12-26 06:30:47

标签: php mysql pdo

我在这里使用php mysql pdo并尝试连接fname和lname但没有遇到任何问题{“error”:true,“error_msg”:“注册时发生了未知错误!”} ..plzz帮助我如果我错了,请原谅我

.PHP

<?php
/*
starts with database connection 
and gives out the result of query
in json format
*/

        require_once 'DB_Functions.php';
        $db = new DB_Functions();

        // json response array
        $response = array("error" => false);
        //proceed if fields are not empty 
        if (!empty($_POST['salutation']) && !empty($_POST['fname']) && !empty($_POST['mname']) && !empty($_POST['lname']) && !empty($_POST['pob']) && !empty($_POST['dob']) && !empty($_POST['qualification']) && !empty($_POST['pg']) && !empty($_POST['pgy']) && !empty($_POST['graduation']) && !empty($_POST['gy']) && !empty($_POST['schooling']) && !empty($_POST['sy']) && !empty($_POST['religion']) && !empty($_POST['caste']) && !empty($_POST['subcaste']) && !empty($_POST['familyname']) && !empty($_POST['fathername']) && !empty($_POST['mothername']) && !empty($_POST['brothers']) && !empty($_POST['sisters'])){

            //reciving the post parameters
             $salutation =$_POST['salutation'];
             $fname = trim($_POST['fname']);
             $mname = trim($_POST['mname']);
             $lname = trim($_POST['lname']);
             $pob = trim($_POST['pob']);
             $dob = trim($_POST['dob']);
             $qualification = trim($_POST['qualification']);
             $pg = trim($_POST['pg']);
             $pgy = trim($_POST['pgy']);
             $graduation = trim($_POST['graduation']);
             $gy = trim($_POST['gy']);
             $schooling = trim($_POST['schooling']);
             $sy = trim($_POST['sy']);
             $religion = trim($_POST['religion']);
             $caste = trim($_POST['caste']);
             $subcaste = trim($_POST['subcaste']);
             $familyname = trim($_POST['familyname']);
             $fathername = trim($_POST['fathername']);
             $mothername = trim($_POST['mothername']);
             $brothers = trim($_POST['brothers']);
             $sisters = trim($_POST['sisters']);
             /*
             validation process
             begins from here
             */
                    // create a new user profile
                     $user = $db->storeUserProfile($salutation, $fname, $mname, $lname, $pob, $dob, $qualification, $pg, $pgy, $graduation, $gy, $schooling, $sy, $religion, $caste, $subcaste, $familyname, $fathername, $mothername, $brothers, $sisters);
                                if ($user){
                                        // user stored successfully as post params passed
                                        $response["error"] = false;
                                        $response["uid"] = $user["id"];
                                        $response["user"]["salutation"] = $user["salutation"];
                                        $response["user"]["fname"] = $user["fname"];
                                        $response["user"]["mname"] = $user["mname"];
                                        $response["user"]["lname"] = $user["lname"];
                                        $response["user"]["pob"] = $user["pob"];
                                        $response["user"]["dob"] = $user["dob"];
                                        $response["user"]["qualification"] = $user["qualification"];
                                        $response["user"]["pg"] = $user["pg"];
                                        $response["user"]["pgy"] = $user["pgy"];
                                        $response["user"]["graduation"] = $user["graduation"];
                                        $response["user"]["gy"] = $user["gy"];
                                        $response["user"]["schooling"] = $user["schooling"];
                                        $response["user"]["sy"] = $user["sy"];
                                        $response["user"]["religion"] = $user["religion"];
                                        $response["user"]["caste"] = $user["caste"];
                                        $response["user"]["subcaste"] = $user["subcaste"];
                                        $response["user"]["familyname"] = $user["familyname"];
                                        $response["user"]["fathername"] = $user["fathername"];
                                        $response["user"]["mothername"] = $user["mothername"];
                                        $response["user"]["brothers"] = $user["brothers"];
                                        $response["user"]["sisters"] = $user["sisters"];
                                        $response["user"]["uuid"] = $user["unique_id"];
                                        $response["user"]["created_at"] = $user["created_at"];
                                        $response["user"]["updated_at"] = $user["updated_at"];
                                        echo json_encode($response);
                                } else {
                                         // user failed to store
                                        $response["error"] = true;
                                        $response["error_msg"] = "Unknown error occurred in registration!";
                                        echo json_encode($response);
                                }
                }else{
                //missing the required fields
                $response["error"] = true;
                $response["error_msg"] = "Please fill all the required parameters!";
                echo json_encode($response);
        }
?>

这是使用pdo的数据库部分。

PHP

          public function storeUserProfile($salutation, $fname, $mname, $lname, $pob, $dob, $qualification, $pg, $pgy, $graduation, $gy, $schooling, $sy, $religion, $caste, $subcaste, $familyname, $fathername, $mothername, $brothers, $sisters){  
    try {
        $characters = '0123456789';
        $uuid = '';
        $random_string_length = 6;
    for ($i = 0; $i < $random_string_length; $i++) {
        $uuid .= $characters[rand(0, strlen($characters) - 1)];
    }
       $sql = "INSERT INTO profile_info(salutation, fname, mname, lname, fullname, pob, dob, qualification, pg, pgy, graduation, gy, schooling, sy, religion, caste, subcaste, familyname, fathername, mothername, brothers, sisters, unique_id, created_at) VALUES ( '$salutation', '$fname', '$mname', '$lname', '$fname'.', '.'$lname', '$pob', '$dob', '$qualification', '$pg', '$pgy', '$graduation', '$gy', '$schooling', '$sy', '$religion', '$caste', '$subcaste', '$familyname', '$fathername', '$mothername', '$brothers', '$sisters', '$uuid', NOW())";
        $dbh = $this->db->prepare($sql);

        if($dbh->execute()){
            //concatenate the strings 
            $sql = "UPDATE profile_info SET fullname = CONCAT(fname, ', ', lname)";
            $dbh = $this->db->prepare($sql);
            $dbh->execute();
            // get user details
            $sql = "SELECT * FROM profile_info WHERE familyname = '$familyname' LIMIT 1";
            $dbh = $this->db->prepare($sql);
            $result = $dbh->execute();
            $rows = $dbh->fetch();
            $n = count($rows);
            if($n){
                return $rows;
            }
        }
    }
    catch (Exception $e) {
        die('Error accessing database: ' . $e->getMessage());
    }
    return false;
}

2 个答案:

答案 0 :(得分:1)

INSERT查询中名字和姓氏的串联不正确。使用$fullname变量指定人员的全名,并在INSERT查询中使用该变量。这样您就不必更新该行,因为您已经插入了具有正确全名的行。

您的代码应该是这样的:

// your code

$fullname = $fname . ", " . $lname;
$sql = "INSERT INTO profile_info(salutation, fname, mname, lname, fullname, pob, dob, qualification, pg, pgy, graduation, gy, schooling, sy, religion, caste, subcaste, familyname, fathername, mothername, brothers, sisters, unique_id, created_at) VALUES ( '$salutation', '$fname', '$mname', '$lname', '$fullname', '$pob', '$dob', '$qualification', '$pg', '$pgy', '$graduation', '$gy', '$schooling', '$sy', '$religion', '$caste', '$subcaste', '$familyname', '$fathername', '$mothername', '$brothers', '$sisters', '$uuid', NOW())";
$dbh = $this->db->prepare($sql);

if($dbh->execute()){
    // get user details
    $sql = "SELECT * FROM profile_info WHERE familyname = '$familyname' LIMIT 1";
    $dbh = $this->db->prepare($sql);
    $result = $dbh->execute();
    $rows = $dbh->fetch();
    $n = count($rows);
    if($n){
        return $rows;
    }
}

// your code

答案 1 :(得分:0)

如果我正确理解了该问题,则不会插入值,因为您正在执行SELECT语句。 SELECT语句不会修改表数据。你会做这样的事情:

UPDATE profile_info SET fullname = CONCAT(fname, ', ', lname);

注意,这会更新整个表....

这将填充一个预先存在的列,其中新的连接值由每行的fname和lname值组成。

当然,如果您的表目前没有全名列,请添加一个:

ALTER TABLE profile_info ADD COLUMN fullname varchar(25);

<强>更新 把这条线拿出来:

$sql = UPDATE profile_info SET fullname = CONCAT(fname, ', ', lname);

并更改此行:

$sql = "INSERT INTO profile_info(salutation, fname, mname, lname, fullname, pob, dob, qualification, pg, pgy, graduation, gy, schooling, sy, religion, caste, subcaste, familyname, fathername, mothername, brothers, sisters, unique_id, created_at) VALUES ( '$salutation', '$fname', '$mname', '$lname', '$fname'.', '.'$lname', '$pob', '$dob', '$qualification', '$pg', '$pgy', '$graduation', '$gy', '$schooling', '$sy', '$religion', '$caste', '$subcaste', '$familyname', '$fathername', '$mothername', '$brothers', '$sisters', '$uuid', NOW())";

您会看到我在列列表中添加了'fullname',这在值列表中添加:'$fname'.', '.'$lname',

使用PHP的串联运算符.

实现此目的的正确方法是简单地连接值并在插入其余值的同时插入它们。让我知道这是否适合你。

附注,编辑原始代码确实会使编辑在编辑后进入的观众更加困惑。考虑添加有关代码编辑的注释,而不是编辑原始示例。