Directory Structure :
1. Create database name registration1 and table tblstudent as code below :
--
-- Table structure for table `tblstudent`
--
drop database if exists registration1;
create database registration1;
use registration1;
DROP TABLE IF EXISTS `tblstudent`;
SET @saved_cs_client = @@character_set_client;
SET character_set_client = utf8;
CREATE TABLE `tblstudent` (
`StudentID` int(5) NOT NULL default '0',
`FirstName` varchar(20) character set utf8 default NULL,
`LastName` varchar(20) character set utf8 default NULL,
`Sex` varchar(5) default NULL,
`Email` varchar(50) character set utf8 default NULL,
`Address` varchar(100) character set utf8 default NULL,
`Subject` varchar(50) default NULL,
`Sport` varchar(50) default NULL,
`Fruit` varchar(50) character set utf8 default NULL,
PRIMARY KEY (`StudentID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
SET character_set_client = @saved_cs_client;
--------------------------
2. Insert data to table database by using insert.php\
<?php
if (isset($_POST['submit'] ) ) {
$host_name = "localhost";
$user_name = "root";
$password = "";
$db = mysql_connect( $host_name, $user_name, $password );
if (!$db)
{
die("Failed" . mysql_error($db));
}
$selectdb=mysql_select_db("registration1",$db);
$fname =$_POST['fname'];
$lname = $_POST['lname'];
$sex =$_POST['sex'];
$mail =$_POST['email'];
$add = $_POST['add'];
$subject =$_POST['subject'];
$sport =$_POST['sport'];
$fruit = $_POST['fruit'];
// example of inserting data into that table:
$sql = "INSERT INTO tblstudent (FirstName,LastName,Sex,Email,Address,subject,Sport,Fruit) VALUES('$fname','$lname','$sex','$mail','$add','$subject','$sport','$fruit')";
if (mysql_query($sql)) {
echo "New record created successfully";
} else {
echo "fail to insert".mysql_error();
}
mysql_close($db);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Bootstrap 101 Template</title>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<h1>Hello, world!</h1>
<table border="0" align="center" width="60%">
<tr><td>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" >
<div class="form-group">
<label for="exampleInputEmail1">Fist name</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="Firstname" name="fname">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Last name</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="Lastname" name="lname">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Sex</label>
<select name="sex" class="form-control">
<option value="mail">Mail</option>
<option value="femail">Femail</option>
</select>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email" name="email">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Address</label>
<textarea cols=30 rows=10 name="add"></textarea>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Subject</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="Subject" name="subject">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Sport</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="sport" name="sport">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Fruit</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="fruit" name="fruit">
</div>
<input type="submit" class="btn btn-default" name="submit" value="submit">
</form>
</td></tr></table>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
Note : you can using validation to check data to table database by using java script in <head>
<script language="javascript">
function validateFormOnSubmit(theForm) {
var reason = "";
//reason += validateUsername(theForm.username);
//reason += validatePassword(theForm.pwd);
//reason += validateEmail(theForm.email);
//reason += validatePhone(theForm.phone);
reason += validateEmpty(theForm.fname);
reason += validateEmpty(theForm.lname);
if (reason != "") {
alert("Some fields need correction:\n" + reason);
return false;
}
return true;
}
function validateEmpty(fld) {
var error = "";
if (fld.value.length == 0) {
fld.style.background = 'pink';
error = "The required field has not been filled in.\n"
} else {
fld.style.background = 'White';
}
return error;
}
</script>
in form tag have to add attribute as below :
onsubmit="return validateFormOnSubmit(this)" name="theForm"
---------------------------------
3. registration.php
<?php
$conn=mysql_connect("localhost","root","") or die("Cannot connect to Mysql server!");
mysql_select_db("registration1",$conn);
$queryID=$_GET['ID'];
$sql="delete from tblstudent where studentID='$queryID'";
mysql_query($sql) or die("cannot delete "+ mysql_error());
$query="select * from tblstudent";
$result=mysql_query($query,$conn);
echo "<table border=1><tr><th>Student ID<th>FirstName<th>LastName<th>Sex
<th>Email<th>Address<th>Subject<th>Sport<th>Fruit</tr> ";
while ($row=mysql_fetch_array($result)) {
$stdid= $row['StudentID'];
$fname=$row['FirstName'];
$lname=$row['LastName'];
$sex=$row['Sex'];
$email=$row['Email'];
$address=$row['Address'];
$subj= $row['Subject'];
$sport=$row['Sport'];
$fruit=$row['Fruit'];
echo "<tr><td>$stdid<td>$fname<td>$lname<td>$sex<td>$email
<td>$address<td>$subj<td>$sport<td>$fruit<td>";
?>
<a href=editform.php?varID=<?php echo $stdid ?>>Edit</a><td>
<a href='#' onClick="confirmDelete('<?php echo $stdid ?>')">Delete</a>
</tr>
<?php
}
echo "</table>";
mysql_close($conn);
?>
<html>
<head>
<script language="javascript">
function confirmDelete(ID){
var answer=confirm("Do you want to delete this record?");
if(answer)
window.location="registration.php?ID="+ID;
else
alert("Record was not deleted");
}
</script>
</head>
<body>
</body>
</html>
-------------------------------
4. EditForm.php
<?php
$conn = mysql_connect( "localhost", "root", "");
if (!$conn)
{ die("Failed" . mysql_error($conn));}
$selectdb=mysql_select_db("registration1",$conn);
$ID=$_GET['varID'];
$query="select * from tblstudent where StudentID=$ID";
$result=mysql_query($query,$conn);
while($row=mysql_fetch_array($result)){
$strID= $row['StudentID'];
$fname=$row['FirstName'];
$lname=$row['LastName'];
$sex=$row['Sex'];
$email=$row['Email'];
$address=$row['Address'];
$subj= $row['Subject'];
$sport=$row['Sport'];
$fruit=$row['Fruit'];
}
mysql_close($conn);
echo "Hi ".$lname;
?>
<html>
<body>
<a href="insert.php">Add Record</a>
<form method="POST">
<table bgcolor="pink" align="center">
<caption>Online Registration</caption>
<tr><td>Student ID<td><input type="text" name="txtid" value="<?php echo $strID;?>"></tr>
<tr><td>First Name<td><input type="text" name="txtfname" value="<?php echo $fname;?>"></tr>
<tr><td>Last Name<td><input type="text" name="txtlname" value="<?php echo $lname;?>"></tr>
<tr><td>Sex<td><input type="text" name=txtGender value="<?php echo $sex; ?>"></tr>
<tr><td>E-mail<td><input type="text" name="txtmail" value="<?php echo $email;?>"></tr>
<tr><td>Address<td><textarea name="txtaddress" cols="20" rows="3" ><?php echo $address;?></textarea></tr>
<tr><td>Select Subject:<td><input type="text" name=txtSubject value="<?php echo $subj;?>"></tr>
</select> </tr>
<tr><td>Favorite sport:<td><input type="text" name=txtSport value="<?php echo $sport;?>"></tr>
<tr><td>Fovorite Fruits:<td><input type="text" name=txtFruit value="<?php echo $fruit;?>"></td>
<tr><td colspan="2" align="center">
<input type="submit" value="Update" name="btn">
<input type="submit" value="Cancel" name="btn" onClick="Javscript:window.location=registration.php;">
</tr>
</table>
</form>
</body></html>
No comments:
Post a Comment