Really scary PHP, and finding those who can recognise it

The following is a fairly typical script that might be written by a well-meaning but naïve new coder. I used it as a brief technical test recently while interviewing candidates for a senior developer role, giving the scenario that a friend of theirs had just taken up PHP and wanted advice on the functionality, coding style and safety of the script.

What advice, I asked, would they provide this hypothetical, and thick-skinned, novice?

Some of them did quite well; two did really well and got themselves hired (after further interviews and tests).
Others just scared me with how long they'd be coding in oblivious bliss.

Read through the script and see what you can spot. Hovering over the text portion of each line should (works in FF anyway) bring up my annotations. They are not intended to be complete, or I'd be here all day.

Printing this page will show all annotations inline.

The source of this page is probably not very clean as I hacked it up from the .phps filter. The original file is here.

© Copyright Richard George (richard@phase.org) 2006. If you want to use it, contact me.

<?
#This might need some fixing...

echo '<html><head><title>Guestbook 0.00000001(a)</title></head>';
echo '<body>';
echo '<h1>Guestbook</h1>';

if(!$mailto) {
    $mailto='webmaster@localhost';
}

if($action=='store') {
    mysql_connect('localhost','realuser','asdhjskdfnjsn');
    mysql_query("insert into guestbook (name,email,subject,body) values (\"$name\",\"$email\",\"$subject\",\"$body\")");
    echo 'Thanks for your comment!';    
    mail($mailto,'New guestbook entry: '.$subject,$body,"From: robomail@mydomain.com");
}

if($action=='add') {
    echo "<form action=\"$_SERVER[PHP_SELF]\">";
    echo "<input type=\"hidden\" value=\"store\" name=\"action\">";
    echo "Name: <input name=\"name\" ><br />";
    echo "Email: <input name=\"email\" ><br />";
    echo "Subject: <input name=\"subject\" ><br />";
    echo "<textarea name=\"body\" ></textarea>";
    echo "<input type=\"submit\" >";
    echo "</form>";
}

if(!$action) {
    mysql_connect('localhost','realuser','asdhjskdfnjsn');
    $res=mysql_query("select * from guestbook");
    while($comment=mysql_fetch_array($res)) {
        echo "<h2>$comment[3]</h2>";        
        echo "<p>Posted by $comment[1] (<a href=\"mailto:$comment[2]\">$comment[2]</a>)</p>";        
        echo "<p>$comment[4]</p>";        
        $i++;
    }
    echo("$i comments found. <a href=\"$_SERVER[PHP_SELF]?action=add\">add one</a>");
}

echo'</body>';
echo'</html>';
?>