dpa31905 2015-07-13 16:27
浏览 68

PHP Query从多个表插入数据

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());
                    }
                }
            }
        }
    }  
}


?>
  • 写回答

1条回答 默认 最新

  • dook0034 2015-07-13 20:35
    关注

    I think you need to look at where you loop over the predictions result set. It looks like on the first iteration you are redirecting to index.php hence only one row is added try looping over the resultset as below

     <?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());
                        }
                    }
    
            }
        }  
    }
    

    I haven't tested this but hopefully it will point you in the right direction

    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私