1

I want to upload an image through form and save url in database, in 'pic' column. It has been showing errors though it is saving the image name and the image is saving any where in my DB. Your kind answer is highly appreciated. Here is the code:

<?php /* Template Name: Dark Knight*/?>
<div id="main-content" class="main-content">
  <div class="main-content-inner">
        <form method="post" enctype='multipart/form-data'>
        <p>
            <div>
                <input name="nametxt" id="nametxt" type="text"><br>
            </div>
        </p>

        <p>
            <div>
                <input name="fileToUpload" id="fileToUpload" type="file">
            </div>
        </p>
            <input id="submitbtn" type='submit' name='Submit' value='Add Member' /><br><br>
        </form>
    </div>
 </div>
<?php 
    if(isset($_POST['Submit']))
    {
        if( isset($_POST['nametxt']) || isset($_FILES['fileToUpload']))
        {
            global $wpdb;
            $target_dir = "uploads/";
            $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]. $_POST['fileToUpload']);
            $data = array('name' => $_POST['nametxt'], 
                          'pic' => $target_file);
            $table = $wpdb->prefix.'tboxteam';
            $format = array('%s','%s');
            $wpdb->insert($table,$data,$format);
        }
    }
?>

Notice: Undefined index: fileToUpload in C:\xampp\htdocs\wordpress\wp-content\themes\twentyfifteen-child\custompage7.php on line 39

STBox
  • 442
  • 2
  • 12

2 Answers2

1

You need to use

enctype='multipart/form-data'

in form when dealing with file upload

0

Try, to work out this w3schools tutorial step by step. Take your time and don't rush things. Take a brake, if you feel overwhelmed and try again later. I did it in 30 minutes, when i cleared my brain.

era-net
  • 332
  • 1
  • 8