Not writing to a MySQL table via PDO, say what!

Okay newbie here so probably have something completely wrong, actually forgot to encrypt the password for starters :-[

[php]
try {
$dbh = new PDO(“mysql:host=$host;dbname=$dbname”, $dbusername, $dbpassword);
}
catch(PDOException $e)
{
$error = "Database Error ".$e->getmessage();

    	include($globalvar['default_template'].'header.php');
		include($globalvar['default_template'].'temp_error.php');
		include($globalvar['default_template'].'footer.php');	
		exit;	    	
	}
	
	/* Okay let's write this bad boy away */
	
	try {
    	
    	$sql = "INSERT INTO lb_member (username, password, email, firstname, lastname, group, created) VALUES (:username, :password, :email, :firstname, :lastname, :group, :created)";
		$q = $dbh->prepare($sql);
		$q->execute(array(':username'=>$username, 
						':password'=>$password,
						':email'=>$email, 
						':firstname'=>$firstname, 
						':lastname'=>$lastname, 
						':group'=>$group, 
						':created'=>$created));
						
		$message = "Account Successfully Created";				

    	include($globalvar['default_template'].'header.php');
		include($globalvar['default_template'].'temp_message.php');
		include($globalvar['default_template'].'footer.php');	
		exit;
			    	
	}
	catch(PDOException $e)
	{
    	$error = "Database Error ".$e->getmessage();
    	
    	include($globalvar['default_template'].'header.php');
		include($globalvar['default_template'].'temp_error.php');
		include($globalvar['default_template'].'footer.php');	
		exit;	    	
	}

[/php]

Not getting any errors, the db connect seems to be rocking, but doesn’t throw an error. So what am I doing wrong?

In your instance, you need to put backticks around your column names. the word “group” is a mysql reserved keyword, so the query thinks you are trying to do something else.
[php]INTO lb_member (username, password, email, firstname, lastname, group, created)[/php]
Should be this
[php]INTO lb_member (username, password, email, firstname, lastname, group, created)[/php]

Wow thanks fastsol, off to try that out and getting rocking :slight_smile: D’uh on the group thing, will change that immediately.

Am intrigued as to why PDO doesn’t report an issue when I’ve enclosed it in the “Try” construct? Do I need to add something additional to make error checking more rigorous? Sorry new to this so some pretty obvious questions for a while :slight_smile: And hey haven’t even started asking about CLASS thingies yer ;D

Sorry couldn’t edit my previous post. Awesome thanks, all working now, and yes changed the name of the “group” column :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service