在数据库中保存文本和图像时发生未定义的变量错误。如何解决?

时间:2018-07-23 07:06:51

标签: php mysql

我是php新手。所以有人,这个问题可能非常幼稚,但我仍然无法解决。我想将图像路径保存在数据库中,并将用户的其他文本输入保存在同一张表中。但这会出现此错误**注意:未定义的索引:年龄在**。请您帮帮我吗?

<?php
  // Create database connection
  $db = mysqli_connect("localhost", "root", "", "construction");

  // Initialize message variable
  $msg = "";

  // If upload button is clicked ...
  if (isset($_POST['upload'])) {
    // Get image name
    $image = $_FILES['image']['name'];
    // Get text
    $image_text = mysqli_real_escape_string($db, $_POST['image_text']);
    $age = mysqli_real_escape_string($db, $_POST['age']);


    // image file directory
    $target = "uploads/".basename($image);

    $sql = "INSERT INTO images (image_path, description,age) VALUES ('$image', '$image_text','$age')";
    // execute query
    mysqli_query($db, $sql);

    if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
      $msg = "Image uploaded successfully";
    }else{
      $msg = "Failed to upload image";
    }
  }

?>
<!DOCTYPE html>
<html>
<head>

</head>
<body>

  <form method="POST" action="imageUpload.php" enctype="multipart/form-data">
    <input type="hidden" name="size" value="1000000">
    <div>
      <input type="file" name="image">
    </div>
    <div>
      <textarea id="text" cols="40" rows="4" name="image_text" placeholder="Say something about this image..."></textarea>
      <input type="text" name="age">
    </div>
    <div>
      <button type="submit" name="upload">POST</button>
    </div>
  </form>
</div>
</body>
</html>

0 个答案:

没有答案