-4

I'm new to PDO, so I apologise if this is a silly question. I have two files: writel.html contains a simple form, with formaction="submitArticle.php" and method="post". submitArticle.php contains the following:

<?php

$con = new PDO('mysql:host=localhost;dbname=mysql','root','root');

$submission = $con->prepare("
    INSERT INTO sfWritings(title, summary, content)
    VALUES(:title, :summary, :content)
    ");

$submission_execution = $submission->execute([
    'title' => $_POST['title'],
    'summary' => $_POST['summary'],
    'content' => $_POST['content']
    ]);

if($submission_execution) {
    echo "article submitted";
}
?>

However, instead of getting either the echo message, or some sort of error, I simply get the whole code, as if it doesn't realise it's PHP or something: Screenshot

If I add some html tags (<html>, <body> etc.) then it starts giving me this: Screenshot when using html tags

The database is ok (I've been able to fetch some test data from it). And the way I'm doing the insert closely follows a (working) example for a tutorial. So I''m a bit stuck for what I'm doing wrong!

  • 4
    You have to execute it in your localhost server and not to open in through "file://" – Kostas Mitsarakis Apr 05 '16 at 21:51
  • Do you have php installed on the host system? Also the URL is accessing the page as a file not as a webpage. – ProWolf Apr 05 '16 at 21:51
  • 2
    You shouldn't immediately assume that it is a problem with pdo. If you just replace the code with `echo "hello world";` you will see that you have the same problem. – Cave Johnson Apr 05 '16 at 22:00

1 Answers1

3

You're not running this script on a server. Just loading the file directly with the browser will never work.

Have a look at: https://www.apachefriends.org/index.html