php - Word 'Variable' 出现在 URL 中,而不是数字 ID - PHP

标签 php mysql

我有一个运行良好的脚本,但我看不到错误。该脚本有两个功能。首先是在数据库中新建一个客户端,效果很好。脚本的第二部分(靠近底部)是为客户端更新数据库(如果存在)。

该页面发送客户端 ID 以进行编辑,但该脚本在某处停止响应。提交时,会加载 view-client.php 页面,但 URL 显示 'client=Array',而不是例如 'client=1'。我想我已经把它缩小到控制注册时输入的新密码的 PHP,都称为 $password 和 $passKey。

这意味着将更新的数据保存到数据库中,并在提交到具有正确 ID 的 view-client.php 页面时重定向用户。任何帮助是极大的赞赏!

编辑

表格和脚本供引用...

<?PHP
include('../core/init.php');
require_once('dbConfig.php');

$randomstring = '';
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

for ($i = 0; $i < 5; $i++) {
    $randomString .= $characters[rand(0, strlen($characters) - 1)];
}
//$generatedId = "SPI-E7HN2SBIIF5W";
$generatedId = 'SPI-'.$randomString;

//Prepare select query
$statement = $db->prepare("SELECT client_unique_id FROM clients WHERE client_unique_id = ? LIMIT 1");

//Determine variable and then bind that variable to a parameter for the select query ?
$id = $generatedId;
$statement->bind_param('s', $id);

//Execute and store result so that num_rows returns a value and not a 0
$statement->execute();
$statement->store_result();

//Bind result to a variable for easy management afterwards
$statement->bind_result($clientId);

