Loud Bite Signup Page

Open up the AccountController.php file and look at successAction(). The action takes the user-supplied data, cleans it up, and then does nothing. You need to fill in that void now. Right after the user's data is cleaned, you need to add in the functionality to save the data. Using the techniques you learned, let's create the insert functionality.

The updated successAction() is shown in Listing 5-8. The action is triggered after the user submits the form to add a new account and is the action that enters a new user into the system.

Listing5-8. AccountController.php: SuccessAction

* Process Sign up Form.

public function successAction() {

//Check if the submitted data is POST type if($form->isValid($_POST)){

$email = $form->getValue("email"); $username = $form->getValue("username"); $password = $form->getValue("password");

//Create Db object require_once "Db/Db_Db.php"; $db = Db_Db::conn();

//Create the record to save into the Db. $userData = array("username" => $username, "email" => $email, "password" => $password, "status" => 'pending',

"created_date" => new Zend_Db_Expr("NOW()"));

try{

//Insert into the accounts. $db->insert('accounts', $userData);

//Get the Id of the user $userId = $db->lastInsertId();

//Send out thank you email. We'll get to this. Chapter 6. }catch(Zend_Db_Exception $e){ $this->view->form = $form;

$this->view->errors = $form->getMessages(); $this->view->form = $form;

Reviewing the action, begin by ensuring that the form has no errors, and the form values meet the required formats. If there are no errors, initialize the $username, $email, and $password variables using the Zend_Form object's getValue(). You then create an instance of the Zend_Db_Adapter_Pdo_Mysql object using the database model. After you have the database object, initialize the data you will save into the database using the $userData variable. You will then pass in accounts and the $userData variable into the insert() method. I also recommend that you wrap the logic in try-catch just in case there are any issues with the insert. If there are no errors, return the unique user ID using lastInsertId() and eventually send out the verification e-mail (covered in Chapter 6).

Open the sign-up form in the browser http://localhost/account/new, fill out the form, and click Create My Account. If there are no database errors, you are now one of the first real users in the application.

Was this article helpful?

0 0

Post a comment