Digital World Freelancer

The Digital World Freelancer group is applying latest Technologies Like HTML, CSS, CSS3, JavaScript, JQuery, PHP and more...... for website designing & developing... reach at our organization http://www.digitalworldfreelancer.com

Facebook Page

Monday, October 13, 2014

How to Send Form Data in Database/How to Add/Edit/Delete/View a form entry dynamically through PHP and MySQL?


Learn here everything very quickly.............


1 Step) Make a Database and Table Like or same Below.....


2 Step) connection.php

<?php
define("HOSTNAME", "localhost");
define("USERNAME", "root");
define("PASSWORD", "");
define("DATABASE", "school_management");
$conn = mysqli_connect(HOSTNAME, USERNAME, PASSWORD, DATABASE);
if(!$conn) {
die("Mysql error");
}
?>



3 Step) add_student.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Add Student</title>
</head>

<body>
<form action="save_student.php" method="post" enctype="multipart/form-data">
<table align="center">
<tr>
<th colspan="2">
Add Student
</th>
</tr>
<tr>
<td>Roll No</td>
<td><input type="text" name="roll_no" value="" maxlength="10" /></td>
</tr>
<tr>
<td>Student Name</td>
<td><input type="text" name="name" value="" maxlength="50"/></td>
</tr>
<tr>
<td>Father's Name</td>
<td><input type="text" name="father" value="" maxlength="50"/></td>
</tr>
<tr>
<td>Mother's Name</td>
<td><input type="text" name="mother" value=""  maxlength="50"/></td>
</tr>
<tr>
<td>Date Of birth</td>
<td><input type="text" name="dob" value="" /><br />(eg : 20/09/2014 => 2014-09-20)</td>
</tr>
<tr>
<td>Streem</td>
<td>
<select name="streem">
<option>Select Streem</option>
<option value="Art">Art</option>
<option value="Science">Science</option>
</select>
</td>
</tr>
<tr>
<td>Student's Photo</td>
<td>
<input type="file" name="photo"  />
  </td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Save" /><input type="reset" name="reset" value="Reset" /></td>
</tr>
</table>
</form>
</body>
</html>


4 Step) save_student.php


<?php 
include("connection.php");
$roll_no = $_POST['roll_no'];
$name = $_POST['name'];
$father = $_POST['father'];
$mother = $_POST['mother'];
$dob = $_POST['dob'];
$streem = $_POST['streem'];
// upload photo
$photo = $_FILES['photo'];
$photo_path = "";
//print_r($photo); die;
if($photo['type'] == "image/jpeg" || $photo['type'] == "image/png" || $photo['type'] == "image/gif") {
$photo_name = uniqid().".jpeg";
$photo_path = "./images/student_photo/".$photo_name;
move_uploaded_file($photo['tmp_name'], $photo_path);
//die("hello");
}
$sql = "insert into students (roll_no, name, father, mother, dob, streem, photo) values (".$roll_no.", '".$name."', '".$father."', '".$mother."', '".$dob."', '".$streem."', '".$photo_path."')";
$result = mysqli_query($conn, $sql) or die($conn);
if($result) {
header("location:index.php");
} else {
header("location:add_student.php?msg=some error");
}
?>


5 Step) edit.php

<?php 
include("connection.php"); 
$id = "";
// check status
if(isset($_GET['id'])) {
$id = $_GET['id'];
} else {
header("location:index.php");
}
// Get student record
$sql = "select * from students where id = ".$id;
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
$data = array();
if(mysqli_num_rows($result) > 0) {
$data = mysqli_fetch_assoc($result);
} else {
header("location:index.php");
}
//print_r($data); die;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Edit student record</title>
</head>

