php - 如何从 INSERT 中的两个输入获取值?

标签 php mysql sql-insert

如何获取 'description'INSERT 中包含的 select name= 'type' 的值???

Array ( 
  [0] => 23000 
  [1] => 1452 
  [2] => Cannot add or update a child row: a foreign key constraint fails (`dblabb`.`items`, CONSTRAINT `fk_items_itemtypes_id` FOREIGN KEY (`type`) REFERENCES `itemtypes` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) 
)
  if(isset($_POST['addItem'])){
  $query = "INSERT INTO items(type,description)  VALUES (':type:id', ':desc')";
  $name = filter_input(INPUT_POST,'desc',FILTER_SANITIZE_SPECIAL_CHARS);

  $sth = $db->prepare($query);
     if($sth->execute(array(':desc' =>$name)``)){

        echo "<h4>Item added</h4>";

      }else{
        echo "<h4>Error</h4>";
        echo "<pre>" . print_r($sth->errorInfo()) . "</pre>";
   }

   <form action="dblabb3.php" method="post">
   <select name="type">
        <?php

        $query = "SELECT * FROM itemtypes ORDER BY name ASC";
        if ($result = $db->query($query)) {
              while ($row = $result->fetch(PDO::FETCH_NUM)) {
                    echo '<option value="' . $row['0'] . '">' .
                            $row['1'] . '</option>';
              }
        } ?>
    </select>
    <input type="text" name="desc" placeholder="description">
    <input type="submit" name="addItem" value="Submit">
    </form>

最佳答案

您的执行语句在数组 [desc] 中只有一个元素,但类型也是必需的。

应该看起来像:

if($sth->execute(array(':type:id'=>$TYPE,':desc' =>$name){}

关于php - 如何从 INSERT 中的两个输入获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23581801/

相关文章:

php - $_Session[variable] 放在代码中的什么位置

mysql - 如何有效地计算用户在一个月内的操作次数?

sql - 复制父、子和孙记录

PHP:类或全局的数据库连接

php - 从其文件夹位置而不是调用者文件夹位置执行 shell 脚本?

javascript - AngularJS组织简单的Ajax程序?

mysql - innodb锁等待超时

php - ldap_set_rebind_proc() 函数在任何 PHP Windows 发行版中都不起作用

mysql - SQL if存在更​​新数量

SQL Server : INSERT from one table to another table