user5444025
user5444025

Reputation:

Concatenation of strings not working..!

I am using php mysql pdo in here and trying to concatenate fname and lname but nothing going right am encountering {"error":true,"error_msg":"Unknown error occurred in registration!"} ..plzz help me out,pardon me if am wrong

.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);
        }
?>

this is the database part using 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;
}

Upvotes: 1

Views: 847

Answers (2)

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

The concatenation of first name and last name in your INSERT query is incorrect. Use a $fullname variable to specify full name of the person, and use that variable in your INSERT query. That way you won't have to update the row because you have already inserted the row with the correct full name.

Your code should be like this:

// 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

Upvotes: 1

Redbeard011010
Redbeard011010

Reputation: 964

If I understand the issue properly, the values are not being inserted because you are executing, instead, a SELECT statement. SELECT statements do not modify table data. You would instead do something like this:

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

Note, this would update the entire table....

This will fill in a pre-existing column with the new concatenated value made from the fname and lname values of each row.

Of course, if your table does not currently have a column for fullname, add one:

ALTER TABLE profile_info ADD COLUMN fullname varchar(25);

UPDATE Take this line out:

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

And change this line:

$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())";

You'll see I added 'fullname' in the columns list, and this in the values list: '$fname'.', '.'$lname',

using PHP's concatenation operator .

The correct way to accomplish this is to simply concatenate the values and insert them at the very same time you insert the rest of the values. Let me know if that does it for you.

A side note, editing your original code does make the question more confusing for viewers who came in after the edits were made. Consider adding notes about any edits to the code, instead of editing the original example.

Upvotes: 0

Related Questions