PHP注册脚本,插入数据库不起作用

时间:2013-11-01 09:25:34

标签: php mysql

我的网站上的注册表单的PHP代码存在问题。提交表单时,有些代码无法执行。

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";
    }       
}

一切正常,直到它开始$ flp =“INSERT INTO ...

任何人都可以帮助我调试此代码吗,请不要链接到我希望能够使用此代码的已编写代码。

编辑:

我更改了一行以故意导致错误,因此我知道PHP正在读取该行,并且它确实给出了启动$ res = mysqli _...

行的语法错误

“解析错误:语法错误,意外'$ res'(T_VARIABLE)在第85行的C:\ XamppNew \ htdocs \ site \ regusr.php”

我在插入行的末尾删除了分号,只是为了得到错误。

编辑:

我设法将问题隔离到if语句的开头。似乎该行不会将每个错误视为没有内容。但是,如果错误存在,它将显示在表单旁边的页面上,并且不会显示此类错误。

4 个答案:

答案 0 :(得分:0)

您需要引用(使用反引号)列名Email Address,因为它有一个空格。

答案 1 :(得分:0)

Email Address字段中使用反引号,因为它有空格。

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

答案 2 :(得分:0)

试试这个..........        $ flp =“INSERT INTO $ tblname(名称,用户名,密码,EmailAddress)         VALUES         ( ' “$名”。', ' “$用户。”', ' “$通。”','” $电子邮件。 “')”;

答案 3 :(得分:0)

试试这个:

<?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();
//}

?>