0

这是我的代码:

mysql_query("INSERT INTO jokes (headline, text, type) VALUES ('{$_POST['headline']}','{$_POST['text']}', '{$_POST['type']}')") or die(mysql_error());  

我想发布一个选择选项,如

<select name="type">  
<option value="regular">regular joke</option>  
<option value="other">other</option>
</select>

这是我的理论,但它不起作用(它没有出现在数据库中,我有一个常规和其他的枚举字段)。有什么帮助吗?

编辑:没关系,我将枚举更改为 varchar,现在它可以工作了:)

4

2 回答 2

0

像这样更改您的数据库,然后执行您的插入查询,我希望它对您有用

alter table `database_name`.`jokes` change `type` `type` enum('regular','other') NULL ;
于 2012-06-23T15:45:52.873 回答
0

此行肯定会引发错误,因为您有单引号(用于 POST 数组名称)嵌套在更多单引号中。要么逃避那些,要么像这样写:

mysql_query("INSERT INTO jokes (headline, text, type) VALUES ('" . $_POST['headline']. "','" . $_POST['text'] . "', '" . $_POST['type'] . "')") or die(mysql_error());

另外,为了避免很多麻烦和未知错误,您永远不应该使用保留字作为变量,例如:

text
type

当我很久以前刚开始使用 PHP 时,我有几个小时甚至几天的头痛,因为我有一个名为:USER 的 mySql 字段(一个保留的 mySql 字,但这可以通过将它包含在 ` 后引号符号中来解决)

于 2016-05-16T15:51:12.870 回答