Tuesday, February 9, 2010

Title Page of summer project


Untitled Document

A Report On Financial Analysis

of
EVEREST BANK LIMITED

Kathmandu

By:
Rupak Nepali

PU Reg. No.:- 2006-2-03-***

Exam Roll No.:- 734***


A Summer Project Report
Submitted to:
Nobel College

Faculty of Management

Pokhara University

In partial fulfillment of the requirements for the degree of
Bachelor of Computer Information System

Kathmandu, Nepal

2009


Search code part 3

<html>
<head>
<title>Search Test</title>
</head>
<body topmargin="0" leftmargin="0">
<form action="search.php" method="post">
Search Term <input type="text" name="searchterm"><br />
<input type="submit" value="Search">
</form>
</body>
</html>





<?php
/*set varibles from form */
$searchterm = $_POST['searchterm'];
trim ($searchterm);
/*check if search term was entered*/
if (!$searchterm){
echo 'Please enter a search term.';
}
/*add slashes to search term*/
if (!get_magic_quotes_gpc())
{
$searchterm = addslashes($searchterm);
}

/* connects to database */
@ $dbconn = new mysqli('host', 'username', 'password', 'database');
if (mysqli_connect_errno())
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
/*query the database*/
$query = "select * from tablename where tablerow like '%".$searchterm."%'";
$result = $dbconn->query($query);
/*number of rows found*/
$num_results = $result->num_rows;

echo '<p>Found: '.$num_results.'</p>';
/*loops through results*/
for ($i=0; $i <$num_results; $i++)
{
$num_found = $i + 1;
$row = $result->fetch_assoc();
echo "$num_found. ".($row['tablerow'])." <br />";
}
/*free database*/
$result->free();
$dbconn->close();
?>

Search code part 2

this code source...

<?php
/*set varibles from form */
$searchterm = $_POST['searchterm'];
Trim ($searchterm);
/*check if search term was entered*/
If (!$searchterm){
echo 'Please enter a search term.';
}
/*add slashes to search term*/
If (!get_magic_quotes_gpc())
{
$searchterm = addslashes($searchterm);
}

/* connects to database */
@ $dbconn = new mysqli('localhost', 'root', '', 'sample1');
If (mysqli_connect_errno())
{
Echo 'Error: Could not connect to database. Please try again later.';
Exit;
}
/*query the database*/
$query = "select * from sheet1 where IndexNo like '%".$searchterm."%'";
$result = $dbconn->query($query);
/*number of rows found*/
$num_results = $result->num_rows;

