HTML/Javascript

SES by business degree online promotion team.

Philippine Airlines International Flight schedules

Philippine Airlines International Flight schedules
Philippine Airlines International Flight schedules

Search This Blog

Thursday, September 23, 2010

Installation of XAMPP to run PHP

From: www.apachefriends.org
Just type "Apachie friends" from google search engine then click search.
note: you can choose old version of xampp.

1.



2.then choose for windows if your using windows operating system.


3. then click EXE file


4.



5.



6.




7.



8.



9.




10.




11.




12.




13.



14.




15.
note: click start in order to run Apache & Mysql.
Hide/Close the XAMPP Control Panel Application






16.

note: just open another mozilla browser, then type http://localhost and press enter.
Automatically you redirected to xampp server.
Choose English



17.




18. Click Security in order to put username & password for your server.




19. note: input first for mysql root password.
note: follow other instruction base from the image.




20.after, click password changing button.




21. Second put username & password for your xampp directory.




22.



note: for beginner, much better to use password "root" for the Msql Section first. & "ila" password for the XAMPP DIRECTORY PROTECTION in order to make
familiar installation with security.


AFTER INSTALLTION OF XAMPP TO YOUR COMPUTER, TRY SAMPLE CODE. SEE BELOW:


TEST YOUR PHP SERVER BY CREATE YOUR FIRST PHP SCRIPT SAMPLE: HELLO
WORLD


Before you can write php code, be sure that you have finished installed notepad2/dreamweaver on your windows operating system.

First, Open notedpad2.
picture




Second, Inside notepad2 write php script:
example:





Third, Save your first php script.





Fourth, Run your script.
Open Internet Explorer/Mozilla Browser, then type http://localhost/hello_world.php and press enter.

Output:








Tuesday, September 21, 2010

Display/Query data from myql database and create alternate background color using FOR LOOP

code to paste:

$con = mysql_connect("localhost","root","yourpassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("personalinfo", $con);

// Alternate background color of the table rows//
function row_color($x){
$background_a = "#4169E1";
$background_b = "#A0522D";

if ( $x%2 ) {
return $background_a;
} else {
return $background_b;
}
}

//connection to the database
$connection = mysql_connect("localhost","root","htciton");
$db = mysql_select_db("personalinfo");

$sql = "select * from table1"; // locate table
$result = mysql_query("$sql"); // display the data output from mysql database table1

// create table
echo "<table bgcolor=#FFFFFF border=0 cellpadding=3 cellspacing=3 align=center>\n";
echo "<tr bgcolor='#4169E1'>";
echo "<td align='center'><b>IDNO</b></td>";
echo "<td align='center'><b>LASTNAME</b></td>";
echo "<td align='center'><b>FIRSTNAME</b></td>";
echo "<td align='center'><b>AGE</b></td>";
echo "<td align='center'><b>SEX</b></td>";
echo "<td align='center'><b>CIVIL STATUS</b></td>";
echo "<td align='center'><b>ADDRESS</b></td>";
echo "</tr>";
// call the fields
for( $x = 0; $x < $row = mysql_fetch_array($result); $x ){
echo "<TR>\n"
."<TD bgcolor=".row_color($x).">".$row["idno"]."</TD>\n"
."<TD bgcolor=".row_color($x).">".$row["lastname"]."</TD>\n"
."<TD bgcolor=".row_color($x).">".$row["firstname"]."</TD>\n"
."<TD bgcolor=".row_color($x).">".$row["age"]."</TD>\n"
."<TD bgcolor=".row_color($x).">".$row["sex"]."</TD>\n"
."<TD bgcolor=".row_color($x).">".$row["civilstatus"]."</TD>\n"
."<TD bgcolor=".row_color($x).">".$row["address"]."</TD>\n"
."</TR>";
}
echo "</TABLE>";
mysql_close($con);
?>

output:

Monday, September 20, 2010

Multiple File Upload & Data Submission with File Validation





code to paste:


submit.php

<html>
<body><br>
<table border="0" cellpadding="2" cellspacing="2" align="center" bgcolor="#cccccc">
<form action="upload.php" method="post" enctype="multipart/form-data">
<tr>
<td colspan="3" align="center"><font face="verdana" size="2"><b>Data & File Submission</b></font></td>
</tr>
<tr>
<td align="right"><font face="verdana" size="2">Complete Name:</font></td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td align="right"><font face="verdana" size="2">Upload filename .tx, .doc extension:</font></td>
<td><input type="file" name="file1" id="file" /> </td>
</tr>
<tr>
<td align="right"><font face="verdana" size="2">Upload filename .jpg, .gid extension:</font></td>
<td><input type="file" name="file2" id="file" /></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="submit" value="Submit" /></td>
</tr>
</form>
</table>
</body>
</html>


upload.php

<?php
$name=$_REQUEST['name'];
$filename=($_FILES['file1']['name']);
$filename2=($_FILES['file2']['name']);

echo $name;
echo "<br>";
echo $filename;
echo "<br>";
echo $filename2;

$ext1 = strrchr($filename, ".");
$ext2 = strrchr($filename2, ".");

echo "<br>";
echo $ext1;
echo "<br>";
echo $ext2;

if (($ext1==".txt" || $ext2==".doc") && ($ext2==".jpg" || $ext2==".gif"))
{
$target = "upload/";
$targetx = $target . basename( $_FILES['file1']['name']);
//Writes the photo to the server
if(move_uploaded_file($_FILES['file1']['tmp_name'], $targetx))
{
//Tells you if its all ok
}
else
{
//Gives and error if its not
}

$targetx = $target . basename( $_FILES['file2']['name']);
//Writes the photo to the server
if(move_uploaded_file($_FILES['file2']['tmp_name'], $targetx))
{
//Tells you if its all ok
}
else
{
//Gives and error if its not
}
echo "<br>";
echo "<br>";
echo "<br>";
echo "<p align='center'><font face='verdana'>data & file submission success!</font></p>";
}
else
{
echo "<p align='center'><font face='verdana'>Input a valid required file, go back to previous page!</font></p>";
}
?>


Wednesday, September 15, 2010

Complete Javascript Validation for your PHP

To have validation of your html form, you can add javascript code before the body tag.
Try this code by copy then paste it to your .html or .php extension file type.
note: You can modify this code.



code to paste:

<html>
<head>
<link type="text/css" rel="stylesheet" href="images/style.css" />
<script type='text/javascript'>
function formValidator()
{
// Make quick references to our fields
var lastaname = document.getElementById('lastname');
var firstname = document.getElementById('firstname');
var mi = document.getElementById('mi');
var age = document.getElementById('age');
var sex = document.getElementById('sex');
var religion = document.getElementById('religion');
var email = document.getElementById('email');
var timedeparture = document.getElementById('timedeparture');
var ampmdep = document.getElementById('ampmdep');
var dayarrival = document.getElementById('dayarrival');
var timearrival = document.getElementById('timearrival');
var ampmar = document.getElementById('ampmar');
var noofseats = document.getElementById('noofseats');
// Check each input in the order that it appears in the form!
if(isAlphabet(lastname, "Please type letter for your lastname")){
if(isAlphabet(firstname, "Please type letter for your firstname")){
if(isAlphabet(mi, "Please type letter for your civilstatus")){
if(isNumeric(age, "Please type number for your age")){
if(madeSelection(sex, "Please choose for sex")){
if(madeSelection(civilstatus, "Please choose for civil status")){
if(isAlphabet(religion, "Please type letter for your religion")){
if(emailValidator(email, "Please type a valid email address")){
return true;
}
}
}
}
}
}
//
}
}
return false;
}

function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}

function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9] $/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z] $/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z] $/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function lengthRestriction(elem, min, max){
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max){
return true;
}else{
alert("Please enter between " min " and " max " characters");
elem.focus();
return false;
}
}

