PHP Notes By Kshitij :: PART 9

 

 

Database connectivity with form

 

mysql_connect(host, user, password)

It accepts hostname, username, password and returns true if connection successfully otherwise returns empty string.

 

mysql_select_db(“databasename”)

It opens database. If opens successfully returns true otherwise empty string.

 

mysql_query(“query”)

it executes Query. It returns true if executes DML query It returns result set in case of Select Query. It returns false if find any error in query.

 

mysql_affected_rows()

It returns how many rows are inserted or updated or deleted.

 

mysql_insert_id()

It returns newly generated id

 

After firing selected query, we can fetch records by following ways:

 

mysql_num_rows($recordSet)

It returns total records fetched by Query.

 

mysql_fetch_row($recordSet)

It returns one row from record set and move cursor to next row.

Here we can access each field by index number (<?= $row[0] ?>) and index starts with 0. If don’t find any records or reached at EOF (End of File) returns null or false.

 

mysql_fetch_assoc($recordSet)

It returns a record and move cursor to next record.

Here we can access each field by fieldname (<?= $row[“id”] ?>). If don’t find any records or reached at EOR (End of Record) returns null or false.

 

mysql_fetch_array($recordSet)

It returns a record and move the cursor to the next record.

Here we can access each field by fieldname (<?= $row[“id”] ?>) or index number        (<?= $row[0] ?>). If don’t find any records or reached at EOR (End of Record) returns null or false.

 

mysql_fetch_object($recordSet)

It returns a record and move the cursor to the next record.

Here we can access each field by fieldname. If don’t find any records or reached at EOR (End of Record) returns null or false. We have to use pointer sign (->) for displaying records from array like <?= $row->id ?>

 

mysql_error()

This function returns error message if mysql has an error otherwise false. blank or 0 means false.

 

die(“String”)

It prints the message and stops the program execution.

 

<?php

error_reporting(0);

$connection=mysql_connect(“localhost”,”root”,””);

if(empty($connection)){

//echo “Could not connect with server”;

die(“Could not connect with server”);

}else{

echo “Connect with server”.”<br/>”;

}

 

$db=mysql_select_db(“ibirds”);

if(empty($db)){

echo “Database not found”;

}else{

echo “Open Database”.”<br/>”;

}

 

//Insert Records Example

/*

$query = “insert into students(fname, lname, city, dob, gender) values(‘Asif’, ‘Khan’, ‘Kota’, ‘1997-01-16’, ‘Male’)”;

 

echo $query . “<hr/>”;

$result=mysql_query($query);

/*

if($result){

//echo “Id is => ” . mysql_insert_id() . ” <hr/>”; //in case of  primary key and auto increment

echo “Record Inserted Successfully.”;

}else{

echo “Failed to Insert Record!<br/>”;

//echo “Error: ” . mysql_error() . ” <hr/>”;        //it displays mysql errors (display in sql window)

}

//blank means false

if(mysql_error() == false){

echo “<hr/>Record Inserted Successfully.”;

}else{

echo “Failed to Insert Record!<hr/>”;

echo “Error: ” . mysql_error() . ” <hr/>”;

}

 

//mysql_affected_rows()–>It returns how many rows are inserted or updated or deleted.

$rows = mysql_affected_rows();

echo “<br/>Total Records Inserted: ” . $rows;

 

 

//Update Records Example

 

$query = “update students set fname = ‘Shekhar where id = 1”;

 

echo $query . “<hr/>”;

$result = mysql_query($query);

 

$rows = mysql_affected_rows();

 

echo “Total Records Updated = $rows <br/>”;

 

if(mysql_error() == false){

echo “Record Updated Successfully.”;

}else{

echo “Failed to Updated Record!<hr/>”;

echo mysql_error();

}*/

?>

<h3>Fatch Row</h3>

<?php

//Fetching Records

//fetch row

$query=”select * from course”;

$data=mysql_query($query);

echo “Total Number of records fatched ==>> “.mysql_num_rows($data);

?>

<table border=”1px” align=”center”>

<tr>

<th>Course Id</th>

<th>Course Name</th>

<th>Fees</th>

<th>Duration</th>

<th>Status</th>

</tr>

<?php

//$row=mysql_fetch_row($data);  //first of all we have to fetch only one record and display it.

while($row = mysql_fetch_object($data)){

?>

<tr>

<td><?= $row->id ?</td>

<td><?= $row->name ?></td>

<td><?= $row->fees ?></td>

<td><?= $row->duration ?></td>

<td><?= $row->status ?></td>

</tr>

<?php

}

?>

</table>

 

 

 

 

 

 

 

 

students.php

 

<?php

error_reporting(0);

mysql_connect(“localhost”, “root”, “”) or die(“Could not connect with server!!!<br/>”);

