带有类别的 PHP 表单提交


PHP form submit with categories

我正在尝试创建一个联系人脚本,一旦有人填写了信息,它就会将其发送到我的数据库。无论如何,除了选择类别之外,一切都很好。类别的作用是在数据库中检查类别名称,然后在有人与我联系时将它们显示在列表中。这有效,尽管在提交时所有内容都会进入我的数据库,而不是类别。

 <?php
  if($_POST[add]){
    $title = strip_tags($_POST['title']);
    $category = $POST['category'];
    if(!$title){
        echo 'All fields are required!';
    }else{
    $sql = $dbh->prepare("INSERT INTO `posts` (`title`, `category`) VALUES ('$title', '$category')");
    $sql->execute();
    $q = $sql->fetch(PDO::FETCH_ASSOC);
        if($sql){
            echo 'Published successfully';
        }else{
            echo '<strong>Error:</strong> '.mysql_error();
        }
    }
}else{
    echo '
    <form method="post">
            <label for="title">Title:</label>
            <input type="text" name="title" id="title" value="" size="50" maxlength="40">
        <br>
        <br>
        <label for="category">Category:</label><br>
        <select name="category" style="width:200px;">';

        $sql = "SELECT * FROM posts_categories ORDER BY `id` ASC";
        $stm = $dbh->prepare($sql);
        $stm->execute();
        $users = $stm->fetchAll();
        foreach ($users as $row) {
        echo '<option value="'. $row["title"] .'">'. $row["title"] .'</option>';
        }
        echo'
        </select>
            <input type="submit" name="add" value="Add"> <input type="reset" value="Reset">
    </form>';
}
?>

$POST['category'];更改为$_POST['category'];

$category = $POST['category'];

应该是

$category = $_POST['category'];