Gotham Gotham - 7 months ago 11
SQL Question

I am unable to link my html forms to mysql table?

I have created a forms page named "employee.php" for taking in user data. Also I have another file named SQLConnectionProcess.php which contains the code for linking forms in employee.php to sql table. The name of the database is "employee information" and the table's name is "employee info". I am using phpmyadmin and XAMPP for local server testing.

employee.php code:

<html>
<body>

<form name="EmployeeDatabase" action="SQLConnectionProcess.php" method="post">

<link rel="stylesheet" href="css.css">

<h1>EMPLOYEE DATABASE</h1>

Employe Card NO: <input type="text" name="cardNO" ><br><br>
Employee NO: <input type="text" name="employeeNO" ><br><br>
Employee Name: <input type="text" name="employeename"><br><br>
Nationality: <input type="text" name="nationality"><br><br>
Profession: <input type="text" name="profession"><br><br>
DOB: <input type="text" name="DOB"><br><br>
DOJ: <input type="text" name="DOJ"><br><br>
DOA(VisitVisa): <input type="text" name="DOA"><br><br>
Company Code: <input type="text" name="companycode"><br><br>
Sponsor Code: <input type="text" name="sponsorcode"><br><br>
Visa Type: <input type="text" name="visatype"><br><br>
Status: <input type="text" name="status"><br><br>

<input type="submit" name="formSubmit" value="Submit">

</form>

</body>
</html>


SQLConnectionProcess.php code:

if(isset($_POST['formSubmit'])){
$cardNO= $_POST['cardNO'];
$employeeNO= $_POST['employeeNO'];
$employeename= $_POST['employeename'];
$nationality= $_POST['nationality'];
$profession= $_POST['profession'];
$DOB= $_POST['DOB'];
$DOJ= $_POST['DOJ'];
$DOA= $_POST['DOA'];
$companycode = $_POST['companycode'];
$sponsorcode= $_POST['sponsorcode'];
$visatype= $_POST['visatype'];
$status= $_POST['status'];
mysqli_connect('localhost','root','password','employee information');
$sql = sprintf("INSERT INTO table_employee info(Employee Card NO,Employee NO,Employee Name,Nationality,Profession,DOB,DOJ,DOA(VisitVisa),Company Code,Sponsor Code,Visa Type,Status) VALUES ('','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",$cardNO,$employeeNO,$employeename,$nationality,$profession,$DOB,$DOJ,$DOA,$companycode,$sponsorcode,$visatype,$status);
mysqli_query($sql);


But when I submit my forms from employee.php I get redirected to a new page which displays all the code in SQLConnectionProcess.php. I am unable to figure out the coding error. Kindly help me.....

Answer

Use below code.

SQLConnectionProcess.php

<?php
if(isset($_POST['formSubmit'])){
  $cardNO= $_POST['cardNO'];
  $employeeNO= $_POST['employeeNO'];
  $employeename= $_POST['employeename'];
  $nationality= $_POST['nationality'];
  $profession= $_POST['profession'];
  $DOB= $_POST['DOB'];
  $DOJ= $_POST['DOJ'];
  $DOA= $_POST['DOA'];
  $companycode = $_POST['companycode'];
  $sponsorcode= $_POST['sponsorcode'];
  $visatype= $_POST['visatype'];
  $status= $_POST['status'];
  $con = mysqli_connect('localhost','root','','employee information');
  $sql = sprintf("INSERT INTO table_employee info(Employee Card NO,Employee NO,Employee Name,Nationality,Profession,DOB,DOJ,DOA(VisitVisa),Company Code,Sponsor Code,Visa Type,Status) VALUES ('','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",$cardNO,$employeeNO,$employeename,$nationality,$profession,$DOB,$DOJ,$DOA,$companycode,$sponsorcode,$visatype,$status);
  mysqli_query($con,$sql);
}
?>
Comments