[英]Getting values from select box
For my internship, I am creating a scoreboard list for the people in the break, to keep track of all the wins. 对于我的实习,我正在为休息时间的人员创建一个记分板列表,以跟踪所有获胜情况。 I am using MVC.
我正在使用MVC。
I have the following tables: 我有以下表格:
Players (p_id PK , name) 玩家(p_id PK ,名称)
Games (game_id PK , p_id FK ) -> p_id foreign key with players.p_id 游戏(game_id PK ,p_id FK ) ->与players.p_id的外键
Winners (game_id FK , p_id FK ) -> game_id FK with games.game_id, p_id FK with players.p_id 获胜者(game_id FK ,p_id FK ) -> game_id FK与games.game_id,p_id FK与players.p_id
For example, I create a game with Player1 & Player2. 例如,我用Player1和Player2创建一个游戏。 I now need to add the winner.
我现在需要添加赢家。 I made a separate page, that links to a form where you can select one of those players and Insert it into the winner's table.
我制作了一个单独的页面,该页面链接到一个表单,您可以在其中选择一个球员并将其插入获胜者的表格。
My form looks like this: 我的表格如下所示:
<form method="post" action="<?php echo ROOT ?>HomeController/winnerBilliard/" enctype="multipart/form-data">
<div class="form-group">
<label for="player_1">Select game number</label><br>
<select class="form-control" name="game_id">
<option selected value="<?php echo $selectedGame['id']; ?>"><?php echo $selectedGame[0]['id']; ?></option>
</select>
</div>
<div class="form-group">
<label for="player_1">Select winner</label><br>
<select class="form-control" name="option">
<option disabled selected value>Choose winner</option>
<option value="<?php echo $selectedGame['player_1']; ?>"><?php echo $selectedGame[0]['player_1']; ?></option>
<option value="<?php echo $selectedGame['player_2']; ?>"><?php echo $selectedGame[0]['player_2']; ?></option>
</select>
</div>
<button type="submit" class="btn btn-primary btn-block" name="create">Add winner</button>
</form>
My method looks like this: 我的方法如下所示:
public function winnerBilliard() {
if(isset($_POST["create"])) {
$game = $_POST['id'];
$option = $_POST['option'];
// var_dump($option); exit();
$query = "INSERT INTO billiard_winners (id,winner) VALUES ('$game','$option')";
$this->model->create($query);
Header('Location: ' . ROOT . 'HomeController/billiardGames');
}
}
My create method in my model looks like this: 我在模型中的create方法如下所示:
public function create($query)
{
$data = $this->db->insert($query);
return $data;
}
For some reason, it ain't reading the values. 由于某种原因,它没有读取值。 If I vardump the query in my create method, I will see this on my screen:
如果在我的create方法中查询,我将在屏幕上看到以下内容:
string(55) "INSERT INTO billiard_winners (id,winner) VALUES ('','')"
字符串(55)“ INSERT INTO billiard_winners(id,winner)VALUES('','')”
That means it is not reading the $game & $option for some reason. 这意味着由于某种原因它没有读取$ game&$ option。 But why?
但为什么?
I think I know where the mistake is. 我想我知道错误在哪里。
My method to INSERT is: 我的INSERT方法是:
public function winnerGames() {
if(isset($_POST["create"])) {
$game = $_POST['id'];
$option = $_POST['option'];
$query = "INSERT INTO winners (id,winner) VALUES ('$game','$option')";
$this->model->create($query);
Header('Location: ' . ROOT . 'HomeController/Games');
}
}
My method to get to the right page and fill the options with the values is: 我到达正确的页面并用值填充选项的方法是:
public function editGame($id) {
$getGame = "SELECT * FROM games WHERE id = $id";
$selectedGame = $this->model->readAll($getGame, $id);
include('app/views/editGame.php');
}
I think my method is trying to get the games from $selectedGame, but isn't able to look into that method. 我认为我的方法正在尝试从$ selectedGame中获取游戏,但是无法研究该方法。 Is that true?
真的吗?
You should map your element names with $_POST, recheck that, 您应该使用$ _POST映射元素名称,然后重新检查,
You should avoid using element tags as property names of elements(eg should avoid option as name of select). 您应避免将element标签用作元素的属性名称(例如,应避免将option作为select的名称)。
Use print_r to debug posted data in php. 使用print_r调试php中发布的数据。
I think you are missing trick here, 我认为您在这里缺少窍门,
$saveArr['id'] = $_POST['id'];
$saveArr['winner'] = $_POST['option'];
$this->model->create($saveArr);
Now in create method, 现在在创建方法中
public function create($saveArr){
$data = $this->db->insert($saveArr);
}
This should work. 这应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.