Mysqli Insert无效。没有错误弹出

时间:2017-02-19 08:34:26

标签: php mysql insert

我的Mysqli Insert在这里遇到了一些问题。我在插件周围放了一个try块以捕获可能的错误,我注意到它有效,但没有插入数据。

以下是代码:

$mysqli = new mysqli("localhost", "yourusername", "somepasswdhere", "users");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}



                        //formular set ?
        if(isset($_POST['submit'])) {
            echo $_POST['username'];

            //bind post variables on local vars.
            $username = $_POST['username'];
            $fname = $_POST['fname'];
            $lname = $_POST['lname'];
            $email = $_POST['email'];
            $password = $_POST['password'];
            $cpassword = $_POST['cpassword'];

            // Heres some crap but thats not that problem so im getting
            // right into the next 
            // maybe problem part

            try{
            $mysqli->query("INSERT INTO `users` ($username) VALUES ('$username')");
            $mysqli->commit();
            echo "works";
            } catch(Exception $e) {
                echo $e->getMessage(), "\n";
            }

为了确保我的代码将被提交,我提出了$mysqli->commit();

2 个答案:

答案 0 :(得分:1)

您的专栏名称不应包含' $'签名。请改用此处。

$mysqli->query("INSERT INTO `users` (username) VALUES ('$username')");

希望它对你有所帮助。

答案 1 :(得分:0)

您可以尝试以下方法进行错误检查:

$success = $mysqli->query(<YOUR SQL QUERY>);
if (!$success) {
   print_r($mysqli->error);
}

另外,在你的查询中为什么你有第一个$用户名?我相信它应该是您的列名,格式如下:

INSERT INTO `users` (<col name>) VALUES (<value>);