Cazpa.com PHP Developer & Website Designer

14Jan/100

Add – Update & Delete records from a MySQL database using PHP

The purpose of this PHP script is to show you how to connect to a MySql database, retrive records, add records,  update them and delete them, this script also uses Javascript to query the database to cut down on page refreshes.

I will add a full explenation very soon but for now you can download the example PHP script to see how you could complete the tasks.

Any questions then please leave a comment and I will get back to you.

http://cazpa.com/wp-content/plugins/downloads-manager/img/icons/winzip.gif download: Add-Update-Delete-PHP-Mysql (5.51KB)
added: 14/01/2010
clicks: 436
description:

6Dec/090

PHP Functions (Arguments & Values)

Creating a basic fuction

Functions are great for cutting down on code and making things easier to read when developing, you can create a function
for just about any process including conditional statements, loops or even calls to other functions.

This article will show you how to create simple functions for your applications and website's.

First things first we need to know how to define and invoke a function:

<?php
// Define your function
function displayEinsteinQuote()
{
	echo "Do not worry about your difficulties in Mathematics. I can assure you mine are still greater.";
}
?>
<?php
// Invoke your Albert Einstein Quote
displayEinsteinQuote()
?>

Once you invoke your function this will output:

Do not worry about your difficulties in Mathematics. I can assure you mine are still greater.

Now I know what you are thinking, why not just echo the quote to begin with? well the next example will show you how to use
a variable to create a more dynamic function

<?php
// Define your function with simple argument
function ConvertStoneToKilos($stone)
{
$diff = '6.35029318';
$total = ($stone*$diff);

	echo "$stone stone is the equivilant to $total kilograms";
}
?>
<?php
// Invoke the function
ConvertStoneToKilos(11);
?>

This will convert eleven stone into kilograms (69.85322498), of coarse changing the varaiable in the brackets will change the output!

7Nov/090

PHP Operators (Basic PHP Operators)

Now that we have the variables lets do some maths using operators, operators are the symbols that connect all the variables together.

Simple maths with PHP:

<?php
// Define variables
$number1 = 123;
$number2 = 2;

// Add
$total1 = $number1 + $number2;

// Subtract
$total2 = $number1 - $number2;

// Multiply
$total3 = $number1 * $number2;

// Divide
$total4 = $number1 / $number2;

// Modulus
$total5 = $number1 % $number2;
?>

Now lets use auto-increment and auto-decrement operators

Auto-increment

<?php
// Define variable
$total = 123;

// increment it
$total++;
// $total is now 124

// This is the same as
$total = $total + 1;
?>

Auto-decrement

<?php
// Define variable
$total = 123;

// decrement it
$total--;
// $total is now 122

// This is the same as
$total = $total - 1;
?>

7Nov/090

PHP Commenting your code

You may have noticed that I have commented the code and if you have tested any of the scripts so far you will have noticed that these do not effect the script itself, these are great for developers to take note of lines of code, greater understanding by other developers as to why you have done certain things and a lot of developers use the multiple line comment to display licensing information, here are the different types of comment you can use:

<?php
// This is a single line comment

# This is also a single line comment

/* This is a
multiple line
comment */
?>

If you wish to comment in HTML then use the following:
<html>
<title></title>
<body>
<!-- Your comment here -->

<?php
// Code here!
?>
</body>
</html>

7Nov/090

PHP Variables (PHP Basics)

Now you have the basic layout down to an art, it is time to make our basic script more dynamic and we will be firstly doing this using variables, variables are strings that change

In the following example we will be assigning variables and using them within the exciting script

<html>
<head>PHP Test Page</head>
<body>
<p>Jack is wearing green trousers</p>

<?php
// Define your variable
$name = 'Jill';

// Now display the variable we have set
echo "<p>$name is wearing a blue skirt</p>";
?>
</body>
</html>

This may sound very simple and pointless at the moment but the further you get into PHP, the more you will realize just how much you use variables ... Please note that when you echo a variable you must enclose it within double quotes, single quotes will not work, you can echo variables on their own in the following way:

<?php
// Define your variable
$color = 'red';

// Display variable
echo $color;
?>

//You can also display a variable like this:
<?php
// Define your variable
$color = 'red';

echo '<p>My favorite color is ';
echo $color;
echo ' what is yours?</p>';

//This will display: My favorite color is red what is yours?
?>

Now we know how to display single variables we can move onto displaying multiple variables.

<?php
// Define variables
$name = 'Jill';
$age  = '23';
$email= 'jill@example.com';

// Build a sentence with all the variables

echo "$name is currently $age years old, you can contact her on $email";
?>

The sentence above is all well and good but it is a little bland, now lets make it a little dynamic

<?php
// Define variables
$name = 'Jill';
$age  = '23';
$email= 'jill@example.com';

// Build a sentence with all the variables

echo "$name is currently $age years old, you can contact her <a href=\"mailto:$email\">here</a>";
?>

You will notice the slashes added to the left of the double quotes, these are important to keep the string from generating errors.

7Nov/090

Introduction to PHP (beginners)

The main aim of this blog category is to teach you the basics of PHP, by the time I have finished my posts you will be able to use your new found knowledge and build yourself your first basic content management system and to give you an insigt into building applications with PHP, the further we progress, the more complex things will get so if you are a PHP beginner then it might be a good thing to start atthe beginning.

Basic Page Layout

PHP allows you to embed commands into HTML pages and wrapped around these commands must always be special start and end tags:

<?php
// Some PHP code here
?>

Now we know how to embed PHP into a HTML page, lets se a sample of this in action:

<html>
<head>PHP Test Page</head>
<body>
<p>Jack is wearing green trousers</p>

<?php
echo '<p>Jill is wearing a blue skirt</p>';
?>
</body>
</html>

Feel free to save this script to your server and test it out.

You will see by the diagram that we have just created our first PHP script!

   
Powered by WordPress Lab