Foreach in prepare statement not working

I have a method that inserts multiple mass dates and times into a database. I have checked to make sure that the method is receiving the array of masses correctly. The array it receives takes the following structure:

array(2) {
[0]=> array(3) {
[“mass_name”]=> string(13) “Barry\'s Mass”
[“mass_date_time”]=> string(19) “2012-04-22 16:00:00”
[“mass_private_public”]=> string(6) “public”
}
[1]=> array(3) {
[“mass_name”]=> string(14) “Jaclyn\'s Mass”
[“mass_date_time”]=> string(19) “2012-04-22 16:00:00”
[“mass_private_public”]=> string(6) “public”
}
}

Here is the PHP used to INSERT into the database.

	public function Set_add_masses($set_masses) {
	
		global $mysqli;
		
		if ($stmt = $mysqli->prepare("INSERT INTO masses (mass_name, mass_date_time, mass_private_public, user_edited) VALUES (?, ?, ?, ?)")) {
			foreach ($set_masses as $key => $value) {
				$stmt->bind_param("sssi", $set_masses[$key]['mass_name'], $set_masses[$key]['mass_date_time'], $set_masses[$key]['mass_private_public'], $_SESSION['user_id']);
				if ($stmt->execute()) {
					$this->masses = 'Success';
				} else {
					$this->masses = 'Failed at INSERT ' . $key;
					break;
				}
			}
			$stmt->close();
		} else {
			$this->masses = NULL;
		}
		
		return $this->masses;
	
	}

When I dump the $this->masses variable, I get “NULL” and the masses aren’t inserted into the database. Any ideas?

Probably there is something wrong with your $mysqli, and $mysqli->prepare returns NULL thus NULL in $this->masses.

See http://us2.php.net/manual/en/mysqli.prepare.php

My query produces the following:


INSERT INTO masses (mass_name, mass_date_time, mass_private_public, user_edited) VALUES ("Barry\\'s Mass", "2012-04-23 00:00:00", "public", 1), ("Jaclyn\\'s Mass", "2012-04-23 00:00:00", "public", 1)

Is there something that I am doing wrong in the query?

I think you need change the (single quote) to a code that will be accepted by the query.

See this page for more info:
http://us2.php.net/manual/en/mysqli.real-escape-string.php

Have you tried echoing $mysqli->error ? According to your script, if you’re getting NULL out of the function, your initial IF returned FALSE.
For that matter, have you made sure $mysqli is a valid object?

(PS: Mysqli->prepare cant return Null. It will return an object or FALSE.)

I think his $mysqli->prepare did return FALSE and then he set $this->masses to NULL.

I figured it out. I actually had changed the name of the column in the database and I didn’t realize it. Sorry for all of the trouble for a mistake on my end, but thanks for the help!