PHP注册表不起作用(对于某些变量)

时间:2015-10-24 09:54:55

标签: php html

嘿,我的注册表单的PHP代码不适用于某些var,但一切看起来都不错。

用户名,密码和电子邮件工作正常,我还有另一个页面可以将数据添加到数据库中,并且使用相同的代码完美地工作。

提前致谢。

<?php if (isset($_POST['submit'])){

$username = $_POST['username'];
$sql = "SELECT * FROM members WHERE username='$username'";
echo $sql;
$result = mysqli_query($con, $sql) or die(mysqli_error($con));
$num = mysqli_num_rows($result);

if ($num >= 1){
    $_SESSION['userError']='Username Already Taken';

}else{
    $username = $_POST['username'];
    $password = $_POST['password'];
    $password = hash('sha256', $password);
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $email = $_POST['email'];
    $address = $_POST['address'];
    $contact = $_POST['contact'];

    $sql2 = "INSERT INTO members(username, password, fname, lname, email, address, contact) VALUES ('$username', '$password', '$fname', '$lname', '$email', '$address', '$contact')";
    mysqli_query($con, $sql) or die(mysqli_error($con));
    echo $sql2;
    $_SESSION['message']="Account Added Successfully";  }} ?>

以下是它链接的表单。

<form class="pure-form reg-form" action="" method="post">
                                    <fieldset class="pure-group" style="float:left">
                                        <input type="text" class="pure-input-1" name="username" value="" placeholder="Username" autofocus/>
                                        <input type="password" class="pure-input-1" name="password" value="" pattern="(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{4,}" placeholder="Password"/>
                                        <input type="text" class="pure-input-1" name"fname" value="" placeholder="First Name"/>
                                        <input type="text" class="pure-input-1" name"lname" value="" placeholder="Last Name"/>
                                    </fieldset>
                                    <fieldset class="pure-group" style="float:right">
                                        <input type="email" class="pure-input-1" name="email" value="" placeholder="E-mail"/>
                                        <input type="text" class="pure-input-1" name"address" value="" placeholder="Address"/>
                                        <input type="text" class="pure-input-1" name"contact" value="" placeholder="Contact"/>
                                        <input type="submit" class="pure-button pure-input-1 pure-button-primary" name="submit" value="Submit">
                                    </fieldset>   
                                </form>

2 个答案:

答案 0 :(得分:0)

从某些输入标签

中检查HTML您遗失的=

您的密码:

<form class="pure-form reg-form" action="" method="post">
                                <fieldset class="pure-group" style="float:left">
                                    <input type="text" class="pure-input-1" name="username" value="" placeholder="Username" autofocus/>
                                    <input type="password" class="pure-input-1" name="password" value="" pattern="(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{4,}" placeholder="Password"/>
                                    <input type="text" class="pure-input-1" name"fname" value="" placeholder="First Name"/>//missing = 
                                    <input type="text" class="pure-input-1" name"lname" value="" placeholder="Last Name"/>//missing = 
                                </fieldset>
                                <fieldset class="pure-group" style="float:right">
                                    <input type="email" class="pure-input-1" name="email" value="" placeholder="E-mail"/>
                                    <input type="text" class="pure-input-1" name"address" value="" placeholder="Address"/> //missing = 
                                    <input type="text" class="pure-input-1" name"contact" value="" placeholder="Contact"/>  //missing = 
                                    <input type="submit" class="pure-button pure-input-1 pure-button-primary" name="submit" value="Submit">
                                </fieldset>   
                            </form>

另外,请检查您PHP代码正在sql2查询并执行sql

改变:

   $sql2 = "INSERT INTO members(username, password, fname, lname, email, address, contact) VALUES ('$username', '$password', '$fname', '$lname', '$email', '$address', '$contact')";
mysqli_query($con, $sql) or die(mysqli_error($con));

到:

$sql2 = "INSERT INTO members(username, password, fname, lname, email, address, contact) VALUES ('$username', '$password', '$fname', '$lname', '$email', '$address', '$contact')";
mysqli_query($con, $sql2) or die(mysqli_error($con));

答案 1 :(得分:0)

您编写查询但不执行查询。

第22行

python -m pip list

将$ sql更改为$ sql2