Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP CRUD Operations with PHP Creating Records Adding Projects

Error!: SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: projects.category

Had this error on this code.

function add_project($title, $category){ include 'connection.php';

$sql = 'INSERT INTO projects(title, category) VALUES(?, ?)';

try{
    $result = $db->prepare($sql);
    $result->bindValue(1, $title, PDO::PARAM_STR);
    $result->bindValue(1, $category, PDO::PARAM_STR);
    $result->execute();
}catch (Exception $e){
    echo "Error!: " . $e->getMessage() . "<br />";
    return false;
}
return true;

}

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Leslie Toh,

You have a simple oversight which is you're trying to assign both your variables to the first placeholder, simply change it to the below so you your variables to indexes 1 & 2.

$result->bindValue(1, $title, PDO::PARAM_STR);
$result->bindValue(2, $category, PDO::PARAM_STR);

Happy coding!

Thanks Chris

Renzo Salvador
Renzo Salvador
523 Points

function add_project($title, $category){ include 'connection.php';

$sql = 'INSERT INTO projects(title, category) VALUES(?, ?)';

try{
    $results = $db->prepare($sql);
    $results->bindValue->(1, $title, PDO::PARAM_STR);
    $results->bindValue->(2, $category, PDO::PARAM_STR);
    $results->execute();
    } catch (Exception $e){
        echo "Error!: " . $e->getMessage() . "<br />";
        return false;
    }
return true;

}

?>

Can anyone spot an error? I get Error 500