我有申请表。当尝试通过表单输入详细信息时,会弹出一个错误消息,提示“ empuser”键为“ Duplicate field”,并且每当我尝试输入其他用户名时,条件“ Username已经存在”就只会执行。我已经尝试了所有可能的解决方案,但无法解决问题。任何帮助,将不胜感激。谢谢。
这是我的代码
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>


数据库设计:

table_empdetails
1.empid int A_I PK无符号
2.empname varchar
3.给varchar发送电子邮件
4.地址varchar
5,国家varchar
6.state varchar
7.城市varchar
8.phone int无符号
9.dept varchar
10,desig varchar
11.empuser varchar唯一
12.emppass varchar

最佳答案

如评论中所述,需要更改

if (isset($_POST['pass'])) {
        $emppass = mysqli_real_escape_string($conn, md5($_POST['pass']));
    } else {
        $emppass = "";
    }




if (isset($_POST['emp'])) {
           $emppass = mysqli_real_escape_string($conn, md5($_POST['emppass']));
        } else {
           $emppass = "";
        }


这会将参数传递到查询中

$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')";

关于php - 无法使用PHP通过INSERT查询将详细信息插入表中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27245169/

10-12 15:00