从表单插入数据库(Silex、postgreSQL、PHP)

从表单插入数据库(Silex、postgreSQL、PHP),php,postgresql,symfony,heroku,Php,Postgresql,Symfony,Heroku,我正在Heroku申请。我最终想要创建一个用户注册页面,但我在整理语法方面遇到了困难,所以现在我只想要一个页面,列出数据库Postgres中的所有名称,有一个打开的文本框,单击“注册”按钮后添加准备好的!将名称添加到数据库并刷新页面,以便现在显示该名称。我很难弄明白怎么做。我可以显示名称,但不知道如何从文本框中添加名称 在my index.php代码中: $app->get('/db/', function() use($app) { $st = $app['pdo']->pre

我正在Heroku申请。我最终想要创建一个用户注册页面,但我在整理语法方面遇到了困难,所以现在我只想要一个页面,列出数据库Postgres中的所有名称,有一个打开的文本框,单击“注册”按钮后添加准备好的!将名称添加到数据库并刷新页面,以便现在显示该名称。我很难弄明白怎么做。我可以显示名称,但不知道如何从文本框中添加名称

在my index.php代码中:

$app->get('/db/', function() use($app) {
  $st = $app['pdo']->prepare("SELECT * FROM users  ");
  $st->execute();

  $usernames = array();
  while ($row = $st->fetch(PDO::FETCH_ASSOC)) {
    $app['monolog']->addDebug('Row ' . $row['username']);
    $usernames[] = $row;
  }
/*
{ THIS IS WHERE I WANT TO READ IN THE NEW NAME BUT I'M NOT SURE HOW
}
*/


  $st = $app['pdo']->prepare("INSERT INTO users ( username)
      VALUES ('[PUT THE NEW NAME HERE]')");
  $st->execute();


  return $app['twig']->render('database.twig', array(
    'names' => $usernames
  ));

});



$app->get('/twig/{name}', function($username) use($app) {
  return $app['twig']->render('index.twig', array(
    'username' => $username,
  ));
});


$app->run();

?>
这是我的database.twig文件:

从地方检察官那里弄到了这些行 塔巴斯:

<ul>
{% for n in names %}
  <li> {{ n.username}} at {{ n.email }}</li>
{% else %}
  <li>Nameless!</li>
{% endfor %}
</ul>

<form action="#" method="post">

 <fieldset>


<div class="form-group">

<input autofocus class="form-control" name="username" placeholder="Username" type="text"/>

</div>


<div class="form-group">

<button type="submit" class="btn btn-default">Register</button>

</div>

</fieldset>

</form>

<div>
    or <a href="login.php">log in</a> for an account
</div>
/**
  * The best option is to use HttpFoundation to catch parameters either   
  * via POST or GET. 
  * So, if you're using httpFoundation (the default behaviour with 
  * Silex) change the header like:
  */
$app->('/db/', function() use($app, Request $request){
    // rest of the code inside
});

/** for catching the new username with HttpFoundation is as easy as   
  * asking for the all or as in this case for just one field:
  */
$username = $request->request->get('username');

/**
  * Once you have the username, you can insert it into the query, but 
  * it is a nice idea to
  * bind it just to avoid sql injection, between modify
  */
$stmt = $app['db']->prepare('INSERT INTO users (?)');
$stmt->bindValue(1, $username, PDO::PARAM_INT);