显示来自数据库的字符串而不是值php

时间:2018-04-15 10:41:47

标签: php mysql html-select

我希望从以数据形式存储的数据库中获取数据。当我对回显值感到恐惧时,它会显示我不想要的值。

所以当我回显国家时,它会显示AX,我在寻找奥兰群岛的价值存储在数据库中。

      <form action="reg_page.php" id="regform">
    Username:<input type="text" name="username">
      Password:<input type="text" name="password">
    Confirm Password:<input type="text" name="conpassword">
    Country:    
   <select id="city" name="city" required>
      <option value="AX">Aland Islands</option>

    <input type="submit">
  </form>

include("db.php"); // include the connection object from the DBConnection.php 
if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    $inUsername = $_POST["username"]; 
    $inPassword = $_POST["password"]; 
    $encryptPassword = password_hash($inPassword, PASSWORD_DEFAULT); 
    $inCountry = $_POST["city"]; 
    $stmt = $db->prepare("INSERT INTO users(username,password,center) VALUES(?, ?,?)"); 
    $stmt->bind_param("sss", $inUsername , $encryptPassword, $inCountry); 
    $stmt->execute(); 
    $result = $stmt->affected_rows; 
    $stmt -> close(); 
    $db -> close(); 
    if($result > 0)

1 个答案:

答案 0 :(得分:0)

这是我的建议 在理想的世界中,您将拥有一个额外的数据库表,其中包含城市密钥和响应的城市名称。

// fiktive Table Cities with dummy values
id    |  Short |  Name
--------------
24    |  VIE   |  "Vienna"
53    |  AX    |  "Aland Islands"
....

然后,您将在表users中存储ID或密钥。然后,当您获得数据时,您将在两个表上进行连接。

更简单(但不是最好的)最终解决方案是“存储”那些硬编码为数组的对,您可以在输出时引用它:

// array with cities you need
$cities = Array("AX"=>"Aland Island", "VIE"=>"Vienna", ....);

// data coming from database
$cityCode = "AX"; 
// would be something like 
$cityCode = $row['city'];

// then you can do:
echo $cities[$cityCode]; // will output "Aland Island"