使用PHP将textarea插入所选选项

时间:2017-12-07 04:52:43

标签: php html mysql

在名为api的数据库中,我有三列idnamearticle。我用一个看起来像这样的形式制作了一个模态:

<form method="post">
   <div class="modal-header">
      <h5 class="modal-title">Add</h5>
   </div>
      <div class="modal-body">
         <div class="form-group row">
            <label class="col-sm-4 col-form-label">Choose</label>
            <div class="col-sm-8">
               <select class="form-control" name="select_c">
                  <?php
                     while ($row = $result->fetch_assoc()) {
                        unset($id, $name);
                        $id = $row['id'];
                        $name = $row['name'];
                        echo '<option value="'.$id.'">'.$name.'</option>';
                     }
                  ?>
               </select>
            </div>
         </div>
         <div class="form-group row">
            <label class="col-sm-4 col-form-label">Article</label>
            <div class="col-sm-8">
               <textarea class="form-control" name="article_c" rows="4"></textarea>
            </div>
         </div>
      </div>
   <div class="modal-footer">
      <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      <button type="submit" class="btn btn-primary">Save changes</button>
   </div>
</form>

这是我的add.php,它在我上面复制的代码中包含在前面。

   $servername = "localhost";
   $username = "root";
   $password = "";
   $dbname = "trying";

   $conn = mysqli_connect($servername, $username, $password, $dbname);

   if (!$conn) {
      die("Could not establish connection." . mysqli_connect_error());
   }

   if ($_SERVER['REQUEST_METHOD'] == 'POST') {

      $article = mysqli_real_escape_string($conn, $_REQUEST['article']);

      $sql = "INSERT INTO api (article) VALUES ('$article')";
      if(mysqli_query($conn, $sql)){
         #echo "Records added successfully.";
      } else{
         echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
      }
   }

   $sql = "SELECT * FROM api";
   $result = $conn->query($sql);

   mysqli_close($conn);

我想要完成的是当我选择<option value="'.$id.'">'.$name.'</option>时,我在<textarea class="form-control" name="article_c" rows="4"></textarea>中输入的文字将插入到我点击时选择的该行的article列中提交。

我该怎么做?

0 个答案:

没有答案