【发布时间】:2015-06-25 05:31:10
【问题描述】:
我在 wordpress 的一页上创建了表单:
<form action="scores_send.php" method="post">
<strong>Date: </strong><input type="date" name="date" /><br />
<strong>TEAM 1:</strong>
Player 1.: <input type="text" name="team1_player1" /><br />
Player 2.: <input type="text" name="team1_player2" /><br />
<strong>TEAM 2:</strong>
Player 1.: <input type="text" name="team2_player1" /><br />
Player 2.: <input type="text" name="team2_player2" /><br />
<strong>SCORE:</strong>
Team 1. <input type="number" name="score_team1" min="0" max="5"/> : <input type="number" name="score_team2" min="0" max="5"/> Team 2.<br />
<input type="submit" value="Add" />
</form>
我还编写了连接到我的外部(无 wordpress)postgres 数据库的 php 代码(代码中的连接参数不正确)并将信息从表单插入到一个表中:
<?php
$date = $_POST['date'];
$t1p1 = $_POST['team1_player1'];
$t1p2 = $_POST['team1_player2'];
$t2p1 = $_POST['team2_player1'];
$t2p2 = $_POST['team2_player2'];
$score1 = $_POST['score_team1'];
$score2 = $_POST['score_team2'];
if($date and $t1p1 and $t1p2 and $t2p1 and $t2p2 and $score1 and $score2) {
$connection = pg_connect("host=127.0.0.1 port=5432 dbname=scores user=user password=pass")
or die('Brak polaczenia z serwerem PostgreSQL');
$db = @pg_select_db('scores', $connection)
or die('Nie moge polaczyc sie z baza danych');
$team1 = @pg_query("INSERT INTO scores.games (score_team1, score_team2, datetime, team1_player1, team1_player2, team2_player1, team2_player2) VALUES ('$score1', '$score2', '$date', '$t1p1', '$t1p2', '$t2p1', '$t2p2'");
if($team1) echo "Scores added.";
else echo "ERROR! Scores not added";
pg_close($connection);
}
?>
我尝试了两种方法: 1.在我的主题文件夹中创建模板php文件和 2.将此代码粘贴到wordpress中的内容编辑器(使用此插件:https://wordpress.org/plugins/insert-php/)
两种方式都行不通。我在 中更改了部分“动作”,但可能是错误的。
【问题讨论】:
-
使用($date && $t1p1 && $t1p2 && $t2p1 && $t2p2 && $score1 && $score2)
-
而不是 "if($date and $t1p1 and $t1p2 and $t2p1 and $t2p2 and $score1 and $score2)" 或者如何?我对 PHP 的了解不够 ;)
标签: php wordpress forms postgresql