1

I'm going to get right to the problem. A line in my code seems to be acting up. It returns with the else and doesn't seem to want to run it. There is no errors in it but it just won't move it to the database. As the title says, it is a mysqli_query in an if statement. Here is the code, I have put arrows to where the errors seems to be.

        $t_name = $_POST['topic_name'];
        $content = $_POST['con'];
        $date = date("y-m-d");

        if(isset($_POST['submit'])) {
            if($t_name && $content) {
                if(strlen($t_name) >= 10 && strlen($t_name) <= 70) {
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
                    if($query = mysqli_query($connect, "INSERT INTO topics('topic_id', 'topic_name', 'topic_content', 'topic_creator', 'date') VALUES ('', '".$t_name."', '".$content."', '".$_SESSION["username"]."', '".$date."')")) {
                        echo "<font color = 'green'>Topic posted!</font>";
                    } else {
                        echo "<font color='red'>Unknown failure, time to panic!</font>";
                    }
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                } else {
                    echo "<font color='red'>Topic name must be between 10 and 70 characters long!</font>";
                }
            } else {
                echo "<font color='red'>All fields must be filled out!</font>";
            }
        }
    } else {
        include('header.php');
        echo "You must be logged in to access this page.  Click <a href='login.php'>here</a> to login.";
    }
    include('footer.php');

Yes, I am aware of the dangers of SQL injection and other things, I will deal with it once I have my codes down.

4
  • have you tried to remove the topic id column from the insert? Commented Apr 24, 2016 at 5:56
  • 1
    oh, and leave mysql deal with the date! use the "now()" function!! dev.mysql.com/doc/refman/5.5/en/… Commented Apr 24, 2016 at 5:57
  • 1
    @HizerCache Never knew that, thanks! Commented Apr 24, 2016 at 6:05
  • ;) I've been in troubles sometime ago with dates and php, since then, when I can, I'm using MySQL native functions! Commented Apr 24, 2016 at 6:06

2 Answers 2

3

Please remove the quotes for every column name: 'topic_id', 'topic_name' ....'date'

 $query = mysqli_query($connect, "INSERT INTO topics(topic_id, topic_name, topic_content, topic_creator, date) VALUES ('', '".$t_name."', '".$content."', '".$_SESSION["username"]."', '".$date."')")

In order to understand your errors, I'd suggest you to debug your query. Try using mysqli_error() function.

if (...) {

} else {
    die( mysqli_error($connect)); // Add this
}
Sign up to request clarification or add additional context in comments.

Comments

2

Your topic_id can't be set to null. Instead remove topic_id or give it a default value.

Also, like @HizerCache mentioned, you can insert the current time using MySQL function NOW().

Something like this:

"INSERT INTO topics(topic_id, topic_name, topic_content, topic_creator, date) VALUES (default, '".$t_name."', '".$content."', '".$_SESSION["username"]."', NOW())"

Also, use mysqli_error() to debug errors.

if($query = mysqli_query($connect, "INSERT INTO topics('topic_id', 'topic_name', 'topic_content', 'topic_creator', 'date') VALUES ('', '".$t_name."', '".$content."', '".$_SESSION["username"]."', '".$date."')")) {
    echo "<font color = 'green'>Topic posted!</font>";
} else {
    echo "Error : " . mysqli_error($connect);
}

5 Comments

On the server, I have the topic_ID to be auto increment. Shouldn't that be enough?
Yes, default will work. Or you can just leave it out as @ObjectManipulator mentioned
@SkrillexNukehulk By passing a ' ', it's telling MySQL to insert an empty string which it can't. Use default and assign topic_id to its default value
It makes sense now, I will change it.
@SkrillexNukehulk please select a best answer

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.