<body>
<form action="update_student.php" method="post" enctype="multipart/form-data">
<input name="id" type="hidden" value="<?php echo $data['id'];?>"/>
<table align="center">
<tr>
<th colspan="2">
Edit Student
</th>
</tr>
<tr>
<td>Roll No</td>
<td><input type="text" name="roll_no" value="<?php echo $data['roll_no'];?>" maxlength="10" /></td>
</tr>
<tr>
<td>Student Name</td>
<td><input type="text" name="name" value="<?php echo $data['name'];?>" maxlength="50"/></td>
</tr>
<tr>
<td>Father's Name</td>
<td><input type="text" name="father" value="<?php echo $data['father'];?>" maxlength="50"/></td>
</tr>
<tr>
<td>Mother's Name</td>
<td><input type="text" name="mother" value="<?php echo $data['mother'];?>"  maxlength="50"/></td>
</tr>
<tr>
<td>Date Of birth</td>
<td><input type="text" name="dob" value="<?php echo $data['dob'];?>" /><br />(eg : 20/09/2014 => 2014-09-20)</td>
</tr>
<tr>
<td>Streem</td>
<td>
<select name="streem">
<option>Select Streem</option>
<option value="Art" <?php echo ($data['streem'] == "Art" ? "selected" :  "");?> >Art</option>
<option value="Science" <?php echo ($data['streem'] == "Science" ? "selected" :  "");?>>Science</option>
</select>
</td>
</tr>
<tr>
<td>Student's Photo</td>
<td>
<input type="file" name="photo"  />
<img src="<?php echo $data['photo'];?>" width="50px"/>
  </td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Update" /><input type="reset" name="reset" value="Reset" /></td>
</tr>
</table>
</form>
</body>
</html>


6 Step) update_student.php


<?php 
include("connection.php");
$id = $_POST['id'];
$roll_no = $_POST['roll_no'];
$name = $_POST['name'];
$father = $_POST['father'];
$mother = $_POST['mother'];
$dob = $_POST['dob'];
$streem = $_POST['streem'];
// upload photo
$photo = $_FILES['photo'];
$photo_path = "";
if(!empty($photo)) {
if($photo['type'] == "image/jpeg" || $photo['type'] == "image/png" || $photo['type'] == "image/gif") {
$photo_name = uniqid().".jpeg";
$photo_path = "./images/student_photo/".$photo_name;
move_uploaded_file($photo['tmp_name'], $photo_path);
//die("hello");
}
}
if($photo_path != "") {
$sql = "update students 
set roll_no = ".$roll_no.", 
name = '".$name."', 
father = '".$father."', 
mother = '".$mother."', 
dob = '".$dob."', 
streem = '".$streem."', 
photo = '".$photo_path."' 
where id = ".$id;
} else {
$sql = "update students 
set roll_no = ".$roll_no.", 
name = '".$name."', 
father = '".$father."', 
mother = '".$mother."', 
dob = '".$dob."', 
streem = '".$streem."' 
where id = ".$id;
}
$result =  mysqli_query($conn, $sql) or die(mysqli_error($conn));
if($result) {
header("location:index.php");
} else  {
header("location:edit_student.php?id=".$id);
}


7 Step) delete_student.php

<?php 
include("connection.php");
$id = "";
if(isset($_GET['id'])) {
$id = $_GET['id'];
} else {
header ("location:index.php");
}
$sql = "delete from students where id = ".$id;
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
header("location:index.php");
?>


8 Step) index.php


<?php include("connection.php"); ?>
<html>
<head>
<title>My schoole management</title>
</head>
<body>
<table align="center" width="1024px" border="1" cellpadding="10" cellspacing="0">
<tr>
<td colspan="7" align="left">Students list</td>
<td align="right">
<a href="add_student.php">Add</a>
</td>
</tr>
<tr>
<td>Roll No.</td>
<td>Name</td>
<td>Father Name</td>
<td>Mother Name</td>
<td>Streem</td>
<td>Date Of birth</td>
<td>Photo</td>
<td>Created date</td>
<td>Actions</td>
</tr>
<?php 
$sql = "select * from students";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row['roll_no'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['father'];?></td>
<td><?php echo $row['mother'];?></td>
<td><?php echo $row['streem'];?></td>
<td><?php echo $row['dob'];?></td>
<td><img src="<?php echo $row['photo'];?>" width="50px"/></td>
<td><?php echo date("d M, Y", strtotime($row['created_date']));?></td>
<td>
<a href="edit_student.php?id=<?php echo $row['id'];?>">Edit</a>
<a href="delete_student.php?id=<?php echo $row['id'];?>">Delete</a>
</td>
</tr>
<?php 
}
  }
?>
</table>
</body>
</html>

No comments: