简体   繁体   English

(下拉菜单,Php 和 Mysql)在 Mysql 中存储值

[英](drop downs, Php and Mysql) with stored value in Mysql

I want to store the name of the city instead of the ID in my MySQL database.我想在我的 MySQL 数据库中存储城市名称而不是 ID。 The PHP function for building the city list is something like this:用于构建城市列表的 PHP function 是这样的:

public function getCity() {
    if(isset($this->attiva)) {
        $base = mysql_connect ('localhost', 'bolmdb', 'bolmdb');
        mysql_select_db ('bolmdb', $base);                                
        $result = mysql_query("SELECT * FROM city WHERE idstate=$_POST[idcity]");
        $city = '<option value="0">Select City...</option>';

        while($row = mysql_fetch_array($result)) {
            $city .= '<option value="' . $row['idcity'] . '">' . utf8_encode($row['nomecity']) . '</option>';
        }
        return $city;
    }
}

and the form HTML:以及 HTML 的形式:

</label>City:</label>
<select id="city" name="city" value="" ><br />
<option>Select City...</option>
</select>

So when the form is submitted, I get the value with $city = $_POST['city'] ;因此,当提交表单时,我得到$city = $_POST['city']的值; and it's stored in MySQL as an ID.它作为 ID 存储在 MySQL 中。 How can I store the name of the city in place of the ID?如何存储城市名称来代替 ID?

[EDIT:] Hi. [编辑:]嗨。 Thank you very much for your reply.非常感谢您的回复。 Maybe I didn't explain me very well.也许我没有很好地解释我。 My problem is that I need to keep $row['idcity'] in the option value to populate dynamically the drop down list with ajax, Obviously.我的问题是我需要将 $row['idcity'] 保留在选项值中,以使用 ajax 动态填充下拉列表,显然。 It's necessary that I connect the State Mysql table with the City Mysql table by ID field to get data: STATE TABLE:我有必要连接Z46A2A41CC6EC6E5204816A2D04634545DZ Z9EDB3C572B56B56B91542AF65948051868181Z与Z9EDB3C572B56B91542AF6515151515151515151515151515151515151515151515151515151515151515159888888888889888888888888888889898999士兰数意士

CREATE TABLE `state` (
  `idstate` int(4) NOT NULL auto_increment,
  `nomestate` varchar(50) NOT NULL default '',
  PRIMARY KEY  (`idregione`)
) TYPE=MyISAM AUTO_INCREMENT=161 ;

-- 
-- Dump dei dati per la tabella `state`
-- 

INSERT INTO `state` VALUES (1, 'Marche');
...

CITY TABLE:城市表:

CREATE TABLE `city` (
  `idcity` int(4) NOT NULL auto_increment,
  `nomecity` varchar(20) NOT NULL default '',
  `idstate` int(4) NOT NULL default '0',
  `siglacity` char(2) NOT NULL default '',
  PRIMARY KEY  (`idprovincia`)
) TYPE=MyISAM AUTO_INCREMENT=109 ;

-- 
-- Dump dei dati per la tabella `city`
-- 

INSERT INTO `city` VALUES (1, 'Ancona', 1, 'AN');
...

and two php funcions:和两个 php 函数:

public function getState()
 {
  if(isset($this->attiva))
  {
$base = mysql_connect ('localhost', 'bolmdb', 'bolmdb');
mysql_select_db ('bolmdb', $base);                                
    $result = mysql_query("SELECT * FROM state");
$state = '<option value="0">Select State...</option>';

while($row = mysql_fetch_array($result))
{
$state .= '<option value="' . $row['idstate'] . '">' . utf8_encode($row['nomestate']) . '</option>';
}           
return $state;
  }
 }

public function getCity()
 {
  if(isset($this->attiva))
  {
  $base = mysql_connect ('localhost', 'bolmdb', 'bolmdb');
mysql_select_db ('bolmdb', $base);                                
    $result = mysql_query("SELECT * FROM city WHERE idstate=$_POST[idstate]");
$city = '<option value="0">Select City...</option>';

while($row = mysql_fetch_array($result))
{
$city .= '<option value="' . $row['idcity'] . '">' . utf8_encode($row['nomecity']) . '</option>';
}           
return $city;
  }
 }

But in this way when I get the value with $city = $_POST['city'];但是以这种方式,当我用$city = $_POST['city'];获得价值时I store in the database the ID value.我将 ID 值存储在数据库中。 I thought to change IDSTATE with NOMESTATE in CITY table but I found that It was no better way to solve it and also It could create non-ASCII characters problems about which @Gerard remarks.我想在 CITY 表中用 NOMESTATE 更改 IDSTATE,但我发现这不是解决它的更好方法,而且它可能会产生关于 @Gerard 评论的非 ASCII 字符问题。 Lastly I wonder as a matter of curiosity if there is a way to get output function (in the specific case . utf8_encode($row['nomecity']). ) instead of the option value.最后,我想知道是否有办法获得 output function (在特定情况下是 .utf8_encode . utf8_encode($row['nomecity']). )而不是选项值。

First, you need to modify the field id, change it to varchar.首先需要修改字段id,改成varchar。 Replace id of city by his name in the database在数据库中用他的名字替换城市的id

I'm not sure and I would prefer to see your database table (not a database table field, as you have shown to Chumillas), but perhaps you should change this fragment我不确定,我希望看到您的数据库表(不是数据库表字段,正如您向 Chumillas 展示的那样),但也许您应该更改此片段

'<option value="' . $row['idcity'] . '">'

by this one通过这个

'<option value="' . utf8_encode($row['nomecity']) . '">'

inside your getCity() function.在你的 getCity() function 里面。

This will probably make that, when the form is submitted and you get the value with这可能会使,当提交表单并且您获得值时

$city = $_POST['city'];

you get the name of the city instead of the ID.你得到的是城市的名字而不是 ID。

EDIT: By the way, as the name of the city can have non-ASCII characters, I strongly recommend you to create another field to your table to store a representative name of the city but without them.编辑:顺便说一句,由于城市名称可以包含非 ASCII 字符,我强烈建议您在表中创建另一个字段来存储城市的代表名称,但没有它们。 For example, if you create a VARCHAR field named 'nickname', you should replace the mentioned line with this one:例如,如果您创建一个名为 'nickname' 的 VARCHAR 字段,则应将上述行替换为以下行:

'<option value="' . $row['nickname'] . '">'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM