2

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.

3
  • Are you sure it is getting into the first if statement? echo something to make sure you are even getting that far because I suspect you are not Commented Mar 17, 2014 at 4:54
  • @Chitowns24 Thank you for finding me another error : IT ' doesen't echo , what do I do? Commented Mar 17, 2014 at 4:56
  • Let's echo $_SERVER['REQUEST_METHOD'] because === means exactly equal and it may not just be 'post' Commented Mar 17, 2014 at 4:57

1 Answer 1

2

try change your controller to:

<?php
include 'config.php';
include 'views/add.view.php';

    if ($_POST) {
        $conn = new PDO('mysql:host=localhost;dbname=list', $config['username'], $config['password']);
        $title = $_POST['title'];
        $body = $_POST['body'];
       if (empty($title) || empty($body))
       {
           $status = "<h3>Enter Values</h3>";
       }
       else
       {
           $stmt = $conn->prepare('INSERT INTO listdata(title, body) VALUES(:title, :body)');
           $stmt->execute(array(':title'=>$title, ':body'=>$body));
           $status =  "<h3 message='id'>Added<h3>";
       }
       echo $status;
}

?>
Sign up to request clarification or add additional context in comments.

3 Comments

Instead of checking the request method, he checked to see if there were any POST variables
thats why i used simple $_POST check,
<pre><code>if ($_POST)</code></pre> , this is why is still code

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.