Insert CheckBox and Radio button Data in MySQL Database Using PHP


MY-SQL Code

CREATE DATABASE IF NOT EXISTS databasename;
CREATE TABLE students(

student_name varchar(255) NOT NULL,
student_email varchar(255) NOT NULL,
student_contact varchar(255) NOT NULL,
student_address varchar(255) NOT NULL,
stars varchar(25) NOT NULL,
sports varchar(25) NOT NULL,
gender text(25) NOT NULL,
)


 

 

 
page- formelements.php 
 
php code

<?php
$connection = mysqli_connect("localhost", "root", "","formdatainsert"); // Establishing Connection with Server
//$db = mysql_select_db("colleges", $connection); // Selecting Database from Server
if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL
$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$address = $_POST['address'];
$sports=implode(' , ',$_POST['sports']);
$star=implode(' , ',$_POST['star']);
$gender = $_POST['gender'];
if($name !=''||$email !=''){
//Insert Query of SQL
$query = mysqli_query($connection, "insert into students(student_name, student_email, student_contact, student_address, star, sports, gender) values ('$name', '$email', '$contact', '$address', '$star', '$sports', '$gender')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
}
mysqli_close($connection); // Closing Connection with Server
?>

FORM HTML CODE


<!DOCTYPE html>
<html>
<head>
<title>PHP insertion</title>
<link href="css/style.css" rel="stylesheet">
</head>
<body>
<div class="maindiv">
<!--HTML Form -->
<div class="form_div">
<div class="title">
<h2>Insert Data In Database Using PHP.</h2>
</div>
<form action="formelements.php" method="post">
<!-- Method can be set as POST for hiding values in URL-->
<h2>Form</h2>
<label>Name:</label>
<input class="input" name="name" type="text" value="" required>
<label>Email:</label>
<input class="input" name="email" type="email" value="" required>
<label>Contact:</label>
<input class="input" name="contact" type="text" value="" required>
<label>Password:</label>
<input class="input" name="password" type="password" value="" required>

<label>Address:</label>
<textarea cols="25" name="address" rows="5" required></textarea><br><br>

<label>start rating: </label><br>
<input type="checkbox" name="star[]" value="5"><label>5 Star</label><br>
<input type="checkbox" name="star[]" value="4"><label>4 Star</label><br>
<input type="checkbox" name="star[]" value="3"><label>3 Star</label><br>
<input type="checkbox" name="star[]" value="2"><label>2 star</label><br>
<input type="checkbox" name="star[]" value="1"><label>1 star</label><br><br/>

<label>sports: </label><br>
<input type="checkbox" name="sports[]" value="tennis"><label>tennis</label><br>
<input type="checkbox" name="sports[]" value="football"><label>football</label><br>
<input type="checkbox" name="sports[]" value="badminton"><label>badminton</label><br>
<input type="checkbox" name="sports[]" value="hockey"><label>hockey</label><br>
<input type="checkbox" name="sports[]" value="cricket"><label>cricket</label><br><br/>

<label>Gender:</label>
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male


<input class="submit" name="submit" type="submit" value="Insert" >
</form>
</div>
</div>
</body>
</html>

Comments

Post a Comment

Popular posts from this blog

Customize radio buttons and checkboxes with CSS sprites

Script For Login, Logout and View Using PHP, MySQL and Bootstrap

Real-Time Web Interface to MQTT using Socket.io and Node.js