Uncaught exception 'PDOException'

now i’m starting to get the picture…a little bit: especially after seeing Mittineague’s .gif. that along with spaceshiptrooper’s explanation made it more clear. thanks. i ran across a code that defined the validation issue with regard to list/menu and other input types. in fact it was a post on a sitepoint forum. here is the url

now that i have a grasp of what’s involved i copied the code and put it in a practice file to work through it in my own code. please take a look:

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=new_order", $username, $password);
	
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/*** echo a message saying we have connected ***/
echo 'Connected to database<br />';

if ($_SERVER["REQUEST_METHOD"] == "POST")

{
$errors = [];
}

if(empty($errors)) {

// prepare sql and bind parameters
    $query = "INSERT INTO details (description, temple, quantity, price)
            VALUES (:description, :temple, :quantity, :price)";
}
    $stmt = $dbh->prepare($query);
        $stmt->bindParam(':description', $_POST['description']);
        $stmt->bindParam(':temple', $_POST['temple']);
        $stmt->bindParam(':quantity', $_POST['quantity']);
        $stmt->bindParam(':price', $_POST['price']);
       $stmt->execute();

{
	$error_msg = array();

	}
	if(!isset($_POST['temple'])){
		$error_msg[] = "No temple was selected.";
	}
	
	if(!isset($_POST['quantity'])){
		$error_msg[] = "No quantity was selected.";
	}

	if(isset($error_msg) && count($error_msg) == 0){
		// do some form processing
	}
	else{
		// redirect to the form again.
		http:soloutionm3.com/ordertest.html
	}

} catch (PDOException $exception) {
echo $exception->getMessage(), "<br />";
}

thanks for the look.

1 Like