Category: Web Development

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

Enabling remember me on signin page using php cookies

In this application, we use PHP cookes to enable “remember me” option <?php session_start(); ?> <html> <head> <?php //print_r($_COOKIE); /* if(strlen($_COOKIE['userid'])!=0) { $_SESSION['isloggedin'] = true; echo "<script language='javascript'>window.location.href='default.php';</script>"; } */ $userid = isset($_COOKIE['userid'])?$_COOKIE['userid']:""; $passwd = isset($_COOKIE['passwd'])?$_COOKIE['passwd']:""; if (isset($_POST['btnsubmit']) ) { //setcookie("count",500); $username = $_POST['txtname']; $password = $_POST['txtpassword']; $remember = isset($_POST['chkremember'])?$_POST['chkremember']:""; if($username == "ali" && $password

arrays in php

In this example, arrays are declared in PHP using name/value pairs and without name value pairs <html> <head> </head> <body> <?php $array = array( "KHI" => "Karachi", "LHR" => "Lahore", "ISL" => "Islamabad"); $array1 = array("pakistan", "usa", "england"); //var_dump($array); //print_r($array); foreach($array as $key => $value) { echo $key . " " . $value . "<br>";

A simple class in PHP

In this example, a simple class is made in PHP. The class object is created, using which various functions of the class are called <html> <head> </head> <body> <?php class student { var $foo; var $bar; var $ans; function student() { $this -> foo = "foo"; $this -> bar = "bar1"; } function display() {

A timer function in PHP using javascript

In the following example, a timer function is designed. The user need to provide input of timer function, as the timer proceeds, the color of progress bar is changing. <html> <head> <title></title> <script language="javascript" type="text/javascript"> var id = null; function myStartFunction(totalTime) { var txtbox = document.getElementById("txtbox"); var mytable = document.getElementById("mytable"); var width = 0; id

How to prevent SQL injection attacks in PHP

To prevent SQL injection attacks or javascript attacks in your PHP web application, always use parameterized query with prepared statements like: $txtuserid = htmlspecialchars(addslashes(trim($_POST['txtid']))); $query = "select * from users where userid=:userid" try { $stmt = $conn->prepare($query); $stmt->bindParam(':userid', $txtuserid); //$txtuserid getting value through $_POST or URL string $stmt->execute(); echo "Record inserted successfully..."; } catch(PDOException $e)

difference between calling objects of select, listbox, radiobutton array, and checkbox in javascript

What is the difference between calling objects of Select, Listbox, Radiobutton array, and checkboxes array in javascript ============================ If you have Select, it will be accessed in javascript like this: var country = document.getElementById(“ddlcountry”);… if( country.selectedIndex == 0) { alert(“Country must be selected”); return false; } To retrieve values and text of select, we use

a javascript program to compute grade from a range of marks

In the following program, the user inputs his/her marks in a text box. The GPA is computed and shown in other box index.html <!DOCTYPE html> <html> <head> <title>Untitled document</title> <meta charset="utf-8" /> <script language="javascript" type="text/javascript"> function calculategpa() { var marks = document.getElementById("txtmarks").value; var gpa = document.getElementById("txtgpa"); if(marks >= 50 && marks <= 60) { gpa.value