z0mbieKale
z0mbieKale

Reputation: 1028

Undefined index when inserting into database

I have a function where people can insert two textfields into the database. It inserts empty fields into the database at the moments at gives me these errors right now:

Notice: Undefined variable: catchphrase in /Applications/XAMPP/xamppfiles/htdocs/modul8B/controllers/home.php on line 17

Notice: Undefined variable: interest in /Applications/XAMPP/xamppfiles/htdocs/modul8B/controllers/home.php on line 17

Notice: Undefined variable: image in /Applications/XAMPP/xamppfiles/htdocs/modul8B/controllers/home.php on line 17

Here's the form:

<?php
include_once 'models/profile_table_class.php';

return"
<div class='main-wrap'>
    <div class='container'>



      <div class='header'>




            <form id='profile-form' method=post action='index.php?page=home' enctype='multipart/form-data'>
                <h1> Your Profile </h1> 
                   <textarea name='catchphrase' id='catchphrase'>

                    </textarea>

                    <textarea name='interest' id='about-you' >

                    </textarea>

                    <h3 class='upload-heading'>Upload Image:</h3>
                    <input type='file' name='image' id='file'>

                    <input id='profile-submit' type='submit' value='profile-submit'/>
             </form>

       </div>
    </div>            
</div>
";

Here's the script for inserting the data:

class Profile_Table {
    private $db; 

    public function __construct($pdo) {
        $this->db = $pdo; 
        ;
    }
    public function insertProfile( $catchphrase, $interest, $image){
        $sql = "INSERT INTO profile (catchphrase, interest, image) Values ('".$catchphrase."', '".$interest."', '".$image."')";
        $statement = $this->db->prepare($sql);
        $data = array ($catchphrase, $interest, $image);  
        $statement->execute ($data);
        return $statement;
    }

And here's the code where the error is coming from:

include_once "models/profile_table_class.php";
    $profile = new Profile_Table($db);


$profileIsSubmitted = isset($_POST['profile-submit']);
if ($profileIsSubmitted) {
    $catchphrase = $_POST ['catchphrase'];
    $interest = $_POST ['interest'];
    $image = $_POST ['image'];

}  try {
        $profile->insertProfile($catchphrase, $interest, $image);
    } catch (Exception $e) {
        $errorDescription = $e;
        echo $e;
    }

Any help appreciated.

Upvotes: 0

Views: 1977

Answers (1)

Debflav
Debflav

Reputation: 1151

Put the try catch in the if condition. If the request is get you will have errors.

if ($profileIsSubmitted) {
    $catchphrase = $_POST ['catchphrase']; // and remove space
    $interest = $_POST ['interest'];// and remove space
    $image = $_POST ['image'];// and remove space

    try {
        $profile->insertProfile($catchphrase, $interest, $image);
    } catch (Exception $e) {
        $errorDescription = $e;
        echo $e;
    }
}  

Upvotes: 1

Related Questions