1

I'm trying to insert data from 2 different tables in 2 different databases (A & B), into a 3rd table in database 3.

Here's my code:

// display all errors generated by script
error_reporting(E_ALL);
ini_set('display_errors','On');
include_once 'db_inc.php';  
try{
    $db = new PDO(DB_INFO, DB_USER, DB_PASS);
    //get the new customer array

    $query = $db->prepare("SELECT * FROM moodle");
    $query->execute(array());   
      $customer_arr=array();
      while($res= $query->fetch(PDO::FETCH_ASSOC)){
      $customer_arr[]=$res;
    }
}
catch (PDOException $e) {
    echo $err5="ERROR: ".$e->getMessage()."\n ";
}
try{
    $db = new PDO(DB_INFO4, DB_USER4, DB_PASS4);

    $nowtime = time();

    $sql4 = "INSERT INTO mdl_groups_members( groupid, userid , timeadded ) 
            SELECT l.mgroup AS moodle, r.id AS mdl_user
            FROM moodle l
            JOIN mdl_user r ON l.orders_id = r.id
            WHERE l.mgroup >0 ON DUPLICATE 
            KEY UPDATE groupid = VALUES (
            groupid
            )";
    $stmt4 = $db->prepare($sql4);
    $stmt4->execute(array($groupid,$userid,$nowtime));
}
catch (PDOException $e) {
    echo $err5="ERROR: ".$e->getMessage()."\n ";
}

I get the following errors.

Notice: Undefined variable: groupid in ... on line 40

Notice: Undefined variable: userid in ... on line 40

Any help will be gratefully appreciated. Thanks.

Community
  • 1
  • 1
Matt
  • 33
  • 4
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Epodax Apr 20 '15 at 12:04

2 Answers2

1

your last line (execute) crashes yeah :
array($groupid,$userid,$nowtime) you didn't define those 3 variables...

Random
  • 3,158
  • 1
  • 15
  • 25
1

You will need to define the variables that you are using on this line of code -

$stmt4->execute(array($groupid,$userid,$nowtime));

Also, you seem to be using the PDO execute method incorrectly. Here is an example of how a statement should be prepared.

<?php
/* Execute a prepared statement by passing an array of insert values */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < :calories AND colour = :colour');
$sth->execute(array(':calories' => $calories, ':colour' => $colour));
?>

http://php.net/manual/en/pdostatement.execute.php

gamesmad
  • 399
  • 1
  • 2
  • 14