在 MySQL ENUM 值中使用撇号将填充 HTML 组合框以进行数据库搜索 [英] Using Apostrophe in MySQL ENUM value that will populate HTML combo box for database search

查看:46
本文介绍了在 MySQL ENUM 值中使用撇号将填充 HTML 组合框以进行数据库搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先在我的 MySQL 列中创建一个带有撇号的 ENUM 值时遇到问题,然后使用该值通过 PHP 填充 HTML 组合框,然后使用所述值作为选择来搜索数据库.

I am having trouble first creating an ENUM value in my MySQL column with an apostrophe in it, then using the value to populate an HTML combo box via PHP, and then using said value as a selection to search the database.

这是我用来创建 ENUM 值的 SQL(我只显示有问题的值,而通常会有几个值):

ALTER TABLE  `primary_images` CHANGE  `imgClass`  `imgClass` ENUM(  'Robin\'s'  ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL

然而这是我在我的数据库列中的最终结果:

`imgClass` enum('Robin''s') DEFAULT NULL

因此我的第一个问题是为什么我的转义反斜杠和单引号变成了双引号?(顺便说一句,我正在使用 phpmyadmin 管理我的数据库)

Thus my first question of why my escape backslash and single quote have turned into a double quote? (I'm using phpmyadmin to administer my database btw)

这是我用来创建组合框选项的 PHP:

$imgClass_query = "SELECT DISTINCT imgClass FROM primary_images ";

...

while ($imgClass_row = mysql_fetch_array($imgClass_result))
  // Add a new option to the combo-box
  echo "<option value='".$imgClass_row[imgClass]."'>".htmlspecialchars($imgClass_row[imgClass])."</option>\n";

如上所示,我将 imgClass 值作为 html 选项属性 value 以及显示值.这是我随后用来搜索数据库的属性 value.

As you can see above, I place the imgClass value as both the html option attribute value, as well as the displayed value. It is the attribute value that I subsequently use to search the database.

这是生成的 HTML 源代码:

<option value="Robin" s'="">Robin's</option>

很明显,当我使用 value 属性搜索数据库时,它会查找不存在的 Robin.

So obviously when I use the value attribute to search the database, it looks for Robin which does not exist.

我应该如何输入 ENUM 值,以及我应该如何创建组合框以保持原始 ENUM 值的完整性?

How should I be entering the ENUM values, and how should I be creating the combo box to maintain the integrity of the original ENUM value?

另外,我不知道为什么在生成的 HTML 中 s' 后面有 ="",以及为什么显示的值是完美的.有什么想法吗?

Also, I'm not sure why there is the ="" after the s' in the generated HTML, and why the displayed value is perfect. Any thoughts?

推荐答案

这里稍微介绍一下 MySQL 中如何处理特殊字符 MySQL 手册

Here's a bit on how to deal with special characters in MySQL MySQL Manual

我认为这是最简单的方法.

I think this is the easiest way for you to do it.

用""引用的字符串中的'"不需要特殊处理,不需要加倍或转义.同样,用'"引用的字符串中的""不需要特殊处理.

A "'" inside a string quoted with """ needs no special treatment and need not be doubled or escaped. In the same way, """ inside a string quoted with "'" needs no special treatment.

因此,不要使用单引号来分隔该特定字符串,而是使用双引号,然后您无需转义其中的单引号.

So instead of using single quotes for delimiting that specific string, use double quotes and then you don't need to escape the single quote inside of it.

在 HTML 方面,我认为字符串中的单引号导致了问题;您可能使用了 HTML 特殊字符代码(请参阅 HTML 代码).您可以使用此函数在 PHP 中轻松完成此操作:PHP htmlspecialchar

On the HTML side, I think the single quote inside the string is causing the problem; you may have use HTML special character code (see HTML codes). You can do that easily in PHP with this function: PHP htmlspecialchar

这篇关于在 MySQL ENUM 值中使用撇号将填充 HTML 组合框以进行数据库搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