文件上传(php)两个警告

yi0zb3m4  于 2023-01-08  发布在  PHP
关注(0)|答案(1)|浏览(128)

我正在工作的小页面,我通过PHP页面更新MySQL记录,所有的功能都很好地工作,但我不断得到一个未定义的数组警告。以下是警告:

Warning: Undefined array key "file" in C:\xampp\htdocs\crud\edit.php on line 11
Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\crud\edit.php on line 11
Warning: Undefined array key "file" in C:\xampp\htdocs\crud\edit.php on line 22
Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\crud\edit.php on line 22

我看了很多这样的主题,但没有设法修复我自己的一个...以下是edit.php的代码

include 'db.php';

// File upload path
$targetDir = "uploads/";
$fileName = basename($_FILES["file"]["name"]);
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);

if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){
}
    // Allow certain file formats
    $allowTypes = array('jpg','png','jpeg','gif','pdf','doc','xlsx');
    if(in_array($fileType, $allowTypes)){
    }
        // Upload file to server
        if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){
        }

我也有一个pcs的HTML,在〈形式:

<form name="update_user" method="post" action="edit.php" enctype="multipart/form-data" >
<tr>
                <td>Нов сертификат:</td>
                <td><input type="file" name="file" ></td>
</tr>

我觉得我错过了一些很简单的事情。
-----更新-------这里是我的完整代码:在一个文件中我把php和html放在一起

// include database connection file
include_once("config.php");
//тук стартирам за фаил ъплоад
// Include the database configuration file
include 'db.php';

// File upload path
$targetDir = "uploads/";
$fileName = basename($_FILES["file"]["name"]);
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);

if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){
}
    // Allow certain file formats
    $allowTypes = array('jpg','png','jpeg','gif','pdf','doc','xlsx');
    if(in_array($fileType, $allowTypes)){
    }
        // Upload file to server
        if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){
        }
// край на фаил ъплоад

// Check if form is submitted for user update, then redirect to homepage after update
if(isset($_POST['update']))
{
    $id = $_POST['id'];
    $toolnr=$_POST['toolnr'];
    $status=$_POST['status'];
    $toolname=$_POST['toolname'];
    $serial=$_POST['serial'];
    $usedat=$_POST['usedat'];
    $owner=$_POST['owner'];
    $calibrated=$_POST['calibrated'];
    $nextcalibration=$_POST['nextcalibration'];
    $vendors=$_POST['vendors'];
    
    // update user data
    $result = mysqli_query($mysqli, "UPDATE tools SET toolnr='$toolnr',status='$status',toolname='$toolname',serial='$serial',usedat='$usedat',owner='$owner',calibrated='$calibrated',nextcalibration='$nextcalibration', vendors='$vendors', file_name = '$fileName' WHERE id='$id'");
    // Redirect to homepage to display updated user in list
    header("Location: index.php");
}

?>
<?php
// Display selected user data based on id
// Getting id from url
$id = $_GET['id'];
// Fetech user data based on id
$result = mysqli_query($mysqli, "SELECT * FROM tools WHERE id=$id");
while($user_data = mysqli_fetch_array($result))
{
    $toolnr = $user_data['toolnr'];
    $status = $user_data['status'];
    $toolname = $user_data['toolname'];
    $serial = $user_data['serial'];
    $usedat = $user_data['usedat'];
    $owner = $user_data['owner'];
    $calibrated = $user_data['calibrated'];
    $nextcalibration = $user_data['nextcalibration'];
    $vendors = $user_data['vendors'];
    $momenten = $user_data['file_name'];
}

?>
<html>
<head>
    <title>Актуализация</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
    <center> <img src="logo-ottobock.png" alt="OttobockLogo"> </center>
    <hr> <br>
    <center> <img src="notification.png" alt="Warning"> </center>
    <center> <i> <p style="color:red;"> В момента работите с най-високо ниво на достъп, моля бъдете внимателни. <br> Всички направени от Вас промени са необратими. <p> </i> </center>
