我有表A(预测),包括表B(灯具)和表C(用户注册表)中的列。 每次新用户注册时,我都希望将其用户名添加到表A以及来自表B的所有数据中的数据。
当我注册新用户时,它只将用户数据插入表A(预测)和表B中的第一行(预测)。
我想要达到的目标是什么;当一个新用户注册他们的用户名时,它将被添加到用户名栏以及季节的灯具中,因此每个灯具的每一行都会插入用户名:
Pred_ID | Fix_ID | Home_Team | Home_Score | Away_Score | Away_Team |用户名
1 ----------- 1 ------- Man U ------------------- 7 ----- -------- 0 -----------------阿森纳------ B.Rubble
2 ----------- 1 ------- Man U ------------------- 5 ----- -------- 0 ----------------阿森纳------- B.Rubble
3 ----------- 2 -------切尔西------------------ 0 ------- ------ 1 ---------------- Leister ------- New.user
4 ----------- 2 -------切尔西------------------ 0 ------- ----- 3 ----------------莱丹-------- New.user
使用插入功能
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;
}
我使用以下updatePredTable()函数
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());
}
}
}
}
}
}
?>
答案 0 :(得分:0)
我认为您需要查看循环预测结果集的位置。看起来在第一次迭代中你重定向到index.php因此只添加了一行尝试循环结果集,如下所示
<?php
require_once 'core/init.php';
include 'navbar.php';
$prediction = DB::getInstance()->query("SELECT * FROM fixtures");
if (!$prediction->count()) {
echo 'No preds';
} else {
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
));
foreach ($prediction->results() as $row) {
$row = get_object_vars($row);
echo $row['Home_Team'], '<br>';
$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());
}
}
}
}
}
我没有对此进行测试,但希望它会指出你正确的方向