I have a php script to input data to a database list in table listdata.
Here is the code for my view.
<form action="../add.php" method="post">
<label for="title">Title:</label>
<br>
<input id="title" type="text" name="title"/>
<br>
<label for="body">Body:</label>
<br>
<textarea name="body" cols="30" rows="10"></textarea>
<br>
<input id="sb" type="submit" values="Add">
</form>
and here is my controller (no framework)
<?php
include 'config.php';
include 'views/add.view.php';
if ($_SERVER['REQUEST_METHOD'] === 'post') {
$conn = new PDO('mysql:host=localhost;dbname=list', $config['username'], $config['password']);
$title = $_POST['title'];
$body = $_POST['body'];
if (empty($title) or empty($body)) {
$status = "<h3>Enter Values</h3>";
echo $status;
} else {
$stmt = $conn->prepare('INSERT INTO listdata(title,body) VALUES(:title,:body)');
$stmt->bindParam(':title',$title);
$stmt->bindParam(':body',$body);
$stmt->execute();
$status = "<h3 message='id'>Added<h3>";
echo $status;
}
}
?>
When I run it in the browser , before or after posting the values , there is no error , but the
$status variable is not being echoed and the database is not being updated.
echo $_SERVER['REQUEST_METHOD']because===means exactly equal and it may not just be 'post'