0

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.

2
  • put db fields and table names in backticks or single quotes Commented Apr 1, 2014 at 12:40
  • 1
    Why is there a space in between $_POST [? Not sure if that's causing the error though. You could do a var_dump($_POST); just to check if your values are getting sent right. Commented Apr 1, 2014 at 12:41

1 Answer 1

1

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;
    }
}  
Sign up to request clarification or add additional context in comments.

8 Comments

okay man, the undefined indexes is removed now, but the form still send empty rows to the database. Any ideas?
Make sure the fields actually exist in $_POST as suggested in another comment. Use var_dump.
Another point your image is an uploaded file, use $_FILES instead.
the var_dump doesn't show anything. after I changed the code as shown (with removed spaces- feeling stupid) it doesn't upload anything, not even the empty rows.
Change method=post to method='post' maybe. Otherwise you can close your php tag after include_once and put html (remove the return). You don't have php in this part.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.