Help with Transaction and 2 Prepared Statements

When a User sends a Private Message, I need to call two INSERT statements.

To make my code more fail-safe, I would like to wrap these Prepared Statements into a MySQL Transaction, but am unsure of how to do this?! :-/

Here is a code snippet…


	// Attempt to Send Private-Message.
	if (empty($errors)){
		// Valid Form Data.

		// Create Private Message.
		// Build Query.
		$q2 = "INSERT INTO private_msg(member_id_from, subject, body, sent_on)
				VALUES (?, ?, ?, NOW())";

		$stmt2 = mysqli_prepare($dbc, $q2);

		mysqli_stmt_bind_param($stmt2, 'iss', $sessMemberID, $pmSubject, $pmBody);

		mysqli_stmt_execute($stmt2);

		// Capture New MessageID.
		$messageID = mysqli_insert_id($dbc);

		if (mysqli_stmt_affected_rows($stmt2)==1){
			// Insert Succeeded.

			// Create PM Recipient.
			// Build Query.
			$q3 = "INSERT INTO private_msg_recipient(member_id_to, message_id, created_on)
					VALUES (?, ?, NOW())";

			$stmt3 = mysqli_prepare($dbc, $q3);

			mysqli_stmt_bind_param($stmt3, 'ii', $pmRecipientID, $messageID);

			mysqli_stmt_execute($stmt3);

			if (mysqli_stmt_affected_rows($stmt3)==1){
				// Insert Succeeded.
				// Private Message Sent.

			}else{
				// Insert Failed.
				// Display Error

			}//End of CREATE PM RECIPIENT

		}else{
			// Insert Failed.
			// Display Error

		}//End of CREATE PRIVATE-MESSAGE

	}else{
		// Invalid Form data.
		// Drop through to display Errors.

	}//End of ATTEMPT TO SEND PRIVATE-MESSAGE

Could someone help me figure out how to use a Transaction for this code, and give me any additional pointer as well?

Sincerely,

Debbie