dsxon40042 2019-05-07 11:01
浏览 24

如何创建帖子然后将数据条目上传到单独的表,其中包含一次包含先前创建的帖子的ID的数据条目?

The main idea is to create a post along with multiple pictures that associate with the post but the entries for these posts are on a separate table.

I tried making it into one table to do both stuff, but I couldn't find a way to include more than one location for a file in on column.

HTML

<form action="includes/post.inc.php" method="POST" class="col s12" enctype="multipart/form-data">
  <div class="row">
    <div class="input-field col s12">
      <input id="title" name="title" type="text" class="validate" required>
      <label for="title">Title</label>
    </div>
  </div>
  <div class="row">
    <div class="input-field col s12">
      <textarea id="body" name="body" class="materialize-textarea" required></textarea>
      <label for="body">Body</label>
    </div>
  </div>
  <div class="row">
    <div class="input-field col s12">
      <select name="category" required>
        <option value="" disabled selected>Choose a Category</option>
        <option value="programming">Programming</option>
      </select>
      <label>Categories</label>
    </div>
  </div>
  <div class="row">
    <div class="file-field input-field">
      <div class="btn">
        <span>File</span>
        <input type="file" name="file[]" multiple>
      </div>
      <div class="file-path-wrapper">
        <input class="file-path validate" type="text" placeholder="Upload one or more files">
      </div>
    </div>
  </div>
  <button class="btn waves-effect waves-light right" type="submit" name="post-submit">Post</button>
</form>

PHP

function reArrayFiles($file_post){
  $file_ary = array();
  $file_count = count($file_post['name']);
  $file_keys = array_keys($file_post);

  for($i = 0; $i<$file_count; $i++){
    foreach($file_keys as $key){
      $file_ary[$i][$key] = $file_post[$key][$i];
    }
  }

  return $file_ary;
}

global $idPost;
if (isset($_POST['post-submit'])) {

  session_start();
  $category = $_POST['category'];
  $title = $_POST['title'];
  $body = $_POST['body'];
  $category = $_POST['category'];
  $id = $_SESSION['userId'];
  $uid = $_SESSION['userUid'];
  $status;
  if(empty($title) || empty($body)){
    header("Location: ../index.php?error=emptyfields&title=".$title."&mail=".$body);
    exit();
}else{
    $sql = "INSERT INTO posts (titlePosts, contentPosts, catPosts, timePosts, statusPosts, uidUsers, idUsers) VALUES (?, ?, ?, NOW(), ?, ?, ?)";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)) {
        header("Location: ../index.php?error=sqlerror");
        exit();
    }else{
        mysqli_stmt_bind_param($stmt, "ssssss", $title, $body, $category, $status, $uid, $id);
        mysqli_stmt_execute($stmt);
    }
  }


  $file_array = reArrayFiles($_FILES['file']);
  $result = mysqli_query($conn, "SELECT idPosts FROM posts WHERE titlePosts={$title} AND bodyPosts={$body} AND idUsers={$id}");
  while($row = mysqli_fetch_assoc($result)){
    $idPost = $row['idPosts'];
  }
  for($i = 0; $i<count($file_array); $i++){
    $fileName = $file_array[$i]['name'];
    $fileTempName = $file_array[$i]['tmp_name'];
    $fileSize = $file_array[$i]['size'];
    $fileError = $file_array[$i]['error'];
    $fileType = $file_array[$i]['type'];

    $fileExt = explode('.',$fileName);
    $fileActualExt = strtolower(end($fileExt));
    $allow = array('jpg','jpeg','png','JPG','JPEG','PNG');

        if(in_array($fileActualExt, $allow)){
          if($fileError === 0){
            if($fileSize < 100000){
              $fileNameNew = uniqid('', true).".".$fileActualExt;
              $fileDestination = '../uploads/'.$fileNameNew;
              move_uploaded_file($fileTempName,$fileDestination);
              mysqli_query($conn, "INSERT INTO post_files (locationFiles, idPosts) VALUES ({$fileNameNew},{$idPost})");
            }else{
              echo "<script>alert('Your file is too big!');</script>";
            }
          }else{
            echo "<script>alert('There was an error uploading your file!');</script>";
          }
        }else{
          echo "<script>alert('You cannot upload a file of this type!');</script>";
        }
  }
  if(!isset($category)){
    header("Location: ../index.php?post=success");
    exit();
  }else{
    header("Location: ../index.php?cat={$category}&post=success");
    exit();
  }
}else{
    header("Location: ../index.php");
    exit();
}

post_files Table

|---------------------|------------------|------------------|
|      idFiles        |   locationFiles  |     idPosts      |
|---------------------|------------------|------------------|
|          int        |   varchar(255)   |       int        |
|---------------------|------------------|------------------|

idPosts is linked with the posts table

I expected the program to create a post along with the data from the fields, and for the files to be uploaded to htdocs and the file to be inserted into the data base. However, everything goes well except for entering data into the post_files Table.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?
    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
    • ¥15 Arduino无法同时连接多个hx711模块,如何解决?