2013-12-18 92 views
-1

我想创建一个联系脚本,一旦有人填写了信息,它会将其发送到我的数据库。反正它一切正常,除了选择类别。这些类别的功能是检查数据库中的类别名称,然后当有人与我联系时,它会在列表中显示它们。这个工作,虽然当提交一切进入我的数据库,然后是类别。PHP表单提交分类

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

?> 
+0

还有什么恰好进入数据库? – nowhere

+0

@CosLu就是现在。标题和类别。只有数据库中出现的是ID和标题。该分类不会显示。 – user3114922

+4

@ user3114922这不是Stack Overflow的工作原理。如果你已经解决了问题,那么答案最能帮助你“接受”(点击答案左侧的空白勾号,这样你可以帮助未来的访问者) – ChrisF

回答

1

变化$POST['category'];$_POST['category'];

$category = $POST['category']; 

应该

$category = $_POST['category'];