BASIC PHP and MYSQL

Download Report

Transcript BASIC PHP and MYSQL

BASIC PHP and
MYSQL
Edward S. Flores
SQL
What is SQL?
 SQL stands for Structured Query Language
 SQL allows you to access a database
 SQL is an ANSI standard computer language
 SQL can execute queries against a database
 SQL can retrieve data from a database
 SQL can insert new records in a database
 SQL can delete records from a database
 SQL can update records in a database
 SQL is easy to learn
PHP
What is PHP?
PHP originally meant “Personal Home Page” as it was
created in 1994 by Rasmus Lerdorf to track the visitors to
his online resume.
As its usefulness and capabilities grew (and as it started
being used in more professional situations), it came to
mean “PHP: Hypertext Preprocessor.”
PHP is an HTML embedded scripting language.
 HTML embedded means that PHP can be interspersed
within HTML which makes developing dynamic Web
sites more accessible.
 PHP is scripting language as opposed to a programming
language. It is designed to do something only after an
event occurs—for example when a user submits a form
or goes to a URL.
 PHP is a server-side, cross-platform technology. Serverside refers to the fact that everything PHP does occurs
on the server. Its cross-platform nature means that PHP
runs on most operating systems, including Windows,
UNIX (any variants), and Macintosh.
 PHP scripts written on one server will normally work on
another with little or no modifications.
What Do I Need?
As mentioned earlier, PHP is a server-side scripting
language. This means that, although your users will not
need to install new software, you web host will need to
have PHP set up on their server. It should be listed as
part of your.
Writing PHP
Writing PHP on your computer is actually very simple. You
don't need any special software, except for a text editor
(like Notepad in Windows). Run this and you are ready
to write your first PHP script.
Declaring PHP
PHP scripts are always enclosed in between two PHP tags.
This tells your server to parse the information between
them as PHP. The three different forms are as follows:
Declaring PHP
<?
PHP Code In Here
?>
OR
<?php
PHP Code In Here
php?>
PHP Variables
As with other programming languages, PHP allows you to
define variables. In PHP there are several variable types,
but the most common is called a String. It can hold text
and numbers. All strings begin with a $ sign. To assign
some text to a string you would use the following code:

$welcome_text = "Hello and welcome to my website.";
This is quite a simple line to understand, everything inside
the quotation marks will be assigned to the string. You
must remember a few rules about strings though:
Strings are case sensetive so $Welcome_Text is not the
same as $welcome_text
String names can contain letters, numbers and
underscores but cannot begin with a number or
underscore
When assigning numbers to strings you do not need to
include the quotes so:

$user_id = 987
would be allowed.
Outputting Variables
To display a variable on the screen, use exactly the same
code as to display text but in a slightly different form.
The following code would display your welcome text:
<?
$welcome_text = "Hello and welcome to my website.";
Print $welcome_text;
?>
As you can see, the only major difference is that you do
not need the quotation marks if you are printing a
variable.
Formatting Your Text
For this example I will change the text to the Arial font in red.
The normal code for this would be:
 <font face="Arial" color="#FF0000"> </font>
As you can see this code contains 4 quotation marks so
would confuse the script. Because of this you must add a
backslash before each quotation mark to make the PHP
script ignore it. The code would change to:

<font face=\"Arial\" color=\"#FF0000\"> </font>
You can now include this in your print statement:

