PHP会话仅显示变量的一部分

时间:2016-05-18 13:47:38

标签: php session variables

我遇到的问题是,当我在会话数组中保存变量时,它只存储第一个单词。也就是说,如果它是'公司一',它只会保存'公司'。变量来自选择列表:

function listCompany() {
    include 'includes/connection.php';

    $stmt = $conn->prepare("SELECT CompanyName FROM Portal.company ORDER BY CompanyName ASC");
    $stmt->execute();
    $stmt->bind_result($col1);

    ?>

    <select name="CompanyName">
    <?

    $blank = "";
    echo "<option value=" . $blank . ">  </option>";
    while ($stmt->fetch()) {
        echo "<option value=" . $col1 . ">" . $col1 . "</option>";
    }

    ?>
    </select>
<?
}

然后存储在会话中:

$_SESSION['NewOrder'] = $array2 = array(
    "CompanyName" => $_POST['CompanyName'], 

当我回显变量时,我只得到第一个字。我有什么想法吗?

2 个答案:

答案 0 :(得分:3)

您错过了值周围的引号。它应该是

echo "<option value=\"$blank\">  </option>";
while ($stmt->fetch()) {
    echo "<option value=\"$col1\">$col1</option>";
}

在您的代码中,HTML输出将是

<option value=Company one>Company one</option>
              ^-----^ <-- this part is taken as value the rest 
                          after space is invalid HTML so gets ignored

应该是

<option value="Company one">Company one</option>

答案 1 :(得分:1)

问题在于这条线,

echo "<option value=" . $col1 . ">" . $col1 . "</option>";

您没有用单引号引用value。它应该是,

echo "<option value='" . $col1 . "'>" . $col1 . "</option>";