使用PHP以包含多个文本字段的形式上载文件

时间:2017-02-16 09:08:54

标签: php upload

我正在试图弄清楚如何将文件上传到数据库,其中该表单包含多个文本字段。我将BLOB字段上传到数据库中。因此,当我尝试使用ID号搜索字段时,它将检索与之关联的值。哪个工作正常,所以我添加了能够将文件上传到该特定ID号的功能。我得到各种各样的错误,我想得到它的帮助。有人在乎帮忙吗?以下是代码:

<?php

$host = "localhost";
$user = "root";
$password ="";
$database = "ntmadb";

$id = "";
$firstname = "";
$lastname = "";
$username = "";

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// connect to mysql database
try{
    $connect = mysqli_connect($host, $user, $password, $database);
} catch (mysqli_sql_exception $ex) {
    echo 'Error';
}

// get values from the form
function getPosts()
{
    $posts = array();
    $posts[0] = $_POST['id'];
    $posts[1] = $_POST['firstname'];
    $posts[2] = $_POST['lastname'];
    $posts[3] = $_POST['username'];
    return $posts;
}

// Search

if(isset($_POST['search']))
{
    $data = getPosts();

    $search_Query = "SELECT * FROM members WHERE id = $data[0]";

    $search_Result = mysqli_query($connect, $search_Query);

    if($search_Result)
    {
        if(mysqli_num_rows($search_Result))
        {
            while($row = mysqli_fetch_array($search_Result))
            {
                $id = $row['id'];
                $firstname = $row['firstname'];
                $lastname = $row['lastname'];
                $username = $row['username'];
            }
        }else{
            echo 'No Data For This Id';
        }
    }else{
        echo 'Result Error';
    }
}


// Edit
if(isset($_POST['update']))
{
    $data = getPosts();
    $update_Query = "UPDATE `members` SET `firstname`='$data[1]',`lastname`='$data[2]',`username`='$data[3]' WHERE `id` = $data[0]";
    try{
        $update_Result = mysqli_query($connect, $update_Query);

        if($update_Result)
        {
            if(mysqli_affected_rows($connect) > 0)
            {
                echo 'Data Updated';
            }else{
                echo 'Data Not Updated';
            }
        }
    } catch (Exception $ex) {
        echo 'Error Update '.$ex->getMessage();
    }
}

<!--UPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOAD -->
// Check if a file has been uploaded
if(isset($_FILES['uploaded_file'])) {
    // Make sure the file was sent without errors
    if($_FILES['uploaded_file']['error'] == 0) {
        // Connect to the database
        $dbLink = new mysqli('localhost', 'root', '', 'ntmadb');
        if(mysqli_connect_errno()) {
            die("MySQL connection failed: ". mysqli_connect_error());
        }

        // Gather all required data


        $data = $dbLink->real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));


        // Create the SQL query
        $query = "
            INSERT INTO `members` (
                 `data`
            )
            VALUES (
          '{$data}' NOW()
            )";

        // Execute the query
        $result = $dbLink->query($query);   

        // Check if it was successfull
        if($result) {
            echo 'Success! Your file was successfully added!';
        }
        else {
            echo 'Error! Failed to insert the file'
               . "<pre>{$dbLink->error}</pre>";
        }
    }
    else {
        echo 'An error accured while the file was being uploaded. '
           . 'Error code: '. intval($_FILES['uploaded_file']['error']);
    }

    // Close the mysql connection
    $dbLink->close();
}
else {
    echo 'Error! A file was not sent!';
}
?>

这是html文件:

<!DOCTYPE Html>
<html>
    <head>
        <title>PHP INSERT UPDATE DELETE SEARCH</title>
    </head>
    <body>
        <form action="index4.php" method="post" enctype="multipart/form-data" >
            <input type="number" name="id" placeholder="Id" value="<?php echo $id;?>"><br><br>
            <input type="text" name="firstname" placeholder="First Name" value="<?php echo $firstname;?>"><br><br>
            <input type="text" name="lastname" placeholder="Last Name" value="<?php echo $lastname;?>"><br><br>
            <input type="text" name="username" placeholder="User Name" value="<?php echo $username;?>"><br><br>
            <div>
             
                <p>
                  <!-- Input For Edit Values -->
                  <input type="submit" name="update" value="Update">
                  
                  
                  
                  <!-- Input For Find Values With The given ID -->
                  <input type="submit" name="search" value="Find">
                </p>
                <p>
                  <input type="file" name="uploaded_file">
                  <br>
                  <input type="submit" value="Upload file">
                </p>
          </div>
        </form>
    </body>
</html>

感谢任何可以为我提供帮助的人。 :)

0 个答案:

没有答案