mysql_select_db(“college”) or die(“Could not open database!!!<br/>”);

$query=”select * from students”;

if(!empty($_REQUEST[‘search’])){

$search = $_REQUEST[‘search’];

$query.=” where fname like ‘%$search%’ || lname like ‘%$search%’ || city like ‘%$search%'”;

}

$data=mysql_query($query);

?>

<form action=”insert.php”>

<table border=”1px” align=”center”>

<tr>

<th>First Name</th>

<td><input type=”text” name=”fname”/></td>

</tr>

<tr>

<th>Last Name</th>

<td><input type=”text” name=”lname”/></td>

</tr>

<tr>

<th>City</th>

<td><input type=”text” name=”city”/></td>

</tr>

<tr>

<th>Date of Birth</th>

<td><input type=”text” name=”dob”/></td>

</tr>

<tr>

<th>Gender</th>

<td>

<input type=”radio” name=”gender” value=”Male”/>Male

<input type=”radio” name=”gender” value=”Female”/>Female

</td>

</tr>

<tr>

<th colspan=”2″><input type=”submit” value=”Insert Record”/></th>

</tr>

</table>

</form>

<form>

<table align=”center”>

<tr>

<td>

Search Record : <input type=”text” name=”search”/><input type=”submit” value=”Submit Form”/>

</td>

</tr>

</table>

</form>

<table border=”1px” align=”center”>

<tr>

<th>Student’s Id</th>

<th>Name</th>

<th>City</th>

<th>Date of Birth</th>

<th>Gender</th>

<th>Action</th>

</tr>

<? while($row=mysql_fetch_object($data)){ ?>

<tr>

<td><?= $row->id ?></td>

<td><?= $row->fname.” “.$row->lname ?></td>

<td><?= $row->city ?></td>

<td><?= $row->dob ?></td>

<td><?= $row->gender ?></td>

<td>

<a href=”edit.php?id=<?= $row->id ?>”>Edit/</a>

<a href=”delete.php?id=<?= $row->id ?>” onClick=”return confirm(‘Are you sure to delete record??’)”>Delete</a>

</td>

</tr>

<? } ?>

</table>

 

insert.php

<?php

error_reporting(0);

mysql_connect(“localhost”, “root”, “”) or die(“Could not connect with server!!!<br/>”);

mysql_select_db(“college”) or die(“Could not open database!!!<br/>”);

$fname=$_REQUEST[‘fname’];

$lname=$_REQUEST[‘lname’];

$city=$_REQUEST[‘city’];

$dob=$_REQUEST[‘dob’];

$gender=$_REQUEST[‘gender’];

$query=”insert into students(fname, lname, city, dob, gender) values(‘$fname’, ‘$lname’, ‘$city’, ‘$dob’, ‘$gender’)”;

mysql_query($query);

header(“location:students.php”);

?>

 

 

 

 

 

delete.php

 

<?php

error_reporting(0);

mysql_connect(“localhost”, “root”, “”) or die(“Could not connect with server!!!<br/>”);

mysql_select_db(“college”) or die(“Could not open database!!!<br/>”);

$id=$_REQUEST[‘id’];

$query=”delete from students where id=$id”;

mysql_query($query);

header(“location:students.php”);

?>

 

 

 

 

 

 

 

edit.php

<?php

error_reporting(0);

mysql_connect(“localhost”, “root”, “”) or die(“Could not connect with server!!!<br/>”);

mysql_select_db(“college”) or die(“Could not open database!!!<br/>”);

$id=$_REQUEST[‘id’];

if(!empty($id)){

$dt=mysql_query(“select * from students where id=$id”);

$rw=mysql_fetch_object($dt);

}

?>

<form action=”update.php”>

<input type=”hidden” name=”id” value=”<?= $id?>”/>

<table border=”1px” align=”center”>

<tr>

<th>First Name</th>

<td><input type=”text” name=”fname” value=”<?= $rw->fname?>”/></td>

</tr>

<tr>

<th>Last Name</th>

<td><input type=”text” name=”lname” value=”<?= $rw->lname?>”/></td>

</tr>

<tr>

<th>City</th>

<td><input type=”text” name=”city” value=”<?= $rw->city?>”/></td>

</tr>

<tr>

<th>Date of Birth</th>

<td><input type=”text” name=”dob” value=”<?= $rw->dob?>”/></td>

</tr>

<tr>

<th>Gender</th>

<td>

<input type=”radio” name=”gender” value=”Male” <?= $rw->gender==’Male’?’checked’:” ?>/>Male

<input type=”radio” name=”gender” value=”Female” <?= $rw->gender==’Female’?’checked’:” ?>/>Female

</td>

</tr>

<tr>

<th colspan=”2″><input type=”submit” value=”Update Record”/></th>

