为什么我的插入声明不起作用?

时间:2015-11-14 16:00:26

标签: php database mysqli

我在HTML文档中有一个简单的PHP脚本。扩展名为.php,但我无法弄清楚为什么它不会从表单输入中将数据插入表中,我的表结构如下:

UserID : int, primary key, auto increment
Firstname : varchar 
Lastname : varchar
Username : varchar
Password : varchar
DateRegistered : timestamp default - current_timestamp
DateUpdated : timestamp, attributes - on update current_timestamp default 0000-00-00 00:00:00

我的数据库连接凭据正确无误。请协助。

<!Doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Registration Page</title>
</head>
<body>
    <form id="register_form" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
    <p>
        <label for="first_name">First Name</label>
        <input type="text" name="first_name">
    </p>
    <p>
        <label for="last_name">Surname</label>
        <input type="text" name="last_name">
    </p>
    <p>
        <label for="username">Username</label>
        <input type="text" name="username">
    </p>
    <p>
        <label for="password">Password</label>
        <input type="text" name="password">
    </p>
    <p>
        <input type="submit" name="registration_submit" value="Register">
    </p>
    </form>

</body>
</html>

<?php 

if (isset($_POST['registration_submit']))
{

    $fname = $_POST['first_name'];
    $lname = $_POST['last_name'];
    $uname = $_POST['username'];
    $pword = $_POST['password'];

    if (empty($fname) || empty($lname) || empty($uname) || empty($pword))
    {
        echo "Required fields missing";
    }
}
else if (!empty($_POST['first_name']) && !empty($_POST['last_name']) && !empty($_POST['username']) 
    && !empty($_POST['password']) ) 
    {
        $fname = $_POST['first_name'];
        $lname = $_POST['last_name'];
        $uname = $_POST['username'];
        $pword = $_POST['password'];
        echo "ElseIF portion reached";

        $con = new mysqli_connect("localhost", "root", "", "website2");
        $query = "insert into users values(NULL,'$fname','$lname','$uname','$pword',NULL, NULL)";
        $result = $con->query($query);
        if (!$result) die("something went wrong ". $con->error);
        $result->close();
        $con->close();
        echo "<br /> User Registered";
    }
?>

2 个答案:

答案 0 :(得分:0)

由于isset($_POST['registration_submit'])的条件不合适,每当您提交表单时,它都符合if语句,因此您无法执行else if语句。

答案 1 :(得分:0)

尝试

if (isset($_POST['registration_submit']))
{

$fname = $_POST['first_name'];
$lname = $_POST['last_name'];
$uname = $_POST['username'];
$pword = $_POST['password'];

if (empty($fname) || empty($lname) || empty($uname) || empty($pword)) {
    echo "Required fields missing";
    exit(); //just to be safe that below else statement does not gets executed if variable is empty.
}else{
    //you have already did your argument above if data exists, so we don't need to do this argument again (!empty($_POST['data');
    $con = new mysqli_connect("localhost", "root", "", "website2");
    $query = "insert into users values(NULL,'$fname','$lname','$uname','$pword',NULL, NULL)";
    $result = $con->query($query);
    if (!$result) die("something went wrong ". $con->error);
    $result->close();
    $con->close();
    echo "<br /> User Registered";
}
?>