mysql - unable to insert details into table through INSERT query with PHP -


i have application form. when trying enter details through form error pop ups saying "duplicate field '' key 'empuser'" , whenever tried enter different username condition "username existed" executing. have tried possible solutions not rectify problem. appreciated. thank you. here 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 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 empdetails empuser='$empuser'");     $row = mysqli_fetch_row($res);     if ($row > 0) {         echo "username $empuser exists";     } else {           //inserting data 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> 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

as mentioned in comments, need 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 pass parameters 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')"; 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -