I'm having trouble getting the information submitted in my form to post into my database. I'm not sure what the issue is, no error is logged, but I get the message "There was an error that prevented the comment from being saved." which is what I set up as my failure message in my php code, so something's not working.
I've made a form like this only once before, so I'm not great at troubleshooting. I have three things that need to be submitted when the form posts: author, body, and the page_name (which is a hidden input type in the form that sends the current web address).
Here is the php code that submits the form:
<?php
if (isset($_POST['submit'])) {
$required_fields = array("author", "body");
validate_presences($required_fields);
if (empty($errors)) {
$author = mysql_prep($_POST['author']);
$body = mysql_prep($_POST['body']);
$page_name = ($_POST['page_name']);
$query = "INSERT INTO comments (";
$query .= " $author, $body, $page_name";
$query .= ") VALUES (";
$query .= " '{$author}', '{$body}', '{$page_name}'";
$query .= ")";
$result = mysqli_query($connection, $query);
if ($result) {
redirect_to("new_admin.php");
} else {
// Failure
$_SESSION["message"] = "There was an error that prevented the comment from being saved.";
}
}
} else {
$author = "";
$body = "";
}
?>
Here's the functions I use in the above code (it's stored on a separate page that's called at the beginning of the main page):
function mysql_prep($string) {
global $connection;
$escaped_string = mysqli_real_escape_string($connection, $string);
return $escaped_string;
}
function redirect_to($new_location) {
header("Location: " . $new_location);
exit;
}
$connection is what I use for my database connection, which I've double checked is working
And here is the code for my form:
<form action="new_admin.php" method="post">
<input type="hidden" name="page_name" value="<?=$_SERVER['REQUEST_URI']?>" />
<table>
<tr>
<td>Your name:</td>
<td><input type="text" name="author" value="<?php echo $author; ?>" /></td>
</tr>
<tr>
<td>Your comment:</td>
<td><textarea name="body" cols="40" rows="8"><?php echo $body; ?></textarea></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Submit Comment" /></td>
</tr>
</table>
</form>
I think that's everything. I'm just stumped right now and I don't know if it's something stupid I'm missing or if I did something bigger and set the whole thing up wrong.
Any help would be super appreciated!!