print("<font face=\"Arial\" color\"#FF0000\">Hello and
welcome to my website.</font>");
which will make the browser display:
 Hello and welcome to my website.
because it has only been sent the code:

<font face="Arial" color="#FF0000">Hello and welcome
to my website.</font>
You can also use the code
<font face="Arial" color"#FF0000"><? Print “Hello and
welcome to my website.”; ?></font>")
This will also output the same result.
Getting Started
1. Open a NOTEPAD or DREAMWEAVER.
2. Create a file named index.php and save it in the path
C:\xampp\htdocs inside the folder named as your last
name.
3. Insert a PHP tag.
<? ?>
4. Save the file.
My First PHP with
MYSQL Project
Step1.
Open the index.php file in myproject folder in the path
C:\xampp\htdocs with DREAMWEAVER or NOTEPAD.
Step 2.
Open a web browser into http://localhost/phpmyadmin
Step 3.
Create a database named myfriends.
Create a table within the database named frnd_names
With columns id (5), name (50) and age (3).
Connecting to the Database
Include these PHP codes into the index.php page.
Host name = “localhost”
Server
= “root”
Password = “ ” or blank
Database name
Table name
= “myfriends”
= “ frnd_names”
Mysql_connect (“localhost”,”root”,””);
Mysql_select_db(“myfriends”);
Adding Values into the Database
with PHP
Step 1.
Create a file named add.php and save it in the pages
Folder.
Step 2.
Complete the link in the index.php to show the add.php
page
Step 3.
Insert a FORM (dreamweaver)
INSERT -> FORM -> FORM
Or (notepad)
<form name="form1" method="post" action=""> </form>
Step 4.
create a table with 3 rows and 2
columns.
Step 5.
Insert the LABELS, TEXT FIELDS and SUBMIT BUTTONS.
Rename the TEXT FIELDS as lbl_name and
lbl_age.
Step 6.
Type the FORM ACTION of the page.
The FORM ACTION is the file that will handle the data
inside the form.
<form name="form1" method="post" action=“add_db.php">
Step 7.
Create the file in the FORM ACTION and save it in the
pages folder.
Step 8.
In the page add_db.php (form action file) call the values
of the text fields and store it in a variable.
$variable = $_POST[‘TEXT_FIELD_NAME’];
Step 9.
Use a SQL query that will add a value into the database.
Mysql_query(“insert into tbl_name values (‘value1’,’value2’)”);
*You can include the query into a variable for other purposes.
*Don’t forget to include the database connection code in this page.
Step 10.
To direct the page to another page after pushing the
submit button, use the header format, the variable and an
IF statement.
IF (something == something else) {
THEN Statement
}
else { ELSE Statement }
*You can also use the IF Statement to validate the content of the variable
Header (“location: path”);
Viewing the Contents on the
Database with PHP
Step 1.
Create a file named view.php and save it in the pages
Folder.
Step 2.
Complete the link in the index.php to show the view.php
Page
Step 3.
Create 2 tables with 1 row and 2 columns.
On the First column, type the word NAME, type the word
AGE on the second column.
Step 4.
Use a SQL query and a WHILE STATEMENT that will
enable you to view the Contents of a table in a
database.
Mysql_query(“select * from tbl_name where value=‘value’”);
WHILE STATEMENT
while (CONDITION) {
DO
}
Editing the Contents on the
Database with PHP
Step 1.
Create a file named edit.php and save it in the pages
Folder.
Step 2.
Complete the link in the index.php to show the edit.php
Page
Step 3.
Create 2 tables with 1 row and 3 columns.
On the First column, type the word NAME, type the word
AGE on the second column and ACTION on the third
column.
Step 4.
On the third column on the second table, type the word
EDIT and use it as a link to the another page.
Step 5.
Use a SQL query and a WHILE STATEMENT that will
enable you to view the Contents of a table in a
database.
Mysql_query(“select * from tbl_name where value=value”);
WHILE STATEMENT
while (CONDITION) {
DO
}
Step 6.
Create a file named do_edit.php and save it in the pages
Folder.
Step 7.
On the do_edit.php file, use a SQL Query and a WHILE
STATEMENT to retrieve data from the database.
Mysql_query(“select * from tbl_name where value=value”);
WHILE STATEMENT
while (CONDITION) {
DO
}
Step 8.
Insert a FORM (dreamweaver)
INSERT -> FORM -> FORM
Or (notepad)
<form name="form1" method="post" action=""> </form>
Step 9.
Insert the LABELS, TEXT FIELD and SUBMIT BUTTONS.
Step 10.
Place the values from the WHILE STATEMENT in the
TEXT FIELDS.
Step 11.
Type the FORM ACTION of the page.
The FORM ACTION is the file that will handle the data
inside the form.
<form name="form1" method="post" action=“edit_db.php">
Step 12.
Create the file in the FORM ACTION and save it in the
pages folder.
Step 13.
Create a HIDDEN TEXT FIELD in the do_edit.php file
containing the ID field.
Step 14.
In the page edit_db.php call the values of the text fields
and store it in a variable.
$variable = $_POST[‘TEXT_FIELD_NAME’];
Step 15.
Use a SQL query that will update a value on the database.
Mysql_query(“update tbl_name set column=‘value1’,
column2=‘value2’”);
*You can include the query into a variable for other purposes.
*Don’t forget to include the database connection code in this page.
Deleting the Contents on the
Database with PHP
Step 1.
Create a file named delete.php and save it in the pages
Folder.
Step 2.
Complete the link in the index.php to show the delete.php
Page
Step 3.
Create 2 tables with 1 row and 3 columns.
On the First column, type the word NAME, type the word
AGE on the second column and ACTION on the third
column.
Step 4.
On the third column on the second table, type the word
DELETE and use it as a link to the another page.
Step 5.
Use a SQL query and a WHILE STATEMENT that will
enable you to view the Contents of a table in a
database.
Mysql_query(“select * from tbl_name where value=value”);
WHILE STATEMENT
while (CONDITION) {
DO
}
Step 6.
Create a file named do_detete.php and save it in the
Pages Folder.
Step 7.
Use a SQL query that will update a value on the database.
Mysql_query(“delete from tbl_name where column=‘value1’”);
*You can include the query into a variable for other purposes.
*Don’t forget to include the database connection code in this page.
--END-Edward S. Flores