Inserting an array in MySQL

I have one form with two HTML input fields where the tag input is an autocomplete form (from the database):

tags

  • TagID
  • tegu

and I want to insert an array into two tables, document

and documenttag

:

Table 1: document

  • docId

  • docName

Table 2: documenttag

  • docId

  • tagId

But I was unable to insert data into the database. Below is my code:

document-form.php

    <?php

      $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

      $stmt = $conn->prepare("SELECT tag_name FROM tags");
      $stmt->execute();

      $json = array();  
      while($row = $stmt->fetch()){
        $json[] = $row['tag_name'];
      }
      $json_array = json_encode($json);

      ?>
      <script>
        $(function(){
          var sampleTags = <?php echo $json_array; ?>

                $('#tags_1').tagit({
                  availableTags: sampleTags,
                });            
              });
            </script>

    <input type="file" id="file" name="file" required="required">
    <input id="tags_1" name="tags_1[]" type="text" class="tags form-control">

      

document-downloads-action.php

<?php
    $name = $_FILES['file']['name'];
            $type = $_FILES['file']['type'];
            $size = $_FILES['file']['size'];
            $filesize = round($size / 1024, 2);

            $insert_doc = "INSERT INTO document(docId,docName) VALUES ('$docid','$name')";

            $stmt = $conn->prepare($insert_doc);
            $stmt = bindParam(':privacy', $privacy, PDO::PARAM_STR);
            $privacy = $_POST['privacy'];
            $stmt->execute();

            $tags_1 = implode(",", $tags_1);
            $insert_doc_detail = "INSERT INTO documenttag(docId, tagId) VALUES('$docid','$tags_1')";
            $stmt_1->$conn->prepare($insert_doc_detail);
            $stmt_1->bindParam(':tags_1', $tags_1, PDO::PARAM_STR);
            $tags_1[] = $_POST['tags_1'];
            $stmt_1->execute();
?>

      

+3


source to share





All Articles