在Wordpress中,如何使用文件上载选项编写自定义表单并将记录保存在MySql中?

So far I have been able to make a custom form which upon successful entry save the value to a MySql table called "form_entry"

Below is the code for a form:

<form method="post" class="container">
<div class="row">
<div class="col-md-6 mb-3">
<label>Name</label>
<input type="text" name="myname" class="form-control" placeholder="Name" value="John Doe" required>
</div>
<div class="col-md-3 mb-3">
<label>State</label>
<input type="text" name="statename" class="form-control" placeholder="State" required>
</div>
</div>
<button class="btn btn-primary" type="submit" name="BtnSubmit" value="submit">Submit form</button>
</form>

And I insert a few codes in the functions.php under my template folder like this :

if(isset($_POST['BtnSubmit'])) {
    global $wpdb;
    $data_array = array(
                        'myname' => $_POST['myname'],
                        'statename'  => $_POST['statename'],

                        );

    $table_name = 'form_entry';
    $rowResult = $wpdb ->insert($table_name, $data_array, $format=NULL);

    if($rowResult == 1) {
        echo 'Success';
    }
else {
    echo ' Error in my pant';
}   
die;
}

This is working fine. But I want to add a file upload option in the form and save the uploaded file location to MySql. Please help.

You have to add one field into your html form. and add parameter for into form.

<form method="post" class="container" enctype="multipart/form-data">

<div class="row">
    <div class="col-md-6 mb-3">
        <label>File</label>
        <input type="file" name="myfile" class="form-control" required/>
    </div>
</div>

then after submit you have to save uploaded file at particular location and save that file into your db.

if (isset($_POST['BtnSubmit'])) {
    $target = 'uploads/' . basename($_FILES['myfile']['name']);

    if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target)) {
        $fp = fopen($target, "r");
    }
    global $wpdb;
    $data_array = array(
                    'myname' => $_POST['myname'],
                    'statename'  => $_POST['statename'],
                    'filename' => $target
                 );
}

I think it will work for you !! feel free to ask me.