I have table A(predictions) consisting of columns from table B(fixtures) and table C(user registration table). Each time a new user registers I want to add their username to table A along with a data from all data from table B.
When I register a new user it only inserts the users data into table A(predictions) and the first row from table B(predictions).
What i want to achieve is; when a new user registers their username name will be added into the username column along with along the fixtures from the season, therefore the username is inserted in each row for each fixture:
Pred_ID | Fix_ID | Home_Team | Home_Score | Away_Score | Away_Team | username
1 -----------1 -------Man U------------------- 7-------------0-----------------Arsenal------B.Rubble
2 -----------1 -------Man U------------------- 5-------------0----------------Arsenal-------B.Rubble
3 -----------2 -------Chelsea------------------0-------------1----------------Leister-------New.user
4 -----------2 -------Chelsea------------------ 0------------3----------------Leister--------New.user
Using The insert function
public function insert($table, $fields = array()) {
$keys = array_keys($fields);
$values = '';
$x = 1;
foreach ($fields as $field) {
$values .= '?';
if ($x < count($fields)) {
$values .= ', ';
} //are we at end of defined fields
$x++;
}
$sql = "INSERT INTO {$table} (`" . implode('`, `', $keys) . "`) VALUES ({$values})";
if (!$this->query($sql, $fields)->error()) {
return true;
}
return false;
}
Im using the following updatePredTable() function
public function updatePredTable($fields=array()){
if(!$this->_db->insert('predict', $fields)) {
throw new Exception('There was a problem creating an account');
}
}
register.php
<?php
require_once 'core/init.php';
include 'navbar.php';
$prediction = DB::getInstance()->query("SELECT * FROM fixtures");
if (!$prediction->count()) {
echo 'No preds';
} else {
foreach ($prediction->results() as $row) {
$row = get_object_vars($row);
echo $row['Home_Team'], '<br>';
if (Input::exists()) {
if (Token::check(Input::get('token'))) {
var_dump(Token::check(Input::get('token')));
//echo 'I have been run';
$validate = new Validate();
$validation = $validate->check($_POST, array(
'username' => array(
'required' => true,
'min' => 2,
'max' => 20,
'unique' => 'users'
),
'password' => array(
'required' => true,
'min' => 6
),
'password_again' => array(
'required' => true,
'matches' => 'password'
),
'name' => array(
'required' => true,
'min' => 2,
'max' => 50
)
));
if ($validation->passed()) {
//Session::flash('Success', 'You registered successfully!');
//header('Location: index.php');
$user = new User();
$salt = Hash::salt(32);
try {
$user->create(array(
'username' => Input::get('username'),
'password' => Hash::make(Input::get('password'), $salt),
'salt' => $salt,
'name' => Input::get('name'),
'joined' => date('Y-m-d H:i:s'),
'group' => 1
));
$user->updatePredTable(array(
'username' => Input::get('username'),
'fixture_ID' => $row['Fixture_ID'],
'date' => $row['Date'],
'home_Team' => $row['Home_Team'],
'away_Team' => $row['Away_Team']
));
Session::flash('home', 'You have been registered you can now log in!');
//like:
//header('Location: index.php');
Redirect::to('index.php');
}
catch (Exception $e) {
die($e->getMessage());
}
//echo 'Passed';
} else {
foreach ($validation->errors() as $error) {
echo $error, '<br>';
//print_r($validation->errors());
}
}
}
}
}
}
?>