title: Taking parameters from two different objects to insert into a database with php/sql
body: <p>I'm trying to create a 'CD Store' program, where I want to store credit card details on a database, but I want the database to reference the user's ID where I have created elsewhere. How can I pass a parameter from another object to a new one? I tried to write the code but I keep getting this error</p>
  
  <pre><code>Fatal error: Call to a member function getId() on a non-object in applications/XAMPP/xamppfiles/htdocs/CDStore/creditCardAccess.php on line 36
  </code></pre>
  
  <p>I know that it is possibly not being referenced, and the $user variable is showing up yellow in netbeans. I'm not sure how will I go about this? any pointers would be much appreciated. Here is my code thanks</p>
  
  <pre><code>&lt;?php
  
  class creditCardAccess {
  
  protected $link;
  
  public function __construct() {
      $host = "localhost";
      $username = "root";
      $password = "";
      $database = "CDStore";
  
      $dsn = "mysql:host=$host;dbname=$database";
  
      $this-&gt;link = new PDO($dsn, $username, $password);
      $this-&gt;link-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  }
  
  public function __destruct() {
      $this-&gt;link = null;
  }
  public function insertCreditCard($creditCard) {
      if (!isset($creditCard) &amp;&amp; $creditCard != null) {
          throw new Exception("Credit Card Required");
      }
  
      $sql = "INSERT INTO creditCards(cardType, cardName, cardNumber, cardExpiryDate, cardCCV, userID)"
              . "VALUES (:cardType, :cardName, :cardNumber, :cardExpiryDate, :cardCCV, :userID)";
  
      $params = array(
          'cardType' =&gt; $creditCard-&gt;getCardType(),
          'cardName' =&gt; $creditCard-&gt;getCardName(),
          'cardNumber' =&gt; $creditCard-&gt;getCardNumber(),
          'cardExpiryDate' =&gt; $creditCard-&gt;getCardExpiryDate(),
          'cardCCV' =&gt; $creditCard-&gt;getCardCCV(),
          'userID' =&gt; $user-&gt;getId()
      );
  
      $stmt = $this-&gt;link-&gt;prepare($sql);
      $status = $stmt-&gt;execute($params);
      if ($status != true) {
          $errorInfo = $stmt-&gt;errorInfo();
          throw new Exception("Could Not Add Credit Card: " . $errorInfo[2]);
      }
  
      $id = $this-&gt;link-&gt;lastInsertId('creditCard');
      $creditCard-&gt;setId($id);
  }
  
  }
  ?&gt;
  </code></pre>
  
  <p>line 36 is 'userID' => $user->getId()</p>