echo '<p>Found: '.$num_results.'</p>';
/*loops through results*/
For ($I=0; $I <$num_results; $I++)
{
$num_found = $I + 1;
$row = $result->fetch_assoc();
Echo "$num_found. ".($row['IndexNo'])." <br />";
Echo "<table border='1'>
<tr>
<th>IndexNo</th>
<th>Name</th>
<th>Marks</th>
<th>Rank</th>
</tr>";

echo "<tr>";
echo "<td>" . $row['IndexNo'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Marks'] . "</td>";
echo "<td>" . $row['Rank'] . "</td>";
Echo "</tr>";
}
Echo "</table>";
/*free database*/
$result->free();
$dbconn->close();
?>

Search code part 1

1st part

CREATE TABLE users (fname VARCHAR(30), lname VARCHAR(30), info BLOB);

INSERT INTO users VALUES ( "Jim", "Jones", "In his spare time Jim enjoys biking, eating pizza, and classical music" ), ( "Peggy", "Smith", "Peggy is a water sports enthusiast who also enjoys making soap and selling cheese" ),( "Maggie", "Martin", "Maggie loves to cook itallian food including spagetti and pizza" ),( "Tex", "Moncom", "Tex is the owner and operator of The Pizza Palace, a local hang out joint" )


2nd part

<h2>Search</h2>
<form name="search" method="post" action="<?=$PHP_SELF?>">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="fname">First Name</option>
<Option VALUE="lname">Last Name</option>
<Option VALUE="info">Profile</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>


3rd part

<?
//This is only displayed if they have submitted the form
if ($searching =="yes")
{
echo "<h2>Results</h2><p>";

//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}

// Otherwise we connect to our Database
mysql_connect("mysql.yourhost.com", "user_name", "password") or die(mysql_error());
mysql_select_db("database_name") or die(mysql_error());

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

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

//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo $result['fname'];
echo " ";
echo $result['lname'];
echo "<br>";
echo $result['info'];
echo "<br>";
echo "<br>";
}

//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}

//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>





4th part

Breaking the PHP Code Down - Part 1

if ($searching =="yes")

In our original HTML form, we had a hidden field that sets this variable to "yes" when submitted. This line checks for that. If the form has been submitted then it runs the PHP code, if not it just ignores the rest of the coding.

if ($find == "")

The next thing we check before we run the query is that they actually entered a search string. If they haven't, we prompt them to, and don't process any more of the code. If we didn't have this code, and they entered a blank result, it would simply return the entire database's contents.

After this check we connect to our database, but before we can search we need to filter.

$find = strtoupper($find)

This changes all of the characters of the search string to UPPER case. We will explain how this is useful later.

$find = strip_tags($find)

This takes out any code they may have tried to enter in to the search box.

$find = trim ($find)

And this takes out all the whitespace - for example if they accidently put a few spaces at the end of their query.




5th part


Breaking the PHP Code Down - Part 2

$data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'")

This code actually does the searching. We are choosing all the data from our table WHERE the the field they choose is LIKE their search string. We use upper () here to search the uppercase version of the fields. Earlier we converted our search term to uppercase as well. These two things together basically ignore case. Without this a search for "pizza" would not return a profile that had the word "Pizza" with a capitol P. We also use the '%' percentage on either side of our $find variable to indicate that we are not looking solely for that term but rather that term possibly contained in a body of text.

while($result = mysql_fetch_array( $data ))

This line and the lines below it start a loop that will cycle through and return all the data. We then choose what information to ECHO back to our user, and in what format.

$anymatches=mysql_num_rows($data);
if ($anymatches == 0)

This code counts the number of rows of results. If the number is 0, it means that no results were found. If this is the case, we let the user know that.

$anymatches=mysql_num_rows($data)

Finally, incase they forgot, we remind them of what they searched for.

Monday, February 8, 2010

Edit the row from the database

<?php

if(isset($_GET['user_id']))
{
$user_id=$_GET['user_id'];
}
else if(isset($_POST['user_id']))
{
$user_id=$_POST['user_id'];
}
else
{
$user_id=0;
}


if($user_id!=0)
{

//echo "$name $address $user_name $pass";
if (mysql_connect("localhost","root",""))
{

//echo "connected";
mysql_select_db("php_classes");



if(isset($_POST['button']))
{


$name=$_POST['name'];

$address=$_POST['address'];

$user_name=$_POST['user_name'];




$query1="select user_id from profile where user_id=$user_id";

$result1=mysql_query($query1);
if(mysql_num_rows($result1)==0)
{
echo "This username is not available";
}
else
{


$query="update profile set name='$name',address='$address',user_name='$user_name' where user_id=$user_id limit 1";
//echo $query;

$result=mysql_query($query);

if (mysql_affected_rows()==1)
{
echo "Data has been updated";
}
else
{
echo "Data has been not been updated";
}
}








}//end of submit



$query11="select * from profile where user_id=$user_id";
//echo $query11;
$result11=mysql_query($query11);
$rows=mysql_fetch_array($result11,MYSQL_ASSOC);



?>





<form id="form1" name="form1" method="post" action="edit.php">
<p>Name
<label>
<input type="text" name="name" id="name" value="<?php echo $rows['name']; ?>" />
</label>
</p>
<p>Address
<input type="text" name="address" id="address" value="<?php echo $rows['address']; ?>" />
</p>
<p>Username
<input type="text" name="user_name" id="user_name" value="<?php echo $rows['user_name']; ?>"/>
</p>
<input type="hidden" name="user_id" value="<?php echo $user_id; ?>" />
<p>
<input type="submit" name="button" id="button" value="Submit" />
</p>
</form>


<?php
/*
mysql_connect()//connects
mysql_select_db()//
mysql_query()//passes query to mysql from php/
//CUD-Create-Unpdate-Delete---
//R-Read
mysql_affected_rows
mysql_num_rows
mysql_fetch_array
*/
}
else
{
echo "Could not connect to database";
}


}
else
{
echo "No user id Found";
}

?>
Edit the row from the database

list the users in the database

<?php


if (mysql_connect("localhost","root",""))
{

mysql_select_db("php_classes");

$query="select * from profile";
$result=mysql_query($query);

if (mysql_num_rows($result)>0)
{

?>
<table width="100%" border="1" cellspacing="0" cellpadding="0">

<tr>
<td>Name</td>
<td>Address</td>
<td>Username</td>
<td>Added</td>
<td>Delete</td>
<td>Edit</td>
</tr>

<?php
while ($rows=mysql_fetch_array($result,MYSQL_ASSOC))
{
?>
<tr>
<td><?php echo "{$rows['name']}"; ?></td>
<td><?php echo "{$rows['address']}"; ?></td>
<td><?php echo "{$rows['user_name']}"; ?></td>
<td><?php echo "{$rows['added_on']}"; ?></td>
<td><a href="delete_user.php?user_id=<?php echo "{$rows['user_id']}"; ?>">Delete</a></td>
<td><a href="edit.php?user_id=<?php echo "{$rows['user_id']}"; ?>">Edit</a></td>
</tr>

<?php

}//end of while
?>
</table>
<?php

}
else
{
echo "No Records found";
}



}
else
{
echo "Could not connect to database";
}



?>

list the users in the database

insert in the database using the php code

<?php

if(isset($_POST['button']))
{


$name=$_POST['name'];

$address=$_POST['address'];

$user_name=$_POST['user_name'];

$pass=$_POST['pass'];

//echo "$name $address $user_name $pass";
if (mysql_connect("localhost","root",""))
{

//echo "connected";
mysql_select_db("php_classes");

$query1="select user_id from profile where user_name=\"$user_name\"";

$result1=mysql_query($query1);
if(mysql_num_rows($result1)>0)
{
echo "This username is not available";
}
else
{


$query="insert into profile(name,address,user_name,pass,added_on) values ('$name','$address','$user_name',MD5('$pass'),NOW())";
//echo $query;

$result=mysql_query($query);

if (mysql_affected_rows()==1)
{
echo "You Are registered";
}
else
{
echo "You Are NOT registered";
}
}



}
else
{
echo "Could not connect to database";
}




}//end of submit






?>





<form id="form1" name="form1" method="post" action="register.php">
<p>Name
<label>
<input type="text" name="name" id="name" />
</label>
</p>
<p>Address
<input type="text" name="address" id="address" />
</p>
<p>Username
<input type="text" name="user_name" id="user_name" />
</p>
<p>Password
<input type="password" name="pass" id="pass" />
</p>
<p>
<input type="submit" name="button" id="button" value="Submit" />
</p>
</form>


<?php
/*
mysql_connect()//connects
mysql_select_db()//
mysql_query()//passes query to mysql from php/
//CUD-Create-Unpdate-Delete---
//R-Read
mysql_affected_rows
mysql_num_rows
mysql_fetch_array
*/


?>
insert in the database, insert in the mysql database, insert in the php and mysql database, insert in the database using the php code

delete user from the database

<?php
$user_id=$_GET['user_id'];

if(isset($_GET['submit']))
{

if (mysql_connect("localhost","root",""))
{
//echo "connected";
mysql_select_db("php_classes");

$confirm=$_GET['confirm'];

if ($confirm==1)
{


$query="delete from profile where user_id=$user_id limit 1";
//echo $query;

$result=mysql_query($query);

if (mysql_affected_rows()==1)
{
echo "User of ID $user_id has been deleted ";
}
else
{
echo "User of ID $user_id has NOT been deleted";
}

}
else
{
echo "The action was not done";
}


}
else
{
echo "Could not connect to database";
}



}




?>

<form method="get" action="delete_user.php">
Deleting user <?php echo $user_id; ?>
<input type="hidden" name="user_id" value="<?php echo $user_id; ?>" /><br/>
<input type="radio" name="confirm" value="0" checked="checked"/>No   <input type="radio" name="confirm" value="1"/>Yes<br/>

<input type="submit" name="submit" value="Do Action"/>
</form>

delete user from the database, delete with the confirm from the database, delete and confirm in the same page

learn about tables rowspan and colspan

</head>

<body>

<table border="0" cellpadding="2" cellspacing="2" width="100%">


<tr>
<td width="33%">1</td>
<td colspan="2">

</td>
</tr>

<tr>

<td rowspan="2">2</td>
<td>1</td>
<td>1</td>
</tr>

<tr>
<td>1</td>
<td>2</td>

</tr>


</table>


</body>
</html>
learn about tables rowspan and colspan, example of rowspan, example of colspan, example of table in the html, example of table in php

Uses of stripslashes addslashes

<?php
echo "This is printed doc";
//printf("This is another print");
$user="Ram";
echo "This is $user<br/>";
echo 'This is $user<br/>';
$user='ram"s';
$user=addslashes($user);
echo "$user<br/>";
$user=stripslashes($user);
echo "$user<br/>";
//$query="select * from table where username =$user";
//echo $query;


$str = "A 'quote' is <b>bold</b>";

// Outputs: A 'quote' is <b>bold</b>
echo htmlentities($str, ENT_QUOTES);


$test= htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $test;

echo md5("test");


echo nl2br("foo isn't\n bar");
$pass="ram1";
if($pass=="ram")
{
echo "Authorized";
}


echo "<br/>";

$text = '<p>Test paragraph.</p><!-- Comment --> <a href=\"#fragment\">Other text</a>';
echo strip_tags($text);

echo strlen("rupak");

echo "<br/>";

$text = '<p>Test paragraph.</p><!-- Comment --> <a href=\"#fragment\">Other text</a>';
echo str_replace("p","b",$text);

echo "<br/>";

$text1="Returns a string with backslashes in front of predefined charactersReturns a string with backslashes in front of predefined charactersReturns a string with backslashes in front of predefined charactersReturns a string with backslashes in front of predefined charactersReturns a string with backslashes in front of predefined charactersReturns a string with backslashes in front of predefined characters";
echo substr($text1,0,50);
echo "<br/>";
echo strtolower("TEXT");

echo "<br/>";
echo strtoupper("text");
echo "<br/>";

$result= strpos('abcde','g');
if($result!==FALSE)
{
echo "found";
}
else
{
echo "Not found";
}

echo "<br/>";

//
$email = 'name@example.com';
$domain = strstr($email, '@');
echo $domain; // prints @example.com



?>
Uses of stripslashes addslashes, example of stripslashes, example of addslashes, example of substr, example of strtoupper, example of strstr, example of strpos, example of strlen, example of str_replace, example of strip_tags, example of htmlspecials, example of md5