PHP表单提交类别

时间:2013-12-18 11:01:41

标签: php mysql

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

 <?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>';
}

?>

1 个答案:

答案 0 :(得分:1)

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

$category = $POST['category'];

应该是

$category = $_POST['category'];