使用从另一种形式的表单提交中检索的数据[PHP]

时间:2018-03-15 10:08:38

标签: php forms

我有一个小表单,允许用户查找用户数据,他们只需输入用户ID,然后从一系列表中检索数据并显示这些数据。

我想要的逻辑是按一个按钮来获取数据,按另一个按钮来使用它。

以下是表格:

enter image description here

表单上的一个按钮称为获取数据,另一个按钮称为Onboard此用户。

所以每个按钮都有一个非常基本的

if(isset($_POST['nameofbutton']))
{   
    // Get data
}

if(isset($_POST['nameofbutton']))
{   
    // Send data
}

一个按钮使用此脚本获取数据

if(isset($_POST['submit']))
{   
    // Set some variables if necessary
    $id = $_POST['id'];

    // Write sql statement with ? as placeholders for any values
    $sql = "SELECT * 
            FROM tblInvestor 
            LEFT JOIN tblReyker ON tblInvestor.invUserId = tblReyker.ReyNPI_Id 
            LEFT JOIN tblDeclarations ON tblInvestor.invUserId = tblDeclarations.invUserId 
            WHERE tblInvestor.invUserId = ?";

    // Prepare the SQL statement using the database connection parameter
    if($stmt = $dbconINV->prepare($sql))
    {
        // Bind any necessary variables 
        if($stmt->bind_param('s', $id))
        {
            $result = $stmt->execute();

            // If the statement ran successfully
            if($result)
            {
                $result = $stmt->get_result();

                if($result->num_rows >= 1)
                {
                    while($row = $result->fetch_assoc())
                    {
                        // If there are result get them here
                        //
                        $userId = $row['invUserId'];
                        //
                        $email = $row['invUserEmail'];
                        // [Not Encrypted]
                        $title = $row['invUserTitle'];
                        // [Encrypted]
                        $forename = $row['invUserForename'];
                        // [Encrypted]
                        $surname = $row['invUserSurname'];
                        // [Not encrypted]
                        $countryOfBirth = $row['ReyCountryOfBirth'];
                        //
                        $emailType = $row['ReyEmailType'];
                        //
                        $dateOfBirth = $row['ReyDateofbirth'];
                        //
                        $nationalInsurance = $row['ReyNI']; 
                        //
                        $primaryAddress = $row['ReyPrimaryAddress'];
                        //
                        $primaryTelephone = $row['ReyPrimaryTelephone'];    
                        //
                        $bankAccountDetails = $row['ReyBA'];    
                        //
                        $citizenshipDetails = $row['ReyCitizenship'];
                        //
                        $planType = $row['ReyPlanType'];
                        //
                        $externalPlanId = $row['ReyExtPlanID'];

                        if($forename != "")
                        {
                            $forename = $security->decrypt($forename);
                        }

                        if($surname != "")
                        {
                            $surname = $security->decrypt($surname);
                        }

                        if($dateOfBirth != "")
                        {
                            $dateOfBirth = $security->decrypt($dateOfBirth);
                        }

                        if($nationalInsurance != "")
                        {
                            $nationalInsurance = $security->decrypt($nationalInsurance);
                        }

                        if($primaryAddress != "")
                        {
                            $primaryAddress = $security->decrypt($primaryAddress);
                            $primaryAddressDecoded = json_decode($primaryAddress, true);
                        }

                        if($primaryTelephone != "")
                        {
                            $primaryTelephone = $security->decrypt($primaryTelephone);
                            $primaryTelephoneDecoded = json_decode($primaryTelephone, true);
                        }

                        if($bankAccountDetails != "")
                        {
                            $bankAccountDetails = $security->decrypt($bankAccountDetails);
                            $bankAccountDetailsDecoded = json_decode($bankAccountDetails, true); 
                        }

                        if($citizenshipDetails != "")
                        {
                            $citizenshipDetails = $security->decrypt($citizenshipDetails);
                            $citizenshipDetailsDecoded = json_decode($citizenshipDetails, true);
                        }

                        echo "User ID " . $userId . "<br />";
                        echo "Plan ID " . $planType . "<br />";
                        echo "External Plan ID " . $externalPlanId . "<br />";
                        echo "Email: " . $email . "<br />";
                        echo "Title: " . $title . "<br />";
                        echo "Forename: " . $forename . "<br />";
                        echo "Surname: " . $surname . "<br />";
                        echo "Country of birth: " . $countryOfBirth . "<br />";
                        echo "Email type: " . $emailType . "<br />";
                        echo "Date of birth: " . $dateOfBirth . "<br />";
                        echo "National Insurance Number: " . $nationalInsurance . "<br />";

                        $_SESSION['userId'] = $userId;
                        $_SESSION['planType'] = $planType;
                        $_SESSION['externalPlanId'] = $externalPlanId;
                        $_SESSION['title'] = $title;
                        $_SESSION['forename'] = $forename;
                        $_SESSION['surname'] = $surname;
                        $_SESSION['countryOfBirth'] = $countryOfBirth;
                        $_SESSION['emailType'] = $emailType;
                        $_SESSION['dateOfBirth'] = $dateOfBirth;
                        $_SESSION['nationalInsurance'] = $nationalInsurance;
                        $_SESSION['address'] = $primaryAddressDecoded;
                        $_SESSION['citizenship'] = $citizenshipDetailsDecoded;
                        $_SESSION['telephone'] = $primaryTelephoneDecoded;
                        $_SESSION['bankAccount'] = $bankAccountDetailsDecoded;

                        // Address
                        foreach($primaryAddressDecoded as $addressKey => $addressValue)
                        {
                            echo $addressKey . " " . $addressValue . "<br />";
                        }

                        // Address
                        foreach($citizenshipDetailsDecoded as $addressKey => $addressValue)
                        {
                            echo $addressKey . " " . $addressValue . "<br />";
                        }

                        // Address
                        foreach($primaryTelephoneDecoded as $addressKey => $addressValue)
                        {
                            echo $addressKey . " " . $addressValue . "<br />";
                        }

                        // Address
                        foreach($bankAccountDetailsDecoded as $addressKey => $addressValue)
                        {
                            echo $addressKey . " " . $addressValue . "<br />";
                        }
                    }
                }
                else // the statement returned 0 results
                {
                    // Deal with the nothingness
                    echo "No data found";
                }
            }
            else // the sql didnt execute
            {
                // Somethings gone wrong here
                echo "No execution";
            }
        }
        else // the binding was wrong
        {
            // Check your bindings
            echo "Binding error";
        }   
    }
    else // There was an error preparing the sql statement (its wrong)
    {
        // the sql is wrong
        echo "SQL error " . $dbconINV->error;
    }
}