</tr>

</table>

</form>

 

update.php

<?php

error_reporting(0);

mysql_connect(“localhost”, “root”, “”) or die(“Could not connect with server!!!<br/>”);

mysql_select_db(“zakir”) or die(“Could not open database!!!<br/>”);

$id=$_REQUEST[‘id’];

$fname=$_REQUEST[‘fname’];

$lname=$_REQUEST[‘lname’];

$city=$_REQUEST[‘city’];

$dob=$_REQUEST[‘dob’];

$gender=$_REQUEST[‘gender’];

$query=”update students set fname=’$fname’, lname=’$lname’, city=’$city’,

dob=’$dob’,gender= ‘$gender’ where id=$id”;

mysql_query($query);

header(“location:students.php”);

?>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

onepage.php

<?php

error_reporting(0);

mysql_connect(“localhost”, “root”, “”) or die(“Could not connect with server!!!<br/>”);

mysql_select_db(“zakir”) or die(“Could not open database!!!<br/>”);

$query=”select * from students”;

$button=”Insert”;

 

//Insert Record

if((!empty($_REQUEST[‘fname’])) && empty($_REQUEST[‘id’])) {

$fname=$_REQUEST[‘fname’];

$lname=$_REQUEST[‘lname’];

$city=$_REQUEST[‘city’];

$dob=$_REQUEST[‘dob’];

$gender=$_REQUEST[‘gender’];

mysql_query(“insert into students(fname, lname, city, dob, gender) values(‘$fname’, ‘$lname’, ‘$city’, ‘$dob’, ‘$gender’)”);

$fileName=mysql_insert_id();

$fileLoc=$_FILES[‘img’][‘tmp_name’];

move_uploaded_file($fileLoc, “images/$fileName.jpg”);

header(“location:onepage.php”);

}

 

//Edit Record

if(!empty($_REQUEST[‘id’])){

$button=”Update”;

$id=$_REQUEST[‘id’];

$edata=mysql_query(“select * from students where id=$id”);

$rdata=mysql_fetch_object($edata);

}

 

//Update Record

