I'm building an element for a website which I cant seem to get working. The page is supposed to allow the user to upload some news, which is displayed elsewhere. The display element is working fine, however the submission part is not.
After filling out the HTML form on the site, and clicking submit, I get an error:
Error: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null
I understand this is a data handling error, but I cant seem to find the source. If I change $_POST["title"]
to an actual value, it'll skip that part, and give me the same error, but for the next field. If I replace each $_POST
with a value, the statement will work. This, I feel, is an issue with the transmission of data from the HTML page to the PHP page. There is no JS involved, the server is on a local network, and the exact same server is running an older version of the site which does not use prepared statements perfectly fine.
Heres the PHP code:
<?php
$servername = "localhost";
$username = "myUsernameHere";
$password = "myPasswordHere";
$dbname = "myDBnameHere";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// prepare sql and bind parameters
$stmt = $conn->prepare("INSERT INTO home_news (title, body, author, datePost, dateValid)
VALUES (:title, :body, :author, :datePost, :dateValid)");
$stmt->bindParam(':title', $title);
$stmt->bindParam(':body', $body);
$stmt->bindParam(':author', $author);
$stmt->bindParam(':datePost', $datePost);
$stmt->bindParam(':dateValid', $dateValid);
// insert a row
$title = $_POST["title"];
$body = $_POST["body"];
$author = $_POST["author"];
$datePost = $_POST["dateP"];
$dateValid = $_POST["dateV"];
$stmt->execute();
echo "New records created successfully";
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
And heres my HTML code:
<form action="submitNews.php" method="POST">
<table class="subTable" align="center">
<tr>
<td><input type="text" name="title" id="newsTitle" placeholder="News Title"></td>
<td><input type="text" name="author" id="newsAuth" placeholder="Author"></td>
</tr>
<tr>
<td colspan="2"><textarea form="news" name="body" cols="42" rows="5" placeholder="Main Body"></textarea></td>
</tr>
<tr>
<td><label>When should this news be visible from?</label><br /><input type="date" name="dateV" id="newsDateV" value="<?php echo date('Y-m-d'); ?>"></td>
<td><label>When should this news be no longer visible?</label><br /><input type="date" name="dateP" id="newsDateP" value="<?php echo date('Y-m-d'); ?>"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit"></td>
<td></td>
</tr>
</table>
</form>
I imagine its something silly I've overlooked, but I'd really appreciate any input. I understand there is probably obsolete code (like <font>
) but for my project its not too big of a deal. I'm still learning how to use prepared statements.