使用AES_DECRYPT构建PHP选项列表。

时间:2016-03-13 11:16:47

标签: php pdo

我正在尝试从数据库构建一个选项列表,当它被选中时,它会查找从不同数据库发出的所有项目。这很有效,直到我不得不用AES_DECRYPT()解密。存储该值以便查找仍然有效,但没有可见的名称。

<form>
            Select a name and will list all items currently issued to that person. Press return to return an item.      
        <select id="list" name="users" onchange="showUser(this.value)">
    <?php
    $servername = "";
    $username = "";
    $password = "";
    $dbname = "";
    $aeskey = 'key';
        $pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        #Set Error Mode to ERRMODE_EXCEPTION.
        $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  

    $stmt = $pdo->prepare('SELECT id, AES_DECRYPT(Fname,"'.$aeskey.'"),AES_DECRYPT(Sname,"'.$aeskey.'") FROM  `name` ORDER BY  `Sname` DESC ');
    $stmt->execute();
       while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo '<option id ="'.$row['id'].'"value="'.$row['id'].'">'.$row['Fname']." ".$row['Sname'].'</option>';
       }
     ?>
 </select> 
            <div id="textt"><b>items listed here.</b></div>
            <br />
        </form>

如果我将$stmt = $pdo->prepare更改为 ('SELECT id, Fname, Sname FROM 'name' ORDER BY 'Sname' DESC ');它工作正常,但所有名称都已加密。我已经尝试将AES_DECRTYPT移动到while语句中而没有运气。我还检查了phpMyAdmin中的select语句,它运行正常。 在此先感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我通过在这里搜索大量的线程并结合答案找到了答案。

 <form>
    Select a name and will list all items currently issued to that person. Press return to return an item.      
    <select id="list" name="users" onchange="showUser(this.value)">
    <?php
    $servername = "";
    $username = "";
    $password = "";
    $dbname = "";
    $aeskey = 'key';
        $pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        #Set Error Mode to ERRMODE_EXCEPTION.
        $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  

    $stmt = $pdo->prepare('SELECT id, CAST(AES_DECRYPT(Fname,"'.$aeskey.'")
    AS CHAR(50)) fname_decrypt,CAST(AES_DECRYPT(Sname,"'.$aeskey.'")AS CHAR (50)) sname_decrypt FROM  `name` ORDER BY  `Sname` DESC ');
    $stmt->execute();
       while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo '<option id ="'.$row['id'].'"value="'.$row['id'].'">'.
    $row['Fname_decrypt']." ".$row['Sname_decrypt'].'</option>';
       }
     ?>
 </select> 
            <div id="text"><b>items listed here.</b></div>
            <br />
        </form>

使用CAST(AES_DECRYPT(fname,"'.$aeskey.'")AS CHAR(50)) fname_decrypt然后查找$row['fname_decrypt']我得到想要的结果。