有些数据是加密的,所以我解密了,还有一些数据是JSON数组,因此我使用json_decode()。获得数据后,我将其全部存储在当前会话中。

另一个按钮使用会话中的数据进行API调用

if(isset($_POST['onboard']))
{
    $userId = $_SESSION['userId'];
    $planType = $_SESSION['planType'];
    $externalPlanId = $_SESSION['externalPlanId'];
    $title = $_SESSION['title']; 
    $forename = $_SESSION['forename']; 
    $surname = $_SESSION['surname']; 
    $countryOfBirth = $_SESSION['countryOfBirth']; 
    $emailType = $_SESSION['emailType']; 
    $dateOfBirth = $_SESSION['dateOfBirth']; 
    $nationalInsurance = $_SESSION['nationalInsurance']; 
    $primaryAddressDecoded = $_SESSION['address']; 
    $citizenshipDetailsDecoded = $_SESSION['citizenship']; 
    $primaryTelephoneDecoded = $_SESSION['telephone']; 
    $bankAccountDetailsDecoded = $_SESSION['bankAccount']; 

    // Create an array to work with
    $onboardingData = array(
        // Generic details
        "Title" => $title,
        "Forenames" => $forename,
        "Surname" => $surname,
        "CountryOfBirth" => $countryOfBirth,
        "EmailAddress" => $email,
        "EmailType" => $emailType,
        "BirthDate" => $dateOfBirth,
        "Suffix" => null,
        "NationalInsuranceNumber" => $nationalInsurance,

        // Primary address
        "PrimaryAddress" => $primaryAddress,

        // Additional addresses (as an array)
        "AdditionalAddresses" => null,

        // Primary telephone
        "PrimaryTelephone" => $primaryTelephone,

        // Additional telephone
        "AdditionalTelephone" => null,

        // Bank accounts
        "BankAccount" => $bankAccountDetails,

        // Primary citizenship
        "PrimaryCitizenship" => $citizenshipDetails,

        "AdditionalCitizenship" => null,

        "ExternalCustomerId" => $userId,
        "ExternalPlanId" => $externalPlanId,
        "PlanType" => $planType
    );

    // Ensure the array has data in it
    if(!empty($onboardingData)) 
    {
        // Usually where I do API call
        die(var_dump($onboardingData));
    }
}

我的问题是,当我尝试将已解码的JSON数组添加到会话中时,它们会被转储为Array,因此当我尝试构建$onboardingData时,数组为NULL。

我是否过于复杂?

1 个答案:

答案 0 :(得分:1)

PHP会话可以容纳数组,但请记住PHP不支持对象并通过它自己的类“stdClass”处理它们。

将JSON字符串存储为一个值并按需解码它并不罕见。

$_SESSION['mySession'] = '{"name":"Matt", "bestAnswer":true}';


$mySession = json_decode($_SESSION['mySession'], true); // true because I prefer arrays in PHP
$name = $mySession['name'];

没有繁琐的数组要求:

$mySession = json_decode($_SESSION['mySession']);
$name = $mySession->name;