I've dabbled with PHP for a couple of years, I can generally get what I need done and I've been teaching myself PDO. The problem is I want to set it up with classes but i'm not really sure how to.
Below is my code, which takes data from a form, does some rough checking to see if the email is right and the fields are full. It works but it's gotten to a point where I can't take it further without breaking it or getting confused and i want to add things such as validation.
So what i'm asking for is can some show me how to create a basic connection class that outputs acouple of records from a database, this would help me a lot if anyone could.
<?php
try{
$host = "localhost";
$dbname = "db";
$user = "user";
$pass = "pass";
$odb = new PDO("mysql:host=" . $host . ";dbname=" . $dbname, $user, $pass);
$odb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "I'm afraid I can't do that.";
file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}
if($_SERVER["REQUEST_METHOD"] == "POST"){
if (isset($_POST['firstname']) && !empty($_POST['lastname']) && filter_var($_POST['email'],FILTER_VALIDATE_EMAIL) && !empty($_POST['phone'])){
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$q = "INSERT INTO jobform(firstname, lastname, email, phone) VALUES (:firstname, :lastname, :email, :phone);";
$query = $odb->prepare($q);
$results = $query->execute(array(
":firstname" => $firstname,
":lastname" => $lastname,
":email" => $email,
":phone" => $phone,
));
} else {
#echo $errMsg;
}
}
?>