</head>
<body>
    <br/><br/>
    <center> <form name="update_user" method="post" action="edit.php" enctype="multipart/form-data" >
        <table border="0" class="table table-striped" >
            <tr>
                <td>Номер</td>
                <td><input type="text" name="toolnr" class="form-control" value=<?php echo $toolnr;?>></td>
            </tr>
            <tr>
                <td>Статус</td>
                <td>
                <select name="status" id="status" class="form-control" required>
    <?php
        include "dbConn.php";  
        $records = mysqli_query($db, "SELECT status FROM tools WHERE id=$id UNION SELECT currentstatus FROM statuses");  

        while($data = mysqli_fetch_array($records))
        {
            echo "<option value='". $data['status'] ."'>" .$data['status'] ."</option>";  
        }   
    ?>  </select>
                </td>
            </tr>
            <tr>
                <td>Найменование</td>
                <td><input type="text"  name="toolname" class="form-control" value="<?php echo $toolname;?>"></td>
            </tr> <br>
            <tr>
                <td>Сериен номер</td>
                <td><input type="text" name="serial" class="form-control" value="<?php echo $serial;?>"></td>
            </tr>
            <tr>
                <td>Локация</td>
                <td> 
                <select name="usedat" id="usedat" class="form-control" required>
    <?php
        include "dbConn.php";  
        $records = mysqli_query($db, "SELECT usedat FROM tools WHERE id=$id UNION SELECT locations From whereused");  
        while($data = mysqli_fetch_array($records))
        {
            echo "<option value='". $data['usedat'] ."'>" .$data['usedat'] ."</option>";  
        }   
    ?>  </select>
                </td>
            </tr>
            <tr>
                <td>Отговорник</td>
                <td>
                <select name="owner" id="owner" class="form-control"  required>
    <?php
        include "dbConn.php";  
        $records = mysqli_query($db, "SELECT owner FROM tools WHERE id=$id UNION SELECT responsiblepersons From responsibles");  
        while($data = mysqli_fetch_array($records))
        {
            echo "<option value='". $data['owner'] ."'>" .$data['owner'] ."</option>";  
        }   
    ?>  </select>
                </td>
            </tr>
            <tr>
                <td>Калибриран на:</td>
                <td><input type="date" name="calibrated" class="form-control" value=<?php echo $calibrated;?>></td>
            </tr>
            <tr>
                <td>Следваща</td>
                <td><input type="date" name="nextcalibration" class="form-control"  value=<?php echo $nextcalibration;?>></td>
            </tr>
            <tr>
                <td>Сертификат</td>
                <td><?php echo "<a target = '_blank' href='http://10.171.2.15/crud/uploads/$momenten'> Свали </a>";?></td>
            </tr>
            <tr>
                <td>Нов сертификат:</td>
                <td><input type="file" name="file" ></td>
            </tr> 
            <tr>
                <td>Калибрира се при</td>
                <td>
                <select name="vendors" id="vendors" class="form-control" required>
    <?php
        include "dbConn.php";  
        $records = mysqli_query($db, "SELECT vendors FROM tools WHERE id=$id UNION SELECT vendoren FROM vendors");  
        while($data = mysqli_fetch_array($records))
        {
            echo "<option value='". $data['vendors'] ."'>" .$data['vendors'] ."</option>";  
        }   
    ?>  </select>
                </td>
            </tr>
            <tr>
                <td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>></td>
                <td><input type="submit" name="update"  class="btn btn-success" value="Запис">
                 <a href="index.php" class="btn btn-danger" >Отказ</a>  </td>
            </tr>
        </table>
    </form> </center>
</body>
qoefvg9y

qoefvg9y1#

过账工作代码:最终修复的代码(只有php部分)。问题是负责文件上传的代码部分超出了if statement

<?php

// include database connection file
include_once("config.php");

// Check if form is submitted for user update, then redirect to homepage after update
if(isset($_POST['update']))
{

    $id = $_POST['id'];
    $toolnr=$_POST['toolnr'];
    $status=$_POST['status'];
    $toolname=$_POST['toolname'];
    $serial=$_POST['serial'];
    $usedat=$_POST['usedat'];
    $owner=$_POST['owner'];
    $calibrated=$_POST['calibrated'];
    $nextcalibration=$_POST['nextcalibration'];
    $vendors=$_POST['vendors'];
    
// Include the database configuration file
include 'db.php';
$statusMsg = '';

// File upload path
$targetDir = "uploads/";
$fileName = basename($_FILES["file"]["name"]);
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);

if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){

    // Allow certain file formats
    $allowTypes = array('jpg','png','jpeg','gif','pdf','doc','xlsx');
    if(in_array($fileType, $allowTypes)){
    }
    }
        // Upload file to server
        if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){
        }

    // update user data
    $result = mysqli_query($mysqli, "UPDATE tools SET toolnr='$toolnr',status='$status',toolname='$toolname',serial='$serial',usedat='$usedat',owner='$owner',calibrated='$calibrated',nextcalibration='$nextcalibration', vendors='$vendors', file_name = '$fileName' WHERE id='$id'");
    // Redirect to homepage to display updated user in list
    header("Location: index.php");
}

?>

相关问题