1

Using usercake, I added an extra field to the registration page to record a users interests and store it in a table called interests.

Since I not storing in the same table I needed to create a separate query like the following. Please take note that the user supplied data in being turned into an array.

Here is my query:

//add interests1
                $interestsArray = explode(',', $this->interests);
                $interests1 = $interestsArray[0];
                $interests2 = $interestsArray[1];
                $interests3 = $interestsArray[2];
                $interests4 = $interestsArray[3];
                $interests5 = $interestsArray[4];
                $this->interests1 = sanitize($interests1);
                $this->interests2 = sanitize($interests2);
                $this->interests3 = sanitize($interests3);
                $this->interests4 = sanitize($interests4);
                $this->interests5 = sanitize($interests5);
                $stmt = $mysqli->prepare("INSERT INTO ".$db_table_prefix."interests (
                    interest
                    )
                    VALUES (
                    ?
                    )");
                $stmt->bind_param("s", $this->interests1);
                $stmt->execute();
                $stmt->close(); 

However I am getting the following error:

Fatal error: Call to a member function bind_param() on a non-object in /path/models/class.newuser.php on line 218

But there is not line 218 in the code... leaving me confused. I am hoping to eventually store each variable in the array in the column on separate rows. What do you guys suggest?

A link to the whole class if it helps is here: linktocode

RedShirt
  • 855
  • 2
  • 20
  • 33

1 Answers1

1

$stmt might be FALSE due to database errors for some reason, e.g. no connection, no database selected by USE, table does not exist aso. Check for database errors and write them into a log.

if(mysqli_connect_errno())
{
  // log somewhere mysqli_connect_error()
}
else
{
  if($stmt = $mysqli->prepare("INSERT ..."))
  {
    $stmt->bind_param("s", $this->interests1);
    $stmt->execute();
  }
  else
  {
    // log somewhere $mysqli->error;
  }
}
Pinke Helga
  • 6,378
  • 2
  • 22
  • 42
  • apparently I needed to add the database name in front of the prefix. Thanks for showing me how to troubleshoot – RedShirt Jun 22 '14 at 21:36
  • Either so, what is in my opinion the best way, or you can execute the `USE databasename` statement once before and then you *can* omit the database prefixes in front of table/entity names. – Pinke Helga Jun 23 '14 at 10:48