0

I have been trying to figure this out for hours, I have created a database ( MySql/PHPMyadmin) and i am trying to get user input stored to be able to call back up, however the info is not making it/ saving it to the database, everything shows up okay except this part of code:

$registered = mysqli_affected_rows ($dbc);

        echo $registered. "Row is affected";

when run gives me a display of -1 row, I believe this to be a big part of the problem as everything else seems to work okay. I am a complete beginner so could you guys tell me how the best way of debugging this is.

$dbc = $dbc = mysqli_connect ($hostname, $username, $password, $dbname) OR die("Could not Connect");

To input the data to the db i have the following:

if ($_SERVER['REQUEST_METHOD'] == 'POST'){

        $comments = $_POST ['Comments'];

        if (!empty ($comments)){

        include ('mysqldb.php');

        mysqli_query ($dbc,"INSERT INTO 'User-Comments' (Comments) VALUES ('$comments')");

        $registered = mysqli_affected_rows ($dbc);

            echo $registered. "Row is affected";

        }else {
                echo "Nothing Submitted";
            }

        }
3
  • 1
    How do u put data into db? Give us ur code Commented Dec 28, 2014 at 21:52
  • $dbc is the variable i created in the connection file to connect to the database Commented Dec 28, 2014 at 21:53
  • 1
    Can you echo your SQL query, copy it and paste into phpmyadmin and see what's going on? Commented Dec 28, 2014 at 21:57

2 Answers 2

1

The line:

mysqli_query ($dbc,"INSERT INTO 'User-Comments' (Comments) VALUES ('$comments')");

should be:

mysqli_query ($dbc,"INSERT INTO `User-Comments` (Comments) VALUES ('$comments')");

Notice the change in the apostrophe character ( ` ) around your table name.

Sign up to request clarification or add additional context in comments.

1 Comment

I didnt notice this syntax error untill now, it was part of the problem
0

An excerpt from the documentation for function mysqli_stmt_affected_rows(): -1 indicates that the query has returned an error.

You should check the value returned by mysqli_query(). If it returns FALSE then you can get details about the reason (error message) by using function mysqli_error().

2 Comments

Eagle-eye @kRiZ was able to identify the error without running the code. :-)
The problem was this "INSERT INTO 'User-Comments' (Comments) VALUES ('$comments')"); it should have been "INSERT INTO 'User-Comments' (Usercom) VALUES ('$comments')"); but yes wouldnt have noticed the backtick for a very long time after...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.