Category: PHP

uploading single file in php

To upload a single file, the following code is useful. NOTE: you just need to fix the path of $uploaddir <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php if( isset($_POST['btnsubmit']) ) { //print_r($_FILES); $uploaddir = 'A:/AAA/www/web/uploads/'; $uploadfile = $uploaddir . basename($_FILES['userfile']['name']); if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully

nested if else in php

Nested if else in php has slightly different syntax from c++. Here is an example: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php $a=10; $b=20; if($a > $b) { echo "a is greater than b"; } elseif($a == $b) { echo "a is equal to b"; } else { echo "a is

goto statement in php

This is how we can use goto statement php: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php goto a; echo 'Hello'; a: echo 'World'; ?> </body> </html>  

loops and break statement in php

In this example, we see how we can use loops and break statements in php <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php $num=10; while($num >= 0) { $num--; for($i=0; $i<10; $i++) { echo "loop i = " . $i . " <br>" ; if($num == 5) { echo "<br>Loop is break<br>";

object oriented Interfaces in PHP

Like other object oriented programming languages, we can define Interfaces in php as well. Here is the code to define and use interfaces in php <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php interface MyInterface { } class MyClass implements MyInterface { } $a = new MyClass; $b = new MyClass; $c

object oriented Inheritance in php

We can use Objected Oriented Programming model in PHP which has rich set of features. Here an example of inheritance in PHP is presented: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php class ParentClass { function ShowInParent() { echo "This is in Parent Class<br>"; } } class MyClass extends ParentClass { function

Creating a dynamic html table in php

With PHP we can dynamically create HTML table and fetch record from database to show into that table. In the following form, user is asked to input to numbers. index.php <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <form id="frm" name="frm" method="post" action="table.php" > <table border="1"> <tr> <td>Enter Number</td> <td> <input id="txtnum" name="txtnum"

update multi-value attribute record in php

Sometimes, we need to update a  record in php that has multi-value attributes. At database end, the multi-value attributes are stored in a separate table, with the primary of the record against whose the multi-value attributes are stored. In php, to update multi-value attributes, we need to do some extra work. Suppose we have file:

update record in php using parameterized query

In this example, we update record in php. The database table whose record are updated has following details tblstudent (id, name, age, address), where id is an auto-increment primary key Suppose we want to update record against id = 1 The following is the code. The code of opendb.php is defined here <html> <head> </head>

A database connectivity code using php

In this example, we create a PHP file to connect with database. The same file will be used and should be referred in the rest of the applications: opendb.php <?php $servername = "localhost"; $username = "root"; $password = ""; // set this field "" (empty quotes) if you have not set any password in mysql