mysql - unable to insert details into table through INSERT query with PHP
Get the solution ↓↓↓I have a application form. When trying to enter details through form a error pop ups saying "Duplicate field '' for key 'empuser'" and whenever i tried to enter different username the condition "Username already existed" is only executing. I have tried all the possible solutions but could not rectify the problem. Any help would be appreciated. Thank you. Here is my code empdetails.php
<!DOCTYPE html PUBLIC "//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Employee Details</title>
<style>
#r{
color:red;
}
#g{
color:green;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="empdetails.js" type="text/javascript"></script>
</head>
<body>
<form id = "myForm" action="updatedetails.php" method="post">
<div>
<table>
<tr>
<td>Employee Name:
<input type="text" name="empname" id="empname" required/>
</td>
</tr>
</table>
</div>
<div>
<table>
<tr>
<td>Email Id:
<input type="text" name="email" id="email" required />
<span class="error" id="user-result"></span></td>
</tr>
</table>
</div>
<div>
<table>
<tr>
<td>Address:
<input type="text" name="addr" id="addr" required />
</td>
</tr>
</table>
</div>
<div>
<table>
<tr>
<td>Country
<select name="country" id="country" required>
<option selected="selected" value=""> Choose Country </option>
<option id="coun1" value="coun1">Country1</option>
<option id="coun2" value="coun2">Country2</option>
<option id="coun3" value="coun3">Country3</option>
<option id="coun4" value="coun4">Country4</option>
</select></td>
</tr>
</table>
</div>
<div>
<table>
<tr>
<td>State
<select name="state" id="state" required>
<option selected="selected" value=""> Choose State </option>
<option class="st1" value="st1">State1</option>
<option class="st1" value="st1">State2</option>
<option class="st1" value="st1">State3</option>
<option class="st1" value="st1">State4</option>
<option class="st2" value="st2">State5</option>
<option class="st2" value="st2">State6</option>
<option class="st2" value="st2">State7</option>
<option class="st2" value="st2">State8</option>
<option class="st3" value="st3">State9</option>
<option class="st3" value="st3">State10</option>
<option class="st3" value="st3">State11</option>
<option class="st3" value="st3">State12</option>
<option class="st4" value="st4">State13</option>
<option class="st4" value="st4">State14</option>
<option class="st4" value="st4">State15</option>
<option class="st4" value="st4">State16</option>
</select></td>
</tr>
</table>
</div>
<div>
<table>
<tr>
<td>City:
<input type="text" name="city" id="city" required/>
</td>
</tr>
</table>
</div>
<div>
<table>
<tr>
<td>ContactNo:
<input type="text" name="phone" id="phone" required/>
</td>
</tr>
</table>
</div>
<div>
<table>
<tr>
<td>Dept. Name:
<input type="text" name="dept" id="dept" required/>
</td>
</tr>
</table>
</div>
<div>
<table>
<tr>
<td>Designation:
<input type="text" name="desig" id="desig" required/>
</td>
</tr>
</table>
</div>
<div>
<table>
<tr>
<td>Choose Username:
<input type="text" name="empuser" id="empuser" required/>
</td>
</tr>
</table>
</div>
<div>
<table>
<tr>
<td>Choose Password:
<input type="password" name="pass" id="pass" required/>
</td>
</tr>
</table>
</div>
<div>
<table>
<tr>
<td>Re-enter Password:
<inputtype="password"name="conpass"id="conpass"onchange="checkpassword()"required/>
</td>
</tr>
</table>
</div>
<div class"formalert" id="passcheck"></div>
<div>
<table>
<tr>
<td>
<button id="submit">Register</button>
</td>
</tr>
</table>
</div>
</form>
<td>
<form action="./emplogin.php">
<input type="submit" value="Back"/>
</form></td>
<div id="ack"></div>
</body>
</html>
updatedetails.php
<html>
<head>
</head>
<body>
<?php
include '../db/dbconn.php';
//escape variables for security
if (isset($_POST['empname'])) {
$empname = mysqli_real_escape_string($conn, $_POST['empname']);
} else {
$empname = "";
}
if (isset($_POST['email'])) {
$email = mysqli_real_escape_string($conn, $_POST['email']);
} else {
$email = "";
}
if (isset($_POST['addr'])) {
$address = mysqli_real_escape_string($conn, $_POST['addr']);
} else {
$address = "";
}
if (isset($_POST['country'])) {
$country = mysqli_real_escape_string($conn, $_POST['country']);
} else {
$country = "";
}
if (isset($_POST['state'])) {
$state = mysqli_real_escape_string($conn, $_POST['state']);
} else {
$state = "";
}
if (isset($_POST['city'])) {
$city = mysqli_real_escape_string($conn, $_POST['city']);
} else {
$city = "";
}
if (isset($_POST['phone'])) {
$phone = mysqli_real_escape_string($conn, $_POST['phone']);
} else {
$phone = "";
}
if (isset($_POST['dept'])) {
$dept = mysqli_real_escape_string($conn, $_POST['dept']);
} else {
$dept = "";
}
if (isset($_POST['desig'])) {
$desig = mysqli_real_escape_string($conn, $_POST['desig']);
} else {
$desig = "";
}
if (isset($_POST['empuser'])) {
$empuser = mysqli_real_escape_string($conn, $_POST['empuser']);
} else {
$empuser = "";
}
if (isset($_POST['pass'])) {
$emppass = mysqli_real_escape_string($conn, md5($_POST['pass']));
} else {
$emppass = "";
}
$res = mysqli_query($conn, "SELECT empuser FROM empdetails WHERE empuser='$empuser'");
$row = mysqli_fetch_row($res);
if ($row > 0) {
echo "Username $empuser already exists";
} else {
//inserting data into database
$sql="INSERTINTOempdetails(empname,email,address,country,state,city,phone,dept,desig,empuser,emp)
VALUES('$empname','$email','$address','$country','$state','$city','$phone','$dept',
'$desig',
'$empuser',
'$emppass')";
if (!mysqli_query($conn, $sql)) {
die('Error: ' . mysqli_error($conn));
//error detected
} else {
echo "Records inserted sucessfully";
//no errors
}
}
?>
<br />
<br />
Click <a href="emplogin.php">here</a> to login
</body>
</html>
Database design:
table_empdetails 1.empid int A_I PK unsigned 2.empname varchar 3.email varchar 4.address varchar 5.country varchar 6.state varchar 7.city varchar 8.phone int unsigned 9.dept varchar 10.desig varchar 11.empuser varchar unique 12.emppass varchar
Answer
Solution:
As mentioned in the comments, need to change
if (isset($_POST['pass'])) {
$emppass = mysqli_real_escape_string($conn, md5($_POST['pass']));
} else {
$emppass = "";
}
To
if (isset($_POST['emp'])) {
$emppass = mysqli_real_escape_string($conn, md5($_POST['emppass']));
} else {
$emppass = "";
}
This will pass the parameters into the query at
$sql="INSERTINTOempdetails(empname,email,address,country,state,city,phone,dept,desig,empuser,emp)
VALUES('$empname','$email','$address','$country','$state','$city','$phone','$dept',
'$desig',
'$empuser',
'$emppass')";
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: installation failed, reverting ./composer.json and ./composer.lock to their original content
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.