在内容页面上呈现类别

时间:2016-09-15 19:30:47

标签: php html mysql

我有表格内容和类别(已经有2个类别),内容' cat_id' row具有类别' id'的外键。行。

我需要在创建内容时在选择字段中显示类别,并将我选择的一个类别保存到数据库中。

创建内容表单:

<section class="add_content_form">
                <form action="create_content.php" method="POST" id="content_form" enctype="multipart/form-data" >
                    <div class="content_text">
                    <p><label for="title">Title:</label></p>
                    <input type="text" name="title" value=""></div>

                    <div class="content_textarea">
                    <p><label for="text">Text</label></p>
                    <textarea name="text" id="text" cols="30" rows="20" form="content_form">    
                    </textarea></div>
                    <p><label for="text">Add image</label></p>
                    <div class='add_image'><input type="file" name="content_pic"></div>
                    <div class="category">
                    <p><select size="3" multiple name="hero[]">
                        <option disabled>choose category</option>
                        <option value="1">1</option>    
                        <option value="2">2</option>
                        <option value="3">3</option>
                    </select>
                    </p>
                    </div>
                    <div class="submit_content"><input type="submit" value="Send"></div>
                </form>
            </section>

创建内容的功能:

function create_content($title, $text, $content_pic) {

  $title = trim($title);
  $text = trim($text);

  if (!empty($content_pic)) {
    if ($content_pic ["error"] == UPLOAD_ERR_OK) {
      $uploads_dir = __DIR__ . '/files';
          $tmp_name = $content_pic ["tmp_name"];
          $name = $content_pic ["name"];
          if (move_uploaded_file($tmp_name, "{$uploads_dir}/{$name}")) {
            $content_pic = "files/{$name}";
          }
    }
  } 

  $db = database_connect();     

  $stmt = $db->prepare("INSERT INTO content (`title`, `text`,`content_pic`, `author`, `time`) VALUES (?, ?, ?, ?, ?)"); 
  $stmt -> execute(array($title, $text, $content_pic, $_SESSION['user_id'], time()));  

  header("Location: review.php?id=" . $insertId=$db->lastInsertId());
  exit();

}

如何从数据库中获取要显示的类别?:

<div class="category">
     <p><select size="3" multiple name="hero[]">
         <option disabled>choose category</option>
         <option value="1">1</option>
         <option value="2">2</option>
         <option value="3">3</option>
     </select>

0 个答案:

没有答案