【问题标题】:How can I insert more than two option tag value in separated select tag to database如何在单独的选择标签中插入两个以上的选项标签值到数据库
【发布时间】:2016-09-07 14:15:16
【问题描述】:

index.php

<!DOCTYPE html>
</html>
<body>

<form name="fruits" action="selectexec.php" method="post">
<select name="department">
<option value="apple">apple</option>
<option value="mango">mango</option>
<option value="orange">orange</option>
</select>

<select name="company">
<option value="ASUS">ASUS</option>
<option value="LENOVO">LENOVO</option>
<option value="ACER">ACER</option>
</select>

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

</body>
</html>

selectexe.php

<?php
include_once('pdo-connect.php');
if(isset($_POST['submit'])){
$department=$_POST['department'];
$company=$_POST['company'];


// SQL statements
$sql = "INSERT INTO selectformtbl (department,company)values('$department',$company)";
$db->exec($sql);
}

?>

【问题讨论】:

  • 请编辑您的问题并正确格式化。正如您在预览中看到的那样,它看起来并不好。发送前务必正确格式化代码。
  • 你想在selectformtbl上将其存储为2行还是一行中的单个列
  • @Manila,来自相同选择标签或不同选择标签的 2 个选项值?这对于正确回答您的问题至关重要
  • 感谢您的回复。编辑:不同的选择标签,只保存 1 行到数据库表。
  • 但是您是否希望从每个下拉列表中选择多个选项?

标签: php pdo


【解决方案1】:

&lt;select&gt; 标签应该有multiple 属性。

<select name="department[]" multiple></select>
<select name="company[]" multiple></select>

在 PHP 中,您可以将所有这些值内爆并直接存储到数据库中。

$departments = implode($_POST['department']);
$company = implode($_POST['company']);
$sql = "INSERT INTO selectformtbl (`department`,`company`) values ('$department','$company')";
$db->exec($sql);

【讨论】:

  • 虽然这样做确实会让您对SQL Injection Attack 敞开大门,看看Little Bobby Tables 发生了什么,甚至if you are escaping inputs, its not safe! 使用prepared parameterized statements
  • 是的,如果我们很容易在双引号中使用变量,那很好,但我已经发布了这种方式,因为它易于阅读和检查。
  • 现在,完美!并且只是为了防止 SQL 注入攻击而进行了少量编辑。
  • 当我删除了代码中的内爆和多个属性以给我的页面提供更多空间时,它就像一个魅力。我找到了其他方法来保存数据。
  • 您没有阅读或不了解Little Bobby Tables 这仍然对 SQL 注入开放
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-21
  • 1970-01-01
  • 1970-01-01
  • 2020-11-22
  • 2018-04-11
  • 1970-01-01
  • 2021-05-25
相关资源
最近更新 更多