Reputation: 67
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.
Upvotes: 1
Views: 1727
Reputation: 9024
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);
}
Upvotes: 2
Reputation: 8618
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
}
Upvotes: 3