简体   繁体   中英

PHP Registration Script, Inserting to Database not working

I have an issue with my PHP code for a registration form on my website. Some of the code doesn't get executed when the form is submitted.

if ($fnameError = "" && $emailError = "" && $userError = "" && $passError = "" && $cpassError = "" && $tickError = "") {
    $con = mysqli_connect("localhost","root","","login")or die("Can't connect to database");

    $user = $_POST['user'];
    $pass = $_POST['pass'];
    $name = $_POST['fname'];
    $email = $_POST['email'];

    $pwd = crypt('$pass', '$user');
    $pwd = md5($pwd);
    $tblname = "users";
    $flp="INSERT INTO $tblname (Name, Username, Password, Email Address)
        VALUES
        ('$name','$user','$pass','$email')";
    $res = mysqli_query($con, $flp)or die("Can't insert to table");
    if ($res) {
        $complete = "Registered successfully please log in to continue";
    } else {
        echo "error";
    }       
}

Everything works fine until it gets to the line starting $flp="INSERT INTO...

Can anyone assist in helping me debug this code, also, please don't link to already written code I want to be able to use this code.

EDIT:

I changed a line to purposely cause an error so I know PHP is reading the line and it does give me the syntax error for the line starting $res=mysqli_...

"Parse error: syntax error, unexpected '$res' (T_VARIABLE) in C:\\XamppNew\\htdocs\\site\\regusr.php on line 85"

I removed the semi-colon at the end of the Insert line just to get the error.

EDIT:

I've managed to isolate the problem to the start of the if statement. It seems to be that the line doesn't treat each error as having no content. However, if the error exists it will be displayed on the page next to the form and no such error gets displayed.

你需要引用(反引号与)你的列名Email Address ,因为它有它的空间。

Email Address字段中有空格,请在其中使用反引号。

$flp="INSERT INTO $tblname (`Name`, `Username`, `Password`, `Email Address`)

尝试这个.......... $ flp =“ INSERT INTO $ tblname(Name,Username,Password,EmailAddress)VALUES('”。$ name。“','”。$ user。“',' “。$ pass。”','“。$ email。”')“;

Try this:

<?php

//if ($fnameError = "" && $emailError = "" && $userError = "" && $passError = "" && $cpassError = "" && $tickError = "")
//{
    // Connect to DB
    $mysqli = new mysqli("localhost", "root", "", "login");
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s\n", $mysqli->connect_error);
        exit();
    }

    // Parse Input
    $user = $mysqli->real_escape_string($_POST['user']);
    $pass = $mysqli->real_escape_string($_POST['pass']);
    $pwd = md5(crypt('$pass', '$user'));
    $name = $mysqli->real_escape_string($_POST['fname']);
    $email = $mysqli->real_escape_string($_POST['email']);

    // Insert Record
    if ($mysqli->query("INSERT INTO users (`Name`, `Username`, `Password`, `Email Address`) VALUES ('$name', '$user', '$pwd', '$email')")) {
        printf ("New user has id %d.\n", $mysqli->insert_id);
    } else {
        printf("Failed to insert row: %s\n", $mysqli->error);
    }

    // Close DB Connection
    $mysqli->close();
//}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM