Insert data in radio button , Multiple checkbox , select box in database Mysql using PHP – HTML Tutorial makcode.in

Create Insert.html for inserting a data with method POST AND Action = “insert-process.php”

First create a database name crud and create table name cruddetails and add columns – id,name,address,gender,edu,year

insert.html

<!DOCTYPE html>
<html>
<head>
	<title>Insert Records</title>
</head>
<body>
	
  <h1><a href="Records.php">All Records</a></h1>
<form action="insert-process.php" method="POST">
NAME: <input type="text" name="name"><br>
Address: <textarea name="address" cols="20" rows="5"></textarea><br>
Gender: <input type="radio" name="gender" value="Male">Male
        <input type="radio" name="gender" value="Female">Female<br>
Education: <input type="checkbox" name="edu[]" value="BE">BE
           <input type="checkbox" name="edu[]" value="DE">DE
           <input type="checkbox" name="edu[]" value="MCA">MCA<br>
Year: <select name="year">
	<option value="2018">2018</option>
	<option value="2019">2019</option>
	<option value="2020">2020</option>
</select><br>

<input type="submit" name="save">
</form>


</body>
</html>

insert-process.php for processing data between html and php-sql database.

<?php

$conn= mysqli_connect('localhost','root','','crud');
if (!$conn){
	die('could not connect mysql:' .msql_error());
}

if (isset($_POST['save'])) {
	
	$name = $_POST['name'];
	$address = $_POST['address'];
	$gender = $_POST['gender'];
	$edu = implode(",",$_POST['edu']);
	$year = $_POST['year'];

  $sql = "INSERT INTO cruddetails (name,address,gender,edu,year) VALUES ('$name','$address','$gender','$edu','$year')";
  if (mysqli_query($conn,$sql)) {
  	
          echo "<script>alert('Insert successfully');</script>";
          echo "<script>window.location.href='insert.html'</script>";
  } else {
  	echo "error: ". $sql .";
  	" . mysqli_error($conn);
  }
  mysqli_close($conn);
}

?>

record.php for retrieve data from database

<?php
$conn= mysqli_connect('localhost','root','','crud');
if (!$conn){
  die('could not connect mysql:' .msql_error());
}
$result = mysqli_query($conn,"SELECT * FROM cruddetails");
?>
<!DOCTYPE html>
<html>
 <head>
 <title> Retrive data</title>
 </head>
<body>
  <h4><a href="insert.html">Insert Data</a></h4>
<?php
if (mysqli_num_rows($result) > 0) {
?>
  <table>
  
  <tr>
    <td>Name</td>
    <td>Address</td>
    <td>Gender</td>
    <td>Education</td>
    <td>Year</td>
    <td>Action</td>
  </tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
    <td><?php echo $row["name"]; ?></td>
    <td><?php echo $row["address"]; ?></td>
    <td><?php echo $row["gender"]; ?></td>
    <td><?php echo $row["edu"]; ?></td>
    <td><?php echo $row["year"]; ?></td>
    <td><a href="edit.php?id=<?php echo $row["id"]; ?>">Update</a></td>
</tr>
<?php
$i++;
}
?>
</table>
 <?php
}
else{
    echo "No result found";
}
?>
 </body>
</html>

Leave a Reply

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