<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr =
"";
$name = $email = $gender = $comment = $website =
"";
if (isset($_POST['submit'])) {
if
(empty($_POST["name"])) {
$nameErr =
"Name is required";
}
if
(empty($_POST["email"])) {
$emailErr =
"Email is required";
}
if
(empty($_POST["website"])) {
$website =
"";
}
if
(empty($_POST["comment"])) {
$comment =
"";
}
if
(empty($_POST["gender"])) {
$genderErr =
"Gender is required";
}
$n=$_POST['name'];
$e=$_POST['email'];
$w=$_POST['website'];
$c=$_POST['comment'];
$g=$_POST['gender'];
}
?>
<form method="post"
action="w3.php">
Name: <input type="text"
name="name">
* <?php echo $nameErr;?>
<br><br>
E-mail:
<input type="text" name="email">
* <?php echo $emailErr;?>
<br><br>
Website:
<input type="text" name="website">
<?php echo $websiteErr;?>
<br><br>
Comment: <textarea name="comment"
rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="gender"
value="female">Female
<input type="radio" name="gender"
value="male">Male
<input type="radio" name="gender"
value="other">Other
* <?php echo $genderErr;?>
<br><br>
<input type="submit" name="submit"
value="Submit">
</form>
For connection.php
<?php
$con=mysqli_connect("localhost","root","");
$sql="CREATE DATABASE student3";
if(mysqli_query($con,$sql))
{
echo
"database created";
}
?>
Create
table
<?php
$con=new
mysqli("localhost","root","","student3");
$sql="CREATE
TABLE details(roll INT(10) PRIMARY KEY AUTO_INCREMENT,
Name VARCHAR(20) NOT NULL,
Email
VARCHAR(20) NOT NULL,
Website VARCHAR(20)
NOT NULL,
Gender VARCHAR(20)
NOT NULL,
Comment VARCHAR(20)
NOT NULL)";
if(mysqli_query($con,$sql))
{
echo
"table created";
}
?>
Insert table
<?php
if(isset($_POST['submit']))
{
$n=$_POST['name'];
$e=$_POST['email'];
$w=$_POST['website'];
$c=$_POST['comment'];
$g=$_POST['gender'];
}
$con=new
mysqli("localhost","root","","student3");
$sql= "INSERT INTO
details(Name,email,website,comment,gender)VALUES('$n','$e','$w','$c','$g')";
if(mysqli_query($con,$sql))
{
echo
"data inserted";
}
else
{
echo
mysqli_error($con);
}
?>
IF
YOU WANT TO TABLE IS ONLY RECORD WHEN COMPLETE DETAILS IS AVILABLE IN FORM
YOU
ADD JUST ONE LINE BEFORE INSERT QUERY
<?php
$con=new
mysqli("localhost","root","","student3");
$n=$e="";
if(isset($_POST['submit']))
{
$n=$_POST['name'];
$e=$_POST['email'];
$w=$_POST['website'];
$c=$_POST['comment'];
$g=$_POST['gender'];
if($n&&$e&&$w&&$g)
{
$sql=
"INSERT INTO
details(Name,email,website,comment,gender)VALUES('$n','$e','$w','$c','$g')";
if(mysqli_query($con,$sql))
{
echo "data inserted";
}
else
{
echo mysqli_error($con);
}
}
else{
}echo
"Complete *required field";
}
}
mysqli_close($con);
?>
Fetch data
<html>
<head>
<style>
table,th,td
{
border:1px
solid blue;
}
th{
background-color:red;
}
th,td{
height:50px;
width:90px;
}
</style>
</head>
<body>
<table border="1">
<tr><th
colspan="8">view all records</th></tr>
<tr><td>Roll
No.</td><td>Name</td>
<td>email</td><td>website</td>
<td>gender</td>
<td>edit</td>
<td>delete</td>
<td>details</td></tr>
<?php
$conn= new
mysqli("localhost","root","","student3");
$sql="SELECT * FROM
details";
$result=$conn->query($sql);
while($row=$result->fetch_assoc()){
echo
"<table border='1'>";
echo
"<tr><td>".$row['roll']."</td><td>".$row['Name']."</td>";
echo
"<td>".$row['Email']."</td>";
echo
"<td>".$row['Website']."</td>";
echo
"<td>".$row['Gender']."</td>";
echo "<td>edit</td>
<td>delete</td>
<td>details</td></tr>";
}
echo "</table>";
?>
No comments:
Post a Comment