You are on page 1of 9

Nguyen Truong Sinh (Editor)

Using PHP and MySQL

Dynamic Web Design

Thong Ke Publishing House

PREFACE

Nowadays, static Web sites with simple HTML pages no longer meet the needs of users.
Dynamic Web sites combined with databases have become the Web development trend.
This book will introduce you to two of the most popular technologies used to create dynamic
Web sites: the PHP scripting language and the MySQL database management program.
Through guided practise steps, clear scripting codes with many expert hints, the book will
help you get a general understanding of technology before addressing specific issues such
as security, session and cookies, using Web tools. At the end of the book, we will apply the
techniques introduced to build some complete sample applications. For the convenience of
the practice part, you can copy the reference code used for the book from the address:
http://minhkhai.com.vn/dounload.html.

MK.PUB

Note: In this book, in the code section, the symbol (-> ) indicates that this command line is
followed by the above command line.
TABLE OF CONTENTS

PREFACE ......................................................................................................................... 3

ANSWER OPEN ............................................................................................................... 4

TABLE OF CONTENTS .................................................................................................... 5

The opening chapter ........................................................................................................ 7

What is a dynamic website? .............................................................................................11

What is PHP? ..................................................................................................................11

Why use PHP?.................................................................................................................12

How PHP works ...............................................................................................................13

What is MySQL? .............................................................................................................13

What do you need? ..........................................................................................................15

Talking about the book .....................................................................................................16

Is this book for you? .........................................................................................................16

Chapter 1: INTRODUCTION TO PHP ..............................................................................17

Basic syntax .....................................................................................................................17

Send data to the Web browser .........................................................................................20

Learn PHP, HTML and whitespace ..................................................................................24

Write notes.......................................................................................................................28

What is a variable? ..........................................................................................................30

About string variables ( string ) .........................................................................................34

String Match .....................................................................................................................36

Introducing numeric variables ( number ) .........................................................................39

Introduction to constants ..................................................................................................43

Double and single quotes .................................................................................................46

Chapter 2: PROGRAMMING WITH PHP .........................................................................49

Create HTML forms .........................................................................................................49

HTML form handling.........................................................................................................53

Manage Magic Quotes .....................................................................................................57


Conditional statements and operators ..............................................................................60

Switch ..............................................................................................................................65

Check the validity of form data .........................................................................................66

Manually send values to script code.................................................................................72

What is an array? .............................................................................................................79

Create array .....................................................................................................................80

Array Retrieval .................................................................................................................80

Multidimensional array .....................................................................................................86

Arrays and Strings ...........................................................................................................86

Sort array .........................................................................................................................95

for and while loops ...........................................................................................................95

CHAPTER 3: CREATE A DYNAMIC WEBSITE .............................................................100

Using external files .........................................................................................................100

Site structure ..................................................................................................................105

Create and call your own functions ................................................................................107

Create a function that takes parameters.........................................................................113

Set default parameters ...................................................................................................117

Returning values from a function ....................................................................................121

Scope of the variable .....................................................................................................125

Form Processing with PHP Redux .................................................................................130

Send emails ...................................................................................................................140

HTTP Headers ...............................................................................................................180

Create memo form ( sticky form ) ...................................................................................181

Date and time functions .................................................................................................182

Chapter 4: INTRODUCTION TO SQL AND MYSQL ......................................................183

Table definition...............................................................................................................183

NULL and default value..................................................................................................185

Using MySQL Manager ..................................................................................................186


Create databases and tables .........................................................................................187

Insert message ..............................................................................................................188

Two MySQL functions ....................................................................................................189

Data access ...................................................................................................................190

Use condition .................................................................................................................190

Use LIKE and NOT LIKE................................................................................................191

Sort query results ...........................................................................................................192

Limit return results .........................................................................................................192

Updating data ................................................................................................................192

Delete data ....................................................................................................................193

Chapter 5: ADVANCED SQL AND MYSQL ....................................................................195

Database Design ...........................................................................................................195

Standardized ..................................................................................................................195

Lock up ..........................................................................................................................196

Relation..........................................................................................................................198

First normalized form .....................................................................................................199

Second normalized form ................................................................................................200

The third normalized form ..............................................................................................202

Create a database .........................................................................................................204

Using function ................................................................................................................215

Text functions ................................................................................................................216

Numerical Functions ......................................................................................................218

Date and time functions .................................................................................................220

Date and time format......................................................................................................223

Grouping functions .........................................................................................................226

Indexing .........................................................................................................................228

Chapter 6: USING PHP AND MYSQL ...........................................................................233

Create a template ..........................................................................................................233


Connect to MySQL and access the database ................................................................239

Error handling ................................................................................................................244

Make simple queries ......................................................................................................247

Retrieve query results ....................................................................................................259

Safe ...............................................................................................................................263

Magic Quotes and mysql_real_escape_string ( )............................................................263

Using mysql_num_rows ( ) .............................................................................................271

Update records with PHP ...............................................................................................278

Chapter 7: COOKIE AND SESSION ..............................................................................287

Use cookies ...................................................................................................................287

Check cookies................................................................................................................288

Set up cookies ...............................................................................................................295

Retrieve cookies ............................................................................................................299

Set cookie parameters ...................................................................................................299

Delete cookies ...............................................................................................................303

Using Sessions ..............................................................................................................309

Set session variable .......................................................................................................310

Retrieve session variable ...............................................................................................314

Clear session variables ..................................................................................................319

Change the behaviour of the session .............................................................................320

Sessions and cookies ....................................................................................................321

Change session-related cookie settings .........................................................................322

Use sessions without cookies ........................................................................................322

Chapter 8: SAFETY .......................................................................................................333

HTTP Authentication ......................................................................................................334

Form Testing with JavaScript .........................................................................................335

Rule expression .............................................................................................................336

Template definition .........................................................................................................337


Pattern Match ................................................................................................................338

Pattern matching and replacement.................................................................................339

Data security ..................................................................................................................340

Safe practice ..................................................................................................................341

Encode ..........................................................................................................................342

Chapter 9: WEB APPLICATION DEVELOPMENT .........................................................343

PHP debugging techniques ............................................................................................344

SQL and MySQL debugging techniques ........................................................................345

Debugging SQL problems ..............................................................................................346

Detecting errors related to access ..................................................................................347

PHP Error Management .................................................................................................348

Error message ...............................................................................................................349

Error recording ...............................................................................................................350

MySQL Error Management ............................................................................................351

Improve Web Application Performance ..........................................................................352

Table optimization ..........................................................................................................353

Interpreting queries ........................................................................................................354

Chapter 10: EXPANDED TOPICS..................................................................................355

Output buffer ..................................................................................................................356

Caching the Web page...................................................................................................357

PHP and JavaScript browser detection ..........................................................................358

Using PEAR ...................................................................................................................358

Chapter 11: EXAMPLES - CONTENTS MANAGEMENT ...............................................360

Create a template ..........................................................................................................368

Create basic pages ........................................................................................................370

Manage URLs ................................................................................................................430

Additional URLs .............................................................................................................430

View Uploaded URLs .....................................................................................................443


Manage files ..................................................................................................................450

Upload files ....................................................................................................................450

View and download files.................................................................................................560

Chapter 12: EXAMPLE - USER REGISTRATION ..........................................................467

Create a template ..........................................................................................................467

Write configuration script code .......................................................................................472

Create a config file .........................................................................................................472

Generate database script code ......................................................................................475

Create homepage ..........................................................................................................479

Register .........................................................................................................................482

Log in and out ................................................................................................................494

Password Manager ........................................................................................................502

Reset password .............................................................................................................502

Change the password ....................................................................................................509

Site Administration .........................................................................................................515

Moulds ...........................................................................................................................519

Perform a validation check .............................................................................................521

Admin home page View users........................................................................................522

Chapter 13: EXAMPLE – E-commerce ..........................................................................531

Create a database .........................................................................................................536

Administration section ....................................................................................................551

Create a template for the public section .........................................................................556

Product portfolio .............................................................................................................566

Shopping cart .................................................................................................................567

Additional items..............................................................................................................569

View shopping cart .........................................................................................................577

Appendix A: INSTALLATION .........................................................................................577

Install on Windows .........................................................................................................584


MySQL Access Permissions ..........................................................................................585

Set password for the root account ..................................................................................586

Create an account and assign permissions ....................................................................586

Check the installation .....................................................................................................590

Appendix B: THIRD GROUP APPLICATIONS ....................................................................

phpMyAdmin .......................................................................................................................

Mold Systems .....................................................................................................................

Forum software ...................................................................................................................

Content Management..........................................................................................................

Ecommerce.........................................................................................................................

Search tools ........................................................................................................................

Code library ........................................................................................................................

Appendix C: REFERENCES ...............................................................................................

PHP ....................................................................................................................................

Operators and comparison operators. .................................................................................

Date and time .....................................................................................................................

PHP MySQL Functions .......................................................................................................

Rule expressions ................................................................................................................

Other references .................................................................................................................

MySQL ................................................................................................................................

Functions ............................................................................................................................

Date, time ...........................................................................................................................

Appendix D: REFERENCES ...............................................................................................

PHP ...................................................................................................................................

Websites .............................................................................................................................

Newsgroup (newsgroup) and mailing list (mailing list) .........................................................

MySQL ................................................................................................................................

MySQL Tools ......................................................................................................................


SQL ....................................................................................................................................

Safe ....................................................................................................................................

Other contain ......................................................................................................................

Generality ...........................................................................................................................

Web Development ..............................................................................................................

HTML ..................................................................................................................................

JavaScript ...........................................................................................................................

Apache Server ....................................................................................................................

Ecommerce.........................................................................................................................

You might also like