<?php
session_start ();
// Only process this call if we have a query string
// this can be removed if needed
if (isset ( $_POST ['name'] )) {
	// Connect to db
	// Notice the use of _cq after the database connection items.
	// this helps to ensure these calls do not interfere with our database calls.
	$db_vendor_cc = 'mysql';
	$db_mysql_socket_cc = '';
	$db_hostname_cc = 'localhost';
	$db_catalog_cc = 'test';
	$db_mysql_port_cc = '';
	$user_cc = '';
	$pass_cc = 'test';
	$dsn_cc = $db_vendor_cc . ":unix_socket=" . $db_mysql_socket_cc . ";host=" . $db_hostname_cc . ";dbname=" . $db_catalog_cc . ";port={$db_mysql_port_cc};";
	
	// create db object
	try {
		$dbh = new PDO ( $dsn_cc, $user_cc, $pass_cc );
		$dbh->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
	} catch ( PDOException $e ) {
		die ( $e->getMessage () );
	}
	
	// Assign variable values from URL items
	// *EDIT* change the POST value to your actual values, or add as needed
	// Be sure to use token syntax, as in: #{field_name}
	$in_name = isset ( $_POST ['name'] ) ? $_POST ['name'] : "";
	
	$name = $in_name;

	// create query logic
	$sql = "SELECT name FROM fb_demo WHERE name = :name";
	
	try {
		$stmt = $dbh->prepare ( $sql );
	} catch ( PDOException $e ) {
		echo $e->getMessage ();
	}
	
	// bind values to our prepared statement
	$stmt->bindValue ( ':name', $name, PDO::PARAM_STR );
	
	// execute the query
	$result = $stmt->execute ();
	
	if ($result) {
		$row = $stmt->fetch ( PDO::FETCH_ASSOC );
		//print_r($row); die(); // debug the raw result
		if ($row) {
			// collect vars from query if needed
			$_SESSION ['name'] = $row ['name'];
			// !IMPORTANT! Bypass security check by setting a 
			// session variable for the page we want to reach next.
			// If you do not do this the form will simply
			// redirect back to this original page.
			$_SESSION ['pages'] ['page1.php'] = 'yes';
			// redirect to the success page
			header ( "Location: page1.php" );
			exit ( 0 );
		} else {
			// redirect to a different page, or do something else
			header ( "Location: page0.php?fail=1" );
			exit ( 0 );
		}
		
	} // if result
	
} // end: if (isset ( $_POST ['name'] )) {
?>