Problems:
Create a simple data insert form with fields (id,name,roll) in PHP and my-SQL. (2013)

Create html form to insert data into mysql database using php | Insert data in Mysql using Php Form | how to create an html form that stores data in a mysql database and retrieving data  using php

Insert data in Mysql using Php Form

Solution:

<?php
$server_name = "localhost:3308";
$db = "newdb";
$user_name = "root";
$pass = "";

//Create Connection
$conn = new mysqli($server_name,$user_name,$pass,$db);

//Check Connectio
if(!$conn){
die ("Database problem!!!");
}
else{
echo "Connection Ok";
echo "<br>";
}

//insert data into Mysql database table

$a=$_POST["id"];
$b=$_POST["name"];
$c=$_POST["roll"];

if($a!=""){
$d ="INSERT INTO stu(id,name,roll) VALUES('$a','$b','$c')";
if(mysqli_query($conn,$d)){
echo "Data insert ok";
}else{
echo "data insert failed";
}
}

//retriving (shows) data
$e="SELECT* FROM stu where roll ='$c'";
$result = mysqli_query($conn,$e);
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_array($result)){
$id= $row['id'];
// echo $row['id'];
$name= $row['name'];
$roll= $row['roll'];
}
}else{
echo "no data match";
}
?>

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="index.php">
<table>
<tr>
<th>id:</th>
<td><input type="text" name="id"></td>
</tr>
<tr>
<th>name:</th>
<td><input type="name" name="name"></td>
</tr>
<tr>
<th>roll:</th>
<td><input type="text" name="roll"></td>
</tr>
<tr>
<td>
<input type="submit" name="submit" id="sub" value="Submit">
</td>
</tr>
</table>
</form>

<table>
<tr><th>Retriving data</th></tr>
<tr>
<th>id:</th><td><?php echo $id; ?></td>
</tr>
<tr>
<th>Name:</th><td><?php echo $name; ?></td>
</tr>
<tr>
<th>Roll:</th><td><?php echo $roll; ?></td>
</tr>
</table>
</body>
</html>


Screensht of Source Code(Sublime Text):

Insert data in Mysql using Php
Insert data in Mysql using Php
Insert data in Mysql using Php
Insert data in Mysql using Php

Output(Browser):

Connection Ok
Data insert ok
id:
name:
roll:

Retrieving data
id:           A22
Name:    BASU
Roll:       122

Screenshot of Output:

Insert data in Mysql using Php


Post a Comment

Previous Post Next Post