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.
I have the following tables:
Players (p_id PK , name)
Games (game_id PK , p_id FK ) -> p_id foreign key with players.p_id
Winners (game_id FK , p_id FK ) -> game_id FK with games.game_id, p_id FK with players.p_id
For example, I create a game with 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:
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:
string(55) "INSERT INTO billiard_winners (id,winner) VALUES ('','')"
That means it is not reading the $game & $option for some reason. But why?
I think I know where the mistake is.
My method to INSERT is:
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. Is that true?
You should map your element names with $_POST, recheck that,
You should avoid using element tags as property names of elements(eg should avoid option as name of select).
Use print_r to debug posted data in 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.