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

标签 php mysql pdo

我在这里使用 php mysql pdo 并尝试连接 fname 和 lname 但没有任何进展遇到 {"error":true,"error_msg":"Unknown error occurred in registration!"} ..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;
}

最佳答案

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

关于php - 字符串连接不起作用..!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34469092/

相关文章:

php - 时间戳更改而不修改数据库中的字段

mysql - node-mysql 不做任何事情就崩溃了

php - 代码重复 (PHP)

php - 条件 PDO bindParam 更新

php - 何时使用 trigger_error() 与 throw new Error()?

php - 如何在 PHP 的 GD 库中为文本添加光晕或阴影?

php - $_POST 错误

php - 将查询传递给 PDO 时出错

PHP PDO 返回 SELECT FOUND_ROWS() 不一致的结果

javascript - ajax php javascript 集成不起作用