使用 php 创建一个表单,该表单应该获取数据并发送给数据库

问题描述 投票:0回答:1

表格 带有文本和文件输入字段。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form action="legg_til_staff_funksjoner.php" method="post" enctype="multipart/form-data">
        <input type="text" name="name">
        <select name="position" id="">
            <option value="bartender">Bartender</option>
            <option value="bouncer">Bouncer</option>
            <option value="entertainer">Entertainer</option>
            <option value="greeter">Greeter</option>
            <option value="manager">Manager</option>
        </select>
        <input type="text" name="ic_beskrivelse">
        <input type="text" name="ooc_beskrivelse">
        <input type="file" name="link_name">
        <input type="submit">
    </form>
</body>

</html>

表单功能 我收集以表单提交的信息,并且应该将其发送到数据库。

<?php
$target_dir = "staffImages/";
$target_file = $target_dir . basename($_FILES["link_name"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

$name = $_POST['name'];
$position = $_POST['position'];
$ic_beskrivelse = $_POST['ic_beskrivelse'];
$ooc_beskrivelse = $_POST['ooc_beskrivelse'];
$link_name = ($_FILES['link_name']['name']);

include "funksjoner.php";
$db = kobleTilTest();

$sql = "INSERT INTO staff (name, position, ic_beskrivelse, ooc_beskrivelse, link_name) 
VALUES ('$name', '$position','$ic_beskrivelse', '$ooc_beskrivelse', '$link_name'  )";

if (isset($_POST["submit"])) {
    $check = getimagesize($_FILES["link_name"]["tmp_name"]);
    if ($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}

if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}

if ($_FILES["link_name"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}

if (
    $imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
    && $imageFileType != "gif"
) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}

if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
} else {
    if (move_uploaded_file($_FILES["link_name"]["tmp_name"], $target_file)) {
        echo "The file " . htmlspecialchars(basename($_FILES["link_name"]["name"])) . " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
        echo "$target_file";
    }
}

我尝试建立一个表单,接受员工输入的文本和图像,然后将其发布到数据库,但似乎无法使其工作。它有时只上传图像,但文本不存储在数据库中。

php mysqli
1个回答
0
投票

概述

我注意到您的代码中存在一个与处理表单提交方式相关的重大错误。问题是您仅在文件上传部分使用条件检查表单提交,而不是在整个代码块上。

错误

您目前拥有:

...
if (isset($_POST["submit"])) {
  $check = getimagesize($_FILES["link_name"]["tmp_name"]);
  if ($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
}
...

解决方案

您需要将条件移至代码开头,以确保它包含整个表单处理逻辑:

if (isset($_POST["submit"])) {
  $target_dir = "staffImages/";
  $target_file = $target_dir . basename($_FILES["link_name"]["name"]);
  $uploadOk = 1;
  $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

  $name = $_POST['name'];
  $position = $_POST['position'];
  $ic_beskrivelse = $_POST['ic_beskrivelse'];
  $ooc_beskrivelse = $_POST['ooc_beskrivelse'];
  $link_name = ($_FILES['link_name']['name']);
  ...
© www.soinside.com 2019 - 2024. All rights reserved.