if((!empty($_REQUEST[‘fname’])) && !empty($_REQUEST[‘id’])) {

$id=$_REQUEST[‘id’];

$fname=$_REQUEST[‘fname’];

$lname=$_REQUEST[‘lname’];

$city=$_REQUEST[‘city’];

$dob=$_REQUEST[‘dob’];

$gender=$_REQUEST[‘gender’];

mysql_query(“Update students set fname=’$fname’, lname=’$lname’, city=’$city’, dob=’$dob’, gender=’$gender’ where id=’$id'”);

$fileLoc=$_FILES[‘img’][‘tmp_name’];

move_uploaded_file($fileLoc, “images/$id.jpg”);

header(“location:onepage.php”);

}

 

//Delete Record

if(!empty($_REQUEST[‘del’])){

$del=$_REQUEST[‘del’];

mysql_query(“delete from students where id=$del”);

unlink(“images/$del.jpg”);

header(“location:onepage.php”);

}

 

//Search Record

if(!empty($_REQUEST[‘search’])){

$search = $_REQUEST[‘search’];

$query.=” where fname like ‘%$search%’ || lname like ‘%$search%’ || city like ‘%$search%'”;

}

$data=mysql_query($query);

?>

 

<form enctype=”multipart/form-data” method=”post”>

<input type=”hidden” name=”id” value=”<?= $id ?>”/>

<table border=”1px” align=”center”>

<tr>

<th>First Name</th>

<td><input type=”text” name=”fname” value=”<?= $rdata->fname ?>” /></td>

</tr>

<tr>

<th>Last Name</th>

<td><input type=”text” name=”lname” value=”<?= $rdata->lname ?>”/></td>

</tr>

<tr>

<th>City</th>

<td><input type=”text” name=”city” value=”<?= $rdata->city ?>”/></td>

</tr>

<tr>

<th>Date of Birth</th>

<td><input type=”text” name=”dob” value=”<?= $rdata->dob ?>”/></td>

</tr>

<tr>

<th>Gender</th>

<td>

<input type=”radio” name=”gender” value=”Male” <?= $rdata->gender==’Male’?’checked’:” ?>/>Male

<input type=”radio” name=”gender” value=”Female” <?= $rdata->gender==’Female’?’checked’:” ?>/>Female

</td>

</tr>

<tr>

<th>Upload Image</th>

<td><input type=”file” name=”img”/></td>

</tr>

<tr>

<th colspan=”2″><input type=”submit” value=”<?= $button ?>”/></th>

</tr>

</table>

</form>

 

<form>

<table align=”center”>

<tr>

<td>

Search Record : <input type=”text” name=”search”/><input type=”submit” value=”Submit Form”/>

</td>

</tr>

</table>

</form>

<table border=”1px” align=”center”>

<tr>

<th>Student’s Id</th>

<th>Name</th>

<th>City</th>

<th>Date of Birth</th>

<th>Gender</th>

<th>Image</th>

<th>Action</th>

</tr>

<? while($row=mysql_fetch_object($data)){ ?>

<tr>

<td><?= $row->id ?></td>

<td><?= $row->fname.” “.$row->lname ?></td>

<td><?= $row->city ?></td>

<td><?= $row->dob ?></td>

<td><?= $row->gender ?></td>

<td><img src=”images/<?=$row->id?>.jpg” height=”100px” width=”100px”></td>

<td>

<a href=”?id=<?= $row->id ?>”>Edit/</a>

<a href=”?del=<?= $row->id ?>” onClick=”return confirm(‘Are you sure to delete record??’)”>Delete</a>

</td>

</tr>

<? } ?>

</table>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Image Upload (image.php)

 

 

<form action=”process.php” method=”post” enctype=”multipart/form-data”>

<table border=”1px” align=”center”>

<tr>

<th>Upload Image</th>

<td><input type=”file” name=”img”/></td>

<td><input type=”submit” value=”Upload Image”/></td>

</tr>

</table>

</form>

 

 

Image Upload (imageProcess.php)

 

<?php

$fileName = $_FILES[‘img’][‘name’];

$fileSize = $_FILES[‘img’][‘size’];

$fileType = $_FILES[‘img’][‘type’];

$fileLoc = $_FILES[“img”][“tmp_name”];

$fileError = $_FILES[“img”][“error”];

echo $fileName.”<br/>”;

echo $fileSize.” Bytes”.”<br/>”;

echo $fileType.”<br/>”;

echo $fileLoc.”<br/>”;

echo $fileError;

 

if(file_exists(“image/”.$fileName)){

echo “<br/>Failed to upload file.!<br/>”;

//die(“File Aready Exists!”);

unlink(“image/”.$fileName);

}

else{

move_uploaded_file($fileLoc, “image/”.$fileName);

echo “<br/>File uploaded successfully.!”;

}

 

?>

<img src=”image/<?= $fileName ?>” height=”500px” width=”500px”/>

session (login.php)

<?php

error_reporting(0);

$msg = $_REQUEST[‘msg’];

?>

<html>

<head>

<meta charset=”utf-8″>

<title>Login Page</title>

</head>

 

<body>

<h1><?=$msg?></h1>

<form action=”process.php” method=”get”>

<table>

<tr>

<th>Select User Type</th>

<td>

<select name=”type”>

<option value=”admin”>admin</option>

<option value=”student”>student</option>

</select>

</td>

</tr>

<tr>

<th>User Name</th>

<td><input type=”text” name=”uname”/></td>

</tr>

<tr>

<th>Password</th>

<td><input type=”password” name=”password”/></td>

</tr>

<tr>

<th>Submit Form</th>

<td><input type=”submit” value=”Submit”/></td>

</tr>

</table>

</form>

</body>

</html>

process.php

<?php

session_start();

$uname = $_REQUEST[‘uname’];

$password = $_REQUEST[‘password’];

$type = $_REQUEST[‘type’];

if($uname==’admin’ && $password==’root’){

$_SESSION[‘name’]=$uname;

$_SESSION[‘pass’]=$password;

$_SESSION[‘type’]=$type;

if($type==’admin’){

header(“location:home.php”);

}

elseif($type==’student’){

header(“location:about.php”);

}

}

else

{

$error=”User name or password incorrect”;

header(“location:login.php?msg=$error”);

}

?>

 

home.php

<?php

session_start();

if(empty($_SESSION[‘name’]))    header(“location:login.php”);

else

{

$type = $_SESSION[‘type’];

$user = $_SESSION[‘name’];

$password = $_SESSION[‘pass’];

}

?>

<h1>This is my home page</h1>

<h2>Login Type : <?= $type ?></h2>

<h2>Welcome : <?= $user ?></h2>

<h3>Password : <?= $password ?></h3>

<a href=”logout.php”>Logout</a>

about.php

<?php

session_start();

if(empty($_SESSION[‘name’]))                header(“location:login.php”);

else

{

$type = $_SESSION[‘type’];

$user = $_SESSION[‘name’];

$password = $_SESSION[‘pass’];

}

?>

<h1>This is my about us page</h1>

<h1>This is my home page</h1>

<h2>Login Type : <?= $type ?></h2>

<h2>Welcome : <?= $user ?></h2>

<h3>Password : <?= $password ?></h3>

<a href=”logout.php”>Logout</a>

 

 

 

 

logout.php

<?php

session_start();

session_destroy();

header(“location:login.php”);

?>

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *