Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:表单选择框不';t将更改的选择写入数据库_Php_Mysql_Select - Fatal编程技术网

PHP:表单选择框不';t将更改的选择写入数据库

PHP:表单选择框不';t将更改的选择写入数据库,php,mysql,select,Php,Mysql,Select,好的,我有一个表单,它被设置为预加载一个DB记录进行编辑。如果你在url中添加一个?edit=1,它将加载记录#1进行编辑。我有一个像这样的盒子- <select class="field select addr"> <option value="no"<?php if($row['has_amenities'] == "no") {echo ' selected=\"selected\"'; } ?>>No</option> <op

好的,我有一个表单,它被设置为预加载一个DB记录进行编辑。如果你在url中添加一个?edit=1,它将加载记录#1进行编辑。我有一个像这样的盒子-

<select class="field select addr">
  <option value="no"<?php if($row['has_amenities'] == "no") {echo ' selected=\"selected\"'; } ?>>No</option>
  <option value="yes"<?php if($row['has_amenities'] == "yes") {echo     'selected=\"selected\"'; } ?>>Yes</option>
</select>
另外,我知道不先清理就编写$u POST值不是一个好主意,但这是防火墙后面的一个内部表单,等等。我会在它工作后清理它:o)


谢谢

尝试用大括号包装数组变量,如下所示:

'$_POST[paragraph_3]' = '{$_POST[paragraph_3]}'
看起来元素没有名称或id——在代码中是这样吗?如果是这样的话,我相信$u POST[has\u便利设施]不会被设置——在$u POST中没有has\u便利设施的价值。您将得到一个空字符串。

将$\u POST[]的所有实例包装在{}(大括号)中,因此如下所示 当$\u POST位于双引号字符串中时,需要使用大括号强制PHP将其作为变量计算

另外,像下面这样引用您的$\u POST数组键 你想养成这样的习惯,即使$u POST[key]通常会起作用。PHP将key视为一个常量,如果未定义,它将自动转换为字符串“key”,以便获得预期的行为

但是,如果已经作为常量存在(通过)函数,您将得到常量的值,而该值不是您想要的值


请查看该部分。

您需要为此选择标记指定一个名称。我还看到,您正在用反斜杠来转义双引号,这是不必要的(它实际上会使用\”,因此输出看起来像:selected=\“selected\”,这是糟糕的html)

尝试使用以下方法:

<select name="has_amenities" class="field select addr">
  <option value="no"<?php if($row['has_amenities'] == "no") {echo ' selected="selected"'; } ?>>No</option>
  <option value="yes"<?php if($row['has_amenities'] == "yes") {echo     'selected="selected"'; } ?>>Yes</option>
</select>

>对

您的SQL语句将按原样工作,但如果用户输入一个引号,它将破坏该语句……可能会造成一个巨大的安全漏洞。请查看google上的“SQL注入”。

仅供将来参考,在一行的开头放4个空格以进行代码格式化。
'{$_POST['key']}'
$_POST['key']
<select name="has_amenities" class="field select addr">
  <option value="no"<?php if($row['has_amenities'] == "no") {echo ' selected="selected"'; } ?>>No</option>
  <option value="yes"<?php if($row['has_amenities'] == "yes") {echo     'selected="selected"'; } ?>>Yes</option>
</select>