We can insert record into database using PHP script.
For sending form data into MySQL table we have two methods – GET, POST. We generally use ‘POST’ method. Because it’s more secure than GET method.
Syntax –
INSERT INTO table_name (column_name1,column_name2,..) VALUES (value1,value2,…);
consider table – customer(enrollno(bigint(240)),custname(varchar(240)),phone(int(10)),loc(varchar(240)))
- Let’s make 2 files for doing insert operation into mysql db using PHP script…
- form1.html – getting form input
- form1.php – send values to database using PHP script.
File – form1.html
<!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.0″> <title>form1</title> </head> <body> <br> <form action=”form1.php” method=”post”> <label for=”custname”>Customer Name</label> <br> <input type=”text” id=”custname” name=”custname”> <br> <label for=”phonenum”>Customer Phone</label> <br> <input type=”text” id=”phonenum” name=”phonenum”> <br> <label for=”loc”>Location</label> <br> <input type=”text” id=”loc” name=”loc”> <br> <br> <input type=”submit” value=”submit”> </form> </body> </html> |

File – form1.php
<?php error_reporting(0); // connection with database $conn = mysqli_connect(‘localhost’,’root’,”,’microcodes’); if($conn){ // echo “connection established!”; } else{ echo “connection error!”; } // getting values from .. form $name = $_POST[‘custname’]; $phone = $_POST[‘phonenum’]; $loc = $_POST[‘loc’]; $sql=”INSERT INTO customers(enrollno,custname,phone,loc) VALUES(NULL, ‘$name’,’$phone’,’$loc’)”; $query = mysqli_query($conn,$sql); if($query){ echo “data send successfuly!”; } else{ echo “data can’t send”; } ?> |

File output – phpmyadmin

- Let’s see another example…
File – form2.html
<!doctype html> <html lang=”en”> <head> <!– Required meta tags –> <meta charset=”utf-8″> <meta name=”viewport” content=”width=device-width, initial-scale=1″> <title>form 2</title> </head> <body> <br> <!– form –> <form action=”form2.php” method=”post”> <label for=”custname”>Customer Name</label> <input type=”text” id=”custname” name=”custname” required> <br><br> <label for=”email”>Email address</label> <input type=”email” id=”email” name=”email” aria-describedby=”emailHelp” required> <br><br> <label for=”passd” >Password</label> <input type=”password” id=”passd” name=”passd” required> <br><br> <font style=”font-size: 20px;”>Select Gender</font> <br> <input type=”checkbox” value=”male” name=”male” id=”male”> <label for=”male”> Male </label> <br> <input type=”checkbox” value=”female” name=”female” id=”female”> <label for=”female”> Female </label> <br> <input type=”checkbox” value=”other” name=”other” id=”other”> <label class=”form-check-label” for=”other”> other </label> <br><br> <font style=”font-size: 20px;”>Select Call-time</font> <select aria-label=”Default select example” name=”calltime” required> <option>select one</option> <option value=”9to1pm”>9AM-1PM</option> <option value=”2to8pm”>2-8PM</option> <option value=”9to11pm”>9-11PM</option> </select> <br> <br> <button type=”submit” value=’Submit’>Submit</button> </form> </body> </html> |

File – form2.php
<?php error_reporting(0); $conn = mysqli_connect(‘localhost’,’root’,”,’microcodes’); if($conn){ // echo “connection established!”; } else{ echo “connection error!”; } $name = $_POST[‘custname’]; $mail= $_POST[’email’]; $pass = $_POST[‘passd’]; $male = $_POST[‘male’]; $female = $_POST[‘female’]; $other = $_POST[‘other’]; $calls = $_POST[‘calltime’]; if($male){ $gen = ‘M’; } if($female){ $gen=’F’; } if($other){ $gen=’O’; } // date() method helps to generate current date format->day/month/year. $dates = date(‘d/m/y’); $sql = “INSERT INTO `callsupport` (`enrollno`, `custname`, `email`, `password`, `gender`, `calltime`, `date`) VALUES (NULL, ‘$name’, ‘$mail’, ‘$pass’, ‘$gen’, ‘$calls’, ‘$dates’)”; $query = mysqli_query($conn,$sql); if($query){ echo “record inserted!!”; echo “<br>”; echo “<br> customer name: $name”; echo “<br> customer email: $mail”; echo “<br> customer contact time: $calls”; } ?> |

File – phpmyadmin

- Let’s make a code, to insert a person name, phone number with his/her CV (.pdf) in MySQL DB.
File – form3.html
<!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.0″> <title>form 3</title> </head> <body> <br> <font style=”font-size: 30px;”>enter your details…</font> <br> <br> <form action=”send.php” method=”post” enctype=”multipart/form-data”> <font style=”font-size: 20px;”>Enter Name</font> <br> <input type=”text” name=”name”> <br> <font style=”font-size: 20px;”>Enter Phone</font> <br> <input type=”text” name=”phone”> <br> <br> <font style=”font-size: 20px;”>Upload your CV</font> <br><br> <input type=”file” name=”pdf_file” accept=”.pdf” title=”Upload your CV”/> <br> <br> <input type=”submit” name=”submit” value=”submit”> </form> </body> </html> |

File – form3.php
<?php error_reporting(0); $con = mysqli_connect(‘localhost’,’root’,”,’microcodes’); if($con){ // echo “connection established!”; } else{ echo “connection error!”; } $name = $_POST[‘name’]; $phone_no = $_POST[‘phone’]; if (isset($_FILES[‘pdf_file’][‘name’])) { $file_name = $_FILES[‘pdf_file’][‘name’]; $file_tmp = $_FILES[‘pdf_file’][‘tmp_name’]; move_uploaded_file($file_tmp,”./pdf/”.$file_name); } else{ echo “<br> file not uploaded!!”; } $sql = “INSERT INTO student(id,name,phone,image) VALUES(NULL,’$name’,’$phone_no’,’$file_name’)”; $query = mysqli_query($con, $sql); if($query){ echo “form data uploaded successfully!!”; echo “<br>”; echo “<br> Student Name: $name”; echo “<br> Student Phone No: $phone_no”; } ?> |

File – phpmyadmin
