Tolkosino
Tolkosino

Reputation: 43

Mysqli Insert is not working. No error poping up

I'm having a bit of problem with my Mysqli Insert here. I put a try block around the insert to catch possible errors, I'm getting the notice that it worked, but no data was inserted.

Here's the Code:

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

To be sure that my code will be committed, I put a $mysqli->commit();

Upvotes: 1

Views: 1257

Answers (2)

uautkarsh
uautkarsh

Reputation: 502

Can you try the following for error checking:

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

Also, in your query why do you have the first $username? I believe it should be your column name, with the following format:

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

Upvotes: 0

Gmnayem
Gmnayem

Reputation: 111

Your column name should not contain '$' sign.Use this instead.

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

Hope it helps you.

Upvotes: 1

Related Questions