PHP -1

Samantha Mathara Arachchi,
B.Sc,Pg.Dip(Com.Tech.),Pg.Dip.(IM),M.Sc.(IM),MCS(SL) MACM,MIEEE

e-mail:(ssp@ucsc.cmb.ac.lk)
Advanced Digital Media Technology Center-University of Colombo School of Computing

PHP basic syntax
How to write PHP in HTML Begin with <?php , and end with ?>
<?php ........PHP code........... ?>

IMPORTANT: PHP code is case-sensitive (like JavaScript) but HTML is not case-sensitive.

Where to insert PHP script in HTML?

Basic structures

Multiple branch

Loop

Function and Comments

Operators

Basic Statement and Function

Function…

Arrays

2

Associative array

Foreach Loop

Modifications

Data Input form (Example –Create Database)
Create database and table (columns details)

Table Structure

Data Input form (Example –Browser View)

Table Structure

Data Input form (Example –Coding… php)
<?php $name=$_POST["st_name"]; $tel=$_POST["st_tel"]; $id=$_POST["st_id"]; $course=$_POST["st_course"]; Variables $sex=$_POST["st_sex"]; $hobbies=$_POST["st_hobbies"]; $linkz =mysql_connect("localhost","root","") or die("Sorry Didnot"); //Connecting mysql_select_db("aa") or die("No_DB_Found"); //Selecting Database $sql = "INSERT INTO student1 (st_name,st_tel,st_id,st_course,st_sex,st_hobbies) VALUES ('$name','$tel','$id','$course','$sex','$hobbies[0],$hobbies[1],$hobbies[2],$hobbies[3] ')"; mysql_query($sql, $linkz); //Passing sql echo "Student ".$name." was entered."; //Confirm message echo "<hr>"; mysql_close($linkz); Table Structure ?>

Data Input form (Example –Coding… HTML)
<html> <body> <h1 align=center>Student Details </h1> <form action="" method=post> <table border=1> <tr><td>Name </td> <td><input type=txt name=st_name></td> </tr> <tr><td>Telephone</td><td><input type=txt name=st_tel></td></tr> <tr><td>Student ID</td><td><input type=txt name=st_id></td></tr> <tr><td>Course</td> <td> <select name=st_course> <option selected>Java</option> <option selected>Visual Basic</option> <option selected>Databasse</option> <option selected>Software Engineering</option> </select> </td> </tr>

Data Input form (Example –Coding… HTML)
<tr>

<td>Sex</td> <td> <input name=st_sex type="radio" value="Male">Male <input name=st_sex type="radio" value="Female">Female</td> </tr> <tr> <td>Hobbies</td> <td><input name=st_hobbies[] type="checkbox" value="swimming" check> Swimming <input name=st_hobbies[] type="checkbox" value="Cricket" check>cricket <input name=st_hobbies[] type="checkbox" value="Rubby" check>Rubby <input name=st_hobbies[] type="checkbox" value="Football" check>Football </td> </tr> </table> <br> <input type=submit> </form> <?php echo "<hr><font size=5>"; echo "</font><br>Add Other Functions here..."; ?> </body> </html>

Data Output form (Example –Coding… php)

<?php $linkz=mysql_connect ("localhost","root","") or die("Sorry Didnot"); //Connecting mysql_select_db("aa",$linkz) or die("No_DB_Found"); //Selecting Database $sql="select * from student1"; //sql to count columns / rows $result=mysql_query($sql,$linkz); //Passing sql

The $_POST Variable
The predefined $_POST variable is used to collect values from a form sent with method="post". Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send. Note: However, there is an 8 Mb max size for the POST method, by default (can be changed by setting the post_max_size in the php.ini file).

Data Output form (Example –Coding… php)

echo "<table border=1>"; $numofrows=mysql_num_rows($result); //echo $numofrows; for($fld=0;$fld<mysql_num_fields($result);$fld++){ echo "<th>"; echo "&nbsp;&nbsp;&nbsp;".mysql_field_name($result,$fld); echo "</th>"; } while ($row = mysql_fetch_assoc($result)) { echo "<tr>"; echo "<td>".$row["ida"]."</td> <td>".$row["st_name"]."</td> <td>".$row["st_id"]."</td> <td>".$row["st_course"]."</td> <td>".$row["st_sex"]."</td> <td>".$row["st_tel"]."</td><td>".$row["st_hobbies"]."</td>"; echo "</tr>"; } echo "</table>"; ?>

echo()
Outputs all parameters. echo() is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo() (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo(), the parameters must not be enclosed within parentheses.

echo "<table border=1>";

mysql_num_rows($result);-….

mysql_num_fields() Function
The mysql_num_fields() function returns the number of fields in a recordset. This function returns FALSE on failure.

for($fld=0;$fld<mysql_num_fields($result);$fld++){ : : }

mysql_field_name() Function
The mysql_field_name() function returns the name of a field in a recordset. Returns the field name on success, or FALSE on failure.

echo "&nbsp;&nbsp;&nbsp;".mysql_field_name($result,$fld);

mysql_fetch_assoc() Function
The mysql_fetch_assoc() function returns a row from a recordset as an associative array. This function gets a row from the mysql_query() function and returns an array on success, or FALSE on failure or when there are no more rows.

while ($row = mysql_fetch_assoc($result)) { echo "<tr>"; echo "<td>".$row["ida"]."</td> <td>".$row["st_name"]."</td> <td>".$row["st_id"]."</td> <td>".$row["st_course"]."</td> <td>".$row["st_sex"]."</td> <td>".$row["st_tel"]."</td><td>".$row["st_hobbies"]."</td>"; echo "</tr>";

Delete
if ($_POST["submit"] == "YES") { 63 $sql="DELETE FROM participants WHERE".stripslashes($_POST["where"]); 64 show_MySQL_table("localhost", "yoichi", "", "test", $sql);

Update
if ($is_update) { $sql = "UPDATE participants2 SET participant_name='$pt_nm', province_id=$pv_id WHERE pt_id=" . $_GET["id"]; $success = "The record has been updated."; }