function madeSelection(elem, helperMsg){
if(elem.value == ""){
alert(helperMsg);
elem.focus();
return false;
}else{
return true;
}
}

function emailValidator(elem, helperMsg){
var emailExp = /^[\w\-\.\ ] \@[a-zA-Z0-9\.\-] \.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
</script>
</head>

<body>
<form method="post" action="save_entry.php" name="personalentry" onsubmit="return formValidator()">
<table border="0" cellpadding="0" cellspacing="0" align="center" width="250">
<tr>
<td colspan="3" align="center" bgcolor="#336699" height="50"><font face="verdana" size="2">Personal Data Sample Entry</font></td>
</tr>
<tr>
<td bgcolor="#0099FF"><font face="verdana" size="2">Lastname</font></td>
<td bgcolor="#0099FF"><input type="text" name="lastname" id="lastname"></td>
</tr>
<tr>
<td bgcolor="#336699"><font face="verdana" size="2">Firstname</font></td>
<td bgcolor="#336699"><input type="text" name="firstname" id="firstname"></td>
</tr>
<tr>
<td bgcolor="#0099FF"><font face="verdana" size="2">mi</font></td>
<td bgcolor="#0099FF"><input type="text" name="mi" id="mi" maxlength="2"></td>
</tr>
<tr>
<td bgcolor="#336699"><font face="verdana" size="2">age</font></td>
<td bgcolor="#336699"><input type="text" name="age" id="age" maxlength="2"></td>
</tr>
<tr>
<td><font face="verdana" size="2">sex</font></td>
<td bgcolor="#0099FF">
<select name="sex" id="sex">
<option value=""></option>
<option value="male"><font face="verdana" size="2">male</font></option>
<option value="female"><font face="verdana" size="2">female</font></option>
</select>
</td>
</tr>
<tr>
<td bgcolor="#336699"><font face="verdana" size="2">civil status</font></td>
<td bgcolor="#336699">
<select name="civilstatus" id="civilstatus">
<font face="verdana" size="2">
<option value=""></option>
<option value="single">single</option>
<option value="married">married</option>
<option value="married">Separated</option>
<option value="widow">widow</option>
</font>
</select>
</td>
</tr>
<tr>
<td bgcolor="#0099FF"><font face="verdana" size="2">religion</font></td>
<td bgcolor="#0099FF"><input type="text" name="religion" id="religion"></td>
</tr>
<tr>
<td bgcolor="#336699"><font face="verdana" size="2">Email</font></td>
<td bgcolor="#336699"><input type="text" name="email" id="email"></td>
</tr>
<tr>
<td colspan="3" align="center" bgcolor="#0099FF"><input type="submit" value="submit"><input type="reset" value="reset"></td>
</tr>
</form>

</body>
</html>

Tuesday, September 14, 2010

Dynamic Combo Box

Using php code, you can get data from mysql database then put the data inside your option value.
This can be useful sometimes if you need to put data base from mysql database, then put value to your combo box.



code to paste:

note: Start php code. see ex. <?php and end with?>
//*put code connecting to the database*//

echo "<tr>";
echo "<td align='right'><font face='verdana' size='2'>Location:</font></td>";
echo "<td>";


$result = mysql_query("SELECT * FROM location order by location");
echo "<select name='location'>";
echo "<option>----Select Location----</option>";
while($row = mysql_fetch_array($result))
{
echo $location = $row['location'];
echo "<option value='$location'>$location</option>";
}

echo "</select>";
echo "</tr>";

//*close database*//

Using Trim Function, create lower, capitalize the string of data

code to paste:

<?php

//* put php code to connect database* //

// We preform a bit of filtering
$search = strtoupper($search);
$search = strip_tags($search);
$search = trim ($search);

//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM personal WHERE upper($lastname) LIKE'%$search%'");

//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo $result['lastname'];
echo " ";
echo $result['firstname'];
echo "<br>";
echo $result['mi'];
echo "<br>";
echo "<br>";
echo $result['age'];
echo "<br>";
echo "<br>";
}
?>

