2017-09-30 5 views
2

テーブル名は、player_id,player_evalおよびeval_dateのカラムを持つ "evaluation"です。
私はプレーヤーを毎日言うと評価します。そのため、特定のプレーヤーを評価すると、データはデータベースに保存されます。同じプレーヤーを同じ日に保存すると、player_idとeval_dateを使用してデータが挿入されません。

どのようにすればいいですか?ありがとう

PS:player_idはプライマリキーではないので、別の日付で同じIDを持つ同じテーブルにプレーヤーを挿入できます。IDと日付がすでにデータベースに存在する場合、挿入を防止するにはどうすればよいですか?

public function update() { 
$players = array(); 

foreach ($this->input->post('checkbox') as $checkbox) { 
    list($value, $player_id) = explode('::', $checkbox); 
    self::update_player($players, $player_id, 'player_att1', $value); 
} 
foreach ($this->input->post('checkbox2') as $checkbox) { 
    list($value, $player_id) = explode('::', $checkbox); 
    self::update_player($players, $player_id, 'player_att2', $value); 
} 
foreach ($this->input->post('checkbox3') as $checkbox) { 
    list($value, $player_id) = explode('::', $checkbox); 
    self::update_player($players, $player_id, 'player_att3', $value); 
} 
foreach ($this->input->post('checkbox4') as $checkbox) { 
    list($value, $player_id) = explode('::', $checkbox); 
    self::update_player($players, $player_id, 'player_att4', $value); 
} 
$this->load->model('Evaluation_model'); 
foreach ($players as $player_id => $data) { 
    $this->Evaluation_model->editview($player_id, $data); 
} 
redirect(base_url('Evaluations/evaluate')); 
} 

static function update_player(&$players, $id, $property, $value) { 
if (!isset($players[$id])) { 
    $players[$id] = array(
     'player_id' => $id, 
     'player_att1' => 0, 
     'player_att2' => 0, 
     'player_att3' => 0, 
     'player_att4' => 0, 
     'player_atotal' => 0 
    ); 
} 
$players[$id][$property] = $value; 
$players[$id]['player_atotal'] += $value; 
} 

答えて

1

まずあなたが

/* This will get you the count of the player for the specific date */ 
    $playerAlreadyExistsCount = $this->db 
    ->where('player_id', $palyerId) 
    ->where('eval_date', $evalDate) 
    ->count_all('evaluation'); 

    if($playerAlreadyExistsCount > 0){ 
     //Player already exists. You can set a flashdata and show the user that the player is already inserted 
    }else{ 
     //Player is not existing so create the new player here. 
    } 
+0

がどのように私はこれを実行することができ、次のようにプレイヤーの数を確認する簡単な使用して、指定した日付のプレイヤーが存在するかしないかどうかを確認する必要がありますか? "$ playerAlreadyExistsCount = $ this-> db"が開始されました。 – Kate

+0

上記のコードをcodeigniterモデルに入れる必要があります。ここで私は、$ this-> db-> where() - > where() - > count_all( 'table_name');というように1行に書くこともできる連鎖を行っています。あなたが連鎖していれば、ずっと簡単になるでしょう。 –

0

既に同じ「ID」とSQL一意性制約を使用して同じ「日付」ともう一つ存在するときは、行の挿入を防止することができます。 https://stackoverflow.com/a/3474309/2768318、データベースを変更してください。

PHP経由での挿入を避けたい場合は、まずそのIDを持つすべてのPLAYERを読み込み、同じ日付の別のものがあるかどうかを確認する必要があります。

関連する問題