关闭html标签

时间:2015-09-16 13:33:33

标签: php html

我有一个PHP注册表单,每次我在表单中输入一个名称和密码时,它会在第52行"意外结束$ end。索引的代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>

<form action="handler.php" method="post">
<h1>Register</h1>
<input type="text" name="username" /><br />
<input type="password" name="password" /><br />
<input type="submit" value="submit" />
</form>

</body>
</html>

处理程序:

<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>

<?php

$username = $_POST['username'];
$password = $_POST['password'];
$counter = 0;

class MyDB extends SQLite3
   {
      function __construct()
      {
         $this->open('db/test.db');
      }
   }
   $db = new MyDB();

$sql =<<<EOF
    SELECT username from users WHERE username='$username';
EOF;

$ret = $db->query($sql);
while($row = $ret->fetchArray(SQLITE3_ASSOC)){
    $counter++;
}

if($counter >= 1){
    echo "user already exists. click <a href='index.php'>here </a> to go back";
}

if($counter = 0){
    $_SESSION['username'] = $username;
    $_SESSION['password'] = $password;
    $sql2 =<<<EOF
    INSERT INTO users(ID, username, password) VALUES (null, '$username', '$password');
    EOF;
    header('Location: index.php');
}

?>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

作为documentation mentions,heredoc结束标记不能缩进,或者实际上不能包含任何其他字符。

所以你的第二个EOF应该是这样的:

if($counter == 0){ //Need to validate if the counter is 0
    $_SESSION['username'] = $username;
    $_SESSION['password'] = $password;
    $sql2 =<<<EOF
    INSERT INTO users(ID, username, password) VALUES (null, '$username', '$password');
EOF;
    header('Location: index.php');
}

(另请注意jqheart建议的$counter == 0更改)

(事实上,对于这个特殊情况,我认为最好使用常规字符串,因为它会使代码更清晰)