PHP Menu II

- Using Trim Function, create lower, capitalize the string of data
- Dynamic Combo Box
- Complete Javascript Validation for your PHP
- Multiple File Upload & Data Submission with File Validation
- Display/Query data from myql database and create alternate background color using FOR LOOP
- Simple Photo Gallery
- PHP Complete Pagination, first, previous, next and last record/Photo Gallery pagination
- Pagination with alternate background color, complete pagination First, Last, Next & Previous using While Loop
- SQL display the highest number Function using MAX() Function
- PHP Basic computation with formatting number
- Formatting Date
- PHP readmore using strip-tags
- PHP FOR NEWS LETTER
- Max Function & sql display the highest number field data from mysql database



Search photo

After creating php script for image/photo upload, you can search photo base from info.

here is a sample code:

<?php


$con = mysql_connect("localhost","root","yourpassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("sample", $con);



$result = mysql_query("SELECT * FROM table1 where lastname='rafiel dilo'");
$count=mysql_num_rows($result);

if($count==1)
{
while($row = mysql_fetch_array($result))
{
echo $picture = '<img src="' . $row['photo'] . '">';
}
}
else
{
echo "record not found";
}

mysql_close($con);
?>

Single Upload Photo with basic data saved to mysql database

Create folder name: images inside htdocs before you can create upload photo.
First, all images file will be uploaded to the images folder.
Second, path info. will be save on the database mysql.

code to paste:


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<form method="post" action="savepicture1.php" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td>Lastname</td>
<td><input name="lastname" type="text"> </td>
</tr>
<tr>
<td>Firsntame</td>
<td><input name="firstname" type="text"> </td>
</tr>
<tr>
<td>Age</td>
<td><input name="age" type="text"> </td>
</tr>

<tr>
<td>Picture</td>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile" >
</td>
<td><input name="upload" type="submit" value=" Upload "></td>
</tr>
</table>
</form>
</body>
</html>


savepicture1.php

<?php
$con = mysql_connect("localhost","root","yourpassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("sample", $con);

$lastname=$_REQUEST['lastname'];

$result = mysql_query("SELECT * FROM table1 where lastname='$lastname'");
$count=mysql_num_rows($result);
if($count==1)
{
echo "<br><br><br><p align='center'><b>Record was already exist</b></p>";
}

else
{
echo "Upload: " . $_FILES["userfile"]["name"] . "<br />";
echo "Type: " . $_FILES["userfile"]["type"] . "<br />";
echo "Size: " . ($_FILES["userfile"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["userfile"]["tmp_name"] . "<br />";

move_uploaded_file($_FILES["userfile"]["tmp_name"],
"images/" . $_FILES["userfile"]["name"]);

echo "Stored in: " . "images/" . $_FILES["userfile"]["name"];

$path = "images/" . $_FILES["userfile"]["name"];

$sql="INSERT INTO table1 (lastname, firstname, age, photo)
VALUES
('$_POST[lastname]','$_POST[firstname]','$_POST[age]','$path')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
}

mysql_close($con);
?>

Single Upload Photo with basic data saved to mysql database

code to paste:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<form method="post" action="savepicture1.php" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td>Lastname</td>
<td><input name="lastname" type="text"> </td>
</tr>
<tr>
<td>Firsntame</td>
<td><input name="firstname" type="text"> </td>
</tr>
<tr>
<td>Age</td>
<td><input name="age" type="text"> </td>
</tr>

<tr>
<td>Picture</td>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile" >
</td>
<td><input name="upload" type="submit" value=" Upload "></td>
</tr>
</table>
</form>
</body>
</html>


savepicture1.php

<?php
$con = mysql_connect("localhost","root","htciton");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("sample", $con);

$lastname=$_REQUEST['lastname'];

$result = mysql_query("SELECT * FROM table1 where lastname='$lastname'");
$count=mysql_num_rows($result);
if($count==1)
{
echo "<br><br><br><p align='center'><b>Record was already exist</b></p>";
}

else
{
echo "Upload: " . $_FILES["userfile"]["name"] . "<br />";
echo "Type: " . $_FILES["userfile"]["type"] . "<br />";
echo "Size: " . ($_FILES["userfile"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["userfile"]["tmp_name"] . "<br />";

move_uploaded_file($_FILES["userfile"]["tmp_name"],
"images/" . $_FILES["userfile"]["name"]);

echo "Stored in: " . "images/" . $_FILES["userfile"]["name"];

$path = "images/" . $_FILES["userfile"]["name"];

$sql="INSERT INTO table1 (lastname, firstname, age, photo)
VALUES
('$_POST[lastname]','$_POST[firstname]','$_POST[age]','$path')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
}

mysql_close($con);
?>

Monday, September 13, 2010

PHP sub query

code to paste:

<?php

$con = mysql_connect("localhost","root","mypassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("sample", $con);

$result = mysql_query("SELECT * FROM subjectentry order by coursecode");

echo "<table border='1'>
<tr>
<th><b>Coursecode</b></th>
<th><b>Students</b></th>
</tr>";
while($row = mysql_fetch_array($result))

{
$shape = $row['coursecode'];
echo "<tr>";
echo "<td valign='top'><b>" . $row['coursecode'] . "</b></td>";

//Sub query type serves as your data shaping
$resultx = mysql_query("SELECT * FROM subjectlist where coursecode = '$shape' order by completename");
echo "<td>";
while($rowx = mysql_fetch_array($resultx))
{
echo "<p>";
echo $rowx['completename'] . " " . $rowx['address'];
echo "<p>";
}
echo "</td>";
//Sub query type like as your data shaping
echo "</tr>";
}
echo "</table>";

mysql_close($con)
?>


output:



another sample

code to paste:

<?php
$con = mysql_connect("localhost","root","htciton");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("students", $con);

$result = mysql_query("SELECT * FROM subjectentry order by coursecode");

echo "<table border='1'>
<tr>
<th><b>Coursecode</b></th>
<th><b>Students</b></th>
</tr>";
while($row = mysql_fetch_array($result))

{
$shape = $row['coursecode'];
echo "<tr>";
echo "<td valign='top'><b>" . $row['coursecode'] . "</b></td>";

//Sub query type serves as your data shaping
$resultx = mysql_query("SELECT * FROM subjectlist where coursecode = '$shape' order by completename");
echo "<td>";
echo '<table border="1">';
while($rowx = mysql_fetch_array($resultx))
{
echo '<tr>';
echo "<td>";
echo $rowx['completename'] . " " . $rowx['address'];
echo "<td>";
echo '</tr>';
}
echo '</table>';
echo "</td>";
//Sub query type serves as your data shaping

echo "</tr>";
}
echo "</table>";

mysql_close($con)
?>

output:



php sample display data from mysql database using html table to your php file

code to paste:

<html>
<body>
<table border="1" align="center">

<form action="query.php" method="post">
<tr>
<td>Marina Number:</td>
<td><input type="text" name="marinanumber" maxlength="7" /></td>
</tr>

<tr>
<td><input type="submit" value="search data"/></td>
<td>&nbsp;</td>
</tr>

</form>

</table>
<br>
<br>
<br>


<?php

$con = mysql_connect("localhost","root","mypassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("personalinfo", $con);

$search=$_POST['idnumber'];


$result = mysql_query("SELECT * FROM marinax where idnumber='$search'");
$count=mysql_num_rows($result);

if($count==1)
{
while($row = mysql_fetch_array($result))
{
echo "<p align='center'>Record found for ID Number:" . $row['idnumber'] . "</p>";
echo "<table border='1' align='center'>";
echo "<tr>";
echo "<td>Name:</td>";
echo "<td>" . $row['name'] . "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>Adress:</td>";
echo "<td>" . $row['address'] . "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>City:</td>";
echo "<td>" . $row['city'] . "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>State:</td>";
echo "<td>" . $row['state'] . "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>ZipCode</td>";
echo "<td>" . $row['zipcode'] . "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>Warranty:</td>";
echo "<td>" . $row['religion'] . "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>Non-warranty</td>";
echo "<td>" . $row['address'] . "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>Tech Number:</td>";
echo "<td>" . $row['petsname'] . "</td>";
echo "</tr>";
echo "</table>";
}

}
else
{
echo "record not found";
}


mysql_close($con)
?>

Friday, September 10, 2010

menu

FOR SAVING DATA PHP SCRIPT
- Basic Validation form Data Entry
- Sample php code to save data to mysql database from html form
- Saving record to mysql database with confirmation
- Saving more than one record entry with confirmation & saving data on a multiple table




QUERY
- Sample simple query
- Sample search data using variable
- Sample to display data from mysql database using html table to your php file.
- Display data from MySql database by confirmation then display data on
HTML table.
- PHP sub queries, like data shaping
- DATA SHAPING WITH USING HTML TABLE
- Display Image from MySql using image field blob data type
- Single Upload Photo with basic personal data
- Search photo


DELETIONS
-Delete Record Entry
-Delete Record Entry

Table Join Missing: refer to the website:///////////////////////


PAGING
-PHP PAGING
-Paging Record shortened code

Sample search data using variable

code to paste:

formsearch.html

<html>
<body>
<table border="1" align="center">

<form action="formsearch.php" method="post">
<tr>
<td>Marina Number:</td>
<td><input type="text" name="lastname" maxlength="7" /></td>
</tr>

<tr>
<td><input type="submit" value="search data"/></td>
<td>&nbsp;</td>
</tr>

</form>
</table>
</body>
</html>


formsearch.php

$con = mysql_connect("localhost","root","yourpassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("sample", $con);

$search=$_POST['lastname'];

$result = mysql_query("SELECT * FROM table1 where lastname='$search'");
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1)
{
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "record not found";
}

mysql_close($con);
?>


Sample simple php query

code to paste:

<html>
<body>
<?php
$con = mysql_connect("localhost","root","yourpassword");

$result = mysql_query("SELECT * FROM person
WHERE FirstName='Peter'");

while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}

mysql_close($con)
?>
</body>
</html>


Code to display data using html table

code to paste:


<?php
$con = mysql_connect("localhost","root","yourpassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM person");

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>

Thursday, September 9, 2010

Saving more than one record entry with confirmation & saving data on a multiple table

Saving more than one record entry with confirmation & saving data on a multiple table
The purpose of this code is to save record on studentinfo and studentlist table in your MySql database in one click of your add entry.

code to paste:

<?php
$con = mysql_connect("localhost","root","yourpassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("sample", $con);

$search=$_POST['studentno'];

$result = mysql_query("SELECT * FROM studentinfo where studentno='$search'");
$count=mysql_num_rows($result);
if ($count==1)
{

echo "student number already exist";

}
else
{
$sql="INSERT INTO studentinfo (studentno, lastname, firstname, mi, age, sex, status, address, schoolyear, semmester)
VALUES
('$_POST[studentno]','$_POST[lastname]','$_POST[firstname]','$_POST[mi]','$_POST[age]','$_POST[sex]','$_POST[status]','$_POST[address]','
$_POST[schoolyear]','$_POST[semmester]')";

$result = mysql_query( $sql,$con );

$sql="INSERT INTO studentlist (studentno, lastname, firstname, address)
VALUES
('$_POST[studentno]','$_POST[lastname]','$_POST[firstname]','$_POST[address]')";

$result = mysql_query( $sql,$con );

//Note: you can use = $result = mysql_query( $sql,$con ); see above
////////////////////////////////////////////
//Note: //Note: This can be omitted for your saving confirmation standard
////////////////////////////////////////////
//if (!mysql_query($sql,$con))
//{
//die('Error: ' . mysql_error());
//}

echo "1 record added";
}

mysql_close($con)
?>

Saving record/data with confirmation

Confirmation if the specific record was already existed in the mysql database the php code will not continue to save data. On another side, if specific data was not yet inside the database the program will execute to save a data.

code to paste:

<html>
<body>
<?php
$con = mysql_connect("localhost","root","yourpassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("personalinfo", $con);

$search=$_POST['idno'];


$result = mysql_query("SELECT * FROM marina where idno='$search'");
$count=mysql_num_rows($result);

if($count==1)
{
echo "marina number already exist";
}
else
{
$sql="INSERT INTO table1 (idno, name, address, city, state, zipcode)
VALUES
('$_POST[idno]','$_POST[name]','$_POST[address]','$_POST[city]','$_POST['state']','$_POST[zipcode]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

}
mysql_close($con)
?>
</body>
</html>

Sample php code to save data to mysql database from html form

Save data from html form to MySql Database.
Again, here is a sample html form for data entry and php script.
Note: You can combine javascript validation and php code on saving data.

code to paste:

form_entry.html

<html>
<body>
<table border="1" align="center">

<form action="save.php" method="post">
<tr>
<td>Lastname:</td>
<td><input type="text" name="lastname" /></td>
</tr>

<tr>
<td>Firsntname:</td>
<td><input type="text" name="firstname" /></td>
</tr>

<tr>
<td>Age:</td>
<td><input type="text" name="age" /></td>
</tr>

<tr>
<td><input type="submit" /></td>
<td>&nbsp;</td>
</tr>

</form>
</table>

</body>
</html>


save.php
<html>
<body>
<?php
$con = mysql_connect("localhost","root","mypassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("sample", $con);

$sql="INSERT INTO table1 (lastname, firstname, age)
VALUES
('$_POST[lastname]','$_POST[firstname]','$_POST[age]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

mysql_close($con)
?>
</body>
</html>

Wednesday, September 8, 2010

Basic Validation form Data Entry

Before you will decide to save data from html form to MySql database. Much better if you will put first a validation control.

Here is a sample Javascript code for validation.
Note: You can combine javascript validation and php code on saving data.

code to paste:
form_entry.html
<html>
<head>
<script language="javascript">
function validateform()
{
formObj = document.personalinfo;
if (formObj.studentno.value=="")
{
alert("Fill for Student Number");
formObj.studentno.focus();
return false;
}

else if (formObj.lastname.value=="")
{
alert("Fill for Lastname");
formObj.lastname.focus();
return false;
}

else if (formObj.firstname.value=="")
{
alert("Fill for Firstname");
formObj.firstname.focus();
return false;
}

else if (formObj.address.value=="")
{
alert("Fill for Address");
formObj.address.focus();
return false;
}
}
</script>

</head>

<body>
<table border="1" align="center">

<form action="save.php" method="post" name="personalinfo" onSubmit="return validateform()" >


<tr>
<td>Student Number:</td>
<td><input type="text" name="studentno" /></td>
</tr>

<tr>
<td>Lastname:</td>
<td><input type="text" name="lastname" /></td>
</tr>

<tr>
<td>Firstname:</td>
<td><input type="text" name="firstname" /></td>
</tr>

<tr>
<td>Address:</td>
<td><input type="text" name="address" /></td>
</tr>

<tr>
<td><input type="submit" /></td>
<td>&nbsp;</td>
</tr>

</form>
</table>

</body>
</html>

Then after clicking submit button, you can add php code to POST or REQUEST data on the save.php file.
It's up to you if you will only display the data, or save the data to MySql Database.

PHP Guide Menu I

FOR SAVING DATA PHP SCRIPT
- Basic Validation form Data Entry
- Sample php code to save data to mysql database from html form
- Saving record to mysql database with confirmation
- Saving more than one record entry with confirmation & saving data on a multiple table




QUERY
-Sample simple query
-Sample search data using variable
-PHP sample display data from mysql database using html table to your php file
-Single upload with basic data saved to mysql database
-Search Photo
-PHP sub query

DELETIONS
- PHP Delete record entry