// Generate a random ID for the user if the previously generated one already exists
if($statement->num_rows > 0) {
    $randomstring = '';
    $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

    for ($i = 0; $i < 0; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    $generatedId = 'SPI-'.$randomString;
    //echo $generatedId;
}

$client = $_POST['createClientId'];
$insertId = $_POST['insertId'];
$passKey = $_POST['PassKey'];
$firstName = $_POST['FirstName'];
$surname = $_POST['Surname'];
$businessName = $_POST['BusinessName'];
$addressLine1 = $_POST['AddressLine1'];
$addressLine2 = $_POST['AddressLine2'];
$townCity = $_POST['TownCity'];
$county = $_POST['County'];
$postcode = $_POST['Postcode'];
$telephone = $_POST['Telephone'];
$mobile = $_POST['Mobile'];
$userName = $_POST['Username'];
$accountType = $_POST['AccountType'];
$email = $_POST['EmailAddress'];
$password = $_POST['Password'];
$additionalInfo = $_POST['AdditionalInformation'];

foreach($passKey as $key => $val) {
    if($password[$key] == '' || !$password[$key]){
        $randomstring = '';
        $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

        for ($i = 0; $i < 18; $i++) {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }
        $generatedPassword = $randomString;

        /* Two create a Hash you do */
        $password = $bcrypt->genHash($generatedPassword);
        //$password = sha1($generatedPassword);
    } else {
        $password = $bcrypt->genHash($password[$key]);
        //$password = sha1($password[$key]);
    }
    if(!$client[$key]) {    
        if($_SESSION['member_unique_id']=="supermember") {
            $member_unique_ids="ISPI-ADMIN";
        } else {
            $member_unique_ids = $_SESSION['member_unique_id'];
        }

        if ($stmt = $db->prepare("INSERT clients (client_id, member_unique_id, client_unique_id, client_key, client_first_name, client_last_name, client_organisation_name, client_business_type, client_username, client_address_line_1, client_address_line_2, client_town, client_county, client_postcode, client_telephone, client_mobile, client_email_address, client_password, client_additional_info) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) {

            $stmt->bind_param("sssssssssssssssssss", $insertId, $member_unique_ids, $generatedId, $passKey[$key], $firstName[$key], $surname[$key], $businessName[$key], $accountType[$key], $userName[$key], $addressLine1[$key], $addressLine2[$key], $townCity[$key], $county[$key], $postcode[$key], $telephone[$key], $mobile[$key], $email[$key], $password, $additionalInfo[$key]);
            $stmt->execute();
            $stmt->close();

            echo $db->insert_id;
        } else {
            echo "ERROR: Could not prepare Insert SQL statement.";
        }
    } else {
        if ($stmt = $db->prepare("UPDATE clients SET client_first_name = ?, client_last_name = ?, client_organisation_name = ?, client_business_type = ?, client_username = ?, client_address_line_1 = ?, client_address_line_2 = ?, client_town = ?, client_county = ?, client_postcode = ?, client_telephone = ?, client_mobile = ?, client_email_address = ?, client_additional_info = ? WHERE client_id = ?")) {

            $stmt->bind_param("ssssssssssssssi", $firstName[$key], $surname[$key], $businessName[$key], $accountType[$key], $userName[$key], $addressLine1[$key], $addressLine2[$key], $townCity[$key], $county[$key], $postcode[$key], $telephone[$key], $mobile[$key], $email[$key], $additionalInfo[$key], $client);
            $stmt->execute();
            $stmt->close();
            echo $client;
        } else {
            echo "ERROR: Could not prepare Update SQL statement.";
        }
    }
}



<head>

<!--START-->
<?PHP include('../layout/start.php'); ?>
<!--/START-->

<script>
$(document).ready(function(){

    function editClient(form) {
        var $this = $(form);
        var string = $this.serialize();
            $.ajax({
                type: "POST",
                url: "../includes/db-edit-client.php",
                data: string,
                cache: false,
                success: function(data){
                    setTimeout(function () {
                        window.location = "view-client.php?member=<?=$member_unique_id?>&client="+data;
                    }, 0);
                }
            });
    }

    $('body').on('click', '#updateClientDetails', function(e) {
        editClient("#editClientForm");
    });

});
</script>
</head>

<body>
<!--MAIN ELEMENTS-->
<?PHP include('../layout/header.php'); ?>
<?PHP include('../layout/menu.php'); ?>
<div class="pageWrapper shrink">
    <div class="pageContainer">
<!--/MAIN ELEMENTS-->

    <!--START FORM-->
    <form id="editClientForm">
        <input type="hidden" name="createClientId[]" value="<?=$_GET['client']?>">
        <input type="hidden" name="PassKey[]">

        <div class="titleBox clientBlue">

            Edit Client - <?=$client_organisation_name?>
            <button id="updateClientDetails" class="mainButton clientBlue">Update Client</button>
        </div>
        <div class="breadcrumbs">
            <ul id="breadcrumbsList">
                <li><a href="home.php">Home</a></li>
                <li><a href="search-clients.php">Clients</a></li>
                <li><a href="edit-client.php?client=<?=$client?>&member=<?=$member_unique_id?>">Edit Client - <?=$client_organisation_name?></a></li>
            </ul>
        </div>

        <!--TABLE-->
        <div class="tableContainer">
            <div class="tableHeader clientBlue">
                <div class="col12 colNoPaddingLeft">Client Details</div>
            </div>
            <div class="tableBody">
                <div class="rowTight">
                    <div class="col3 colNoPaddingLeft"><input type="text" class="formInput" name="FirstName[]" placeholder="First name" autocomplete="off" value="<?=$client_first_name?>"></div>
                    <div class="col3"><input type="text" class="formInput" name="Surname[]" placeholder="Surname" autocomplete="off" value="<?=$client_last_name?>"></div>
                    <div class="col3"><input type="text" class="formInput" name="BusinessName[]" placeholder="Business name" autocomplete="off" value="<?=$client_organisation_name?>"></div>
                    <div class="col3 colNoPaddingRight"><input type="text" class="formInput" name="Username[]" placeholder="Username" autocomplete="off" value="<?=$client_username?>"></div>
                </div>
            </div>
        </div><!--END TABLE-->

        <!--TABLE-->
        <div class="tableContainer">
            <div class="tableHeader clientBlue">
                <div class="col12 colNoPaddingLeft">Contact Details</div>
            </div>
            <div class="tableBody">
                <div class="rowTight">
                    <div class="col3 colNoPaddingLeft"><input type="text" class="formInput" name="AddressLine1[]" placeholder="Address line 1" autocomplete="off" value="<?=$client_address_line_1?>"></div>
                    <div class="col3"><input type="text" class="formInput" name="AddressLine2[]" placeholder="Address line 2" autocomplete="off" value="<?=$client_address_line_2?>"></div>
                    <div class="col3"><input type="text" class="formInput" name="TownCity[]" placeholder="Town/city" autocomplete="off" value="<?=$client_town?>"></div>
                    <div class="col3 colNoPaddingRight"><input type="text" class="formInput" name="County[]" placeholder="County" autocomplete="off" value="<?=$client_county?>"></div>
                </div>
                <div class="rowTight">
                    <div class="col3 colNoPaddingLeft"><input type="text" class="formInput" name="Postcode[]" placeholder="Postcode" autocomplete="off" value="<?=$client_postcode?>"></div>
                    <div class="col3"><input type="text" class="formInput" name="Telephone[]" placeholder="Telephone" autocomplete="off" value="<?=$client_telephone?>"></div>
                    <div class="col3"><input type="text" class="formInput" name="Mobile[]" placeholder="Mobile" autocomplete="off"  value="<?=$client_mobile?>"></div>
                    <div class="col3 colNoPaddingRight">&nbsp;</div>
                </div>
            </div>
        </div><!--END TABLE-->

        <!--TABLE-->
        <div class="tableContainer">
            <div class="tableHeader clientBlue">
                <div class="col12 colNoPaddingLeft">Account Details</div>
            </div>
            <div class="tableBody">
                <div class="rowTight">
                    <div class="col3 colNoPaddingLeft">
                        <select name="AccountType[]" class="formDropdown">
                            <option value="Business type" selected>Business type</option>
                            <?php
                                $types = array('Landlord', 'Tenant', 'Letting agent', 'Estate agent', 'Surveyors', 'Insurance', 'Other');
                                foreach ($types as $type) {
                                    $selected = $client_business_type == $type ? ' selected="selected"' : null;
                                    echo '<option value="'.$type.'"'.$selected.'>'.$type.'</option>';
                                }
                            ?>
                        </select>
                    </div>
                    <div class="col3"><input type="email" class="formInput" name="EmailAddress[]" placeholder="Email address" autocomplete="off" value="<?=$client_email_address?>"></div>
                    <div class="col3"><textarea placeholder="Additional information" name="AdditionalInformation[]" class="formInput"><?=$client_additional_info?></textarea></div>
                    <div class="col3 colNoPaddingRight">&nbsp;</div>
                </div>
            </div>
        </div><!--END TABLE-->

    </form><!--END FORM-->
    </div><!--END PAGE CONTAINER-->
</div><!--END PAGE WRAPPER-->

最佳答案

没有你实际的内容$_POST很难说发生了什么。

但是你最终通过执行 echo $client; 来完成你的脚本。 ,但之前你会这样做:if(!$client[$key])明确表示您期望 $client (您从 $_POST['createClientId'] 填充)成为一个数组(然后您将其作为字符串输出)。

确保在您的 echo声明您指向数组的相关键,或者您实际上是在回显所需的变量。

(我想 $bcrypt$db 是在前面声明的,在您的一个包含中,因为在检查时出现未定义。请注意您的字符大小写,您使用 $randomString 两者 $randomstring ;而 PHP对这类东西很宽容,养成不好的习惯)

关于php - Word 'Variable' 出现在 URL 中,而不是数字 ID - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41439870/

相关文章:

php - 通过发布的数组过滤数据?

PHP session 不更新

php - 将 fetch all 数组拆分为单独的数组

MySQL数据文件丢失

php - 如何在 Laravel 中对连接查询进行分块

mysql - 根据过滤器数据查询数据库中的产品重复

javascript - 包括 php 菜单,将事件类分配给单击的链接

php读取大文本文件日志

php - 变量的外部循环

mysql - 组织 MySQL 数据库信息的实用方法?