PM Query returning 0?

I have a Form where a User can enter in a Private Message.

When the Form is submitted, my PHP first checks that the data is valid. However, it is in this block of code where I’m having trouble. When working properly, my code should take the “To: Username” entered and return back the “To: User ID” but my query keeps returning 0 ?!

Here is a snippet of my code…


	// ************************
	// Validate Form Data.		*
	// ************************

	// Check Recipient.
	if (empty($trimmed['pmTo_Username'])){
		// No Recipient.
		$errors['pmTo'] = 'Enter a PM Recipient (Username).';
	}else{
		// Recipient Exists.

		// ************************
		// Check Username Format.	*
		// ************************
		if (preg_match('~(?x)					# Comments Mode
					^					# Beginning of String Anchor
					(?=.{8,30}$)			# Ensure Length is 8-30 Characters
					[a-z0-9_.-]*			# Match only certain Characters
					$					# End of String Anchor
					~i', $trimmed['pmTo_Username'])){

				// Valid Username.

				// ************************
				// Check Username Exists.	*
				// ************************

				// Build query.
				$q1 = 'SELECT id
						FROM member
						WHERE username=?';

				// Prepare statement.
				$stmt1 = mysqli_prepare($dbc, $q1);

				// Bind variable to query.
				mysqli_stmt_bind_param($stmt1, 's', $trimmed['pmTo_Username']);

				// Execute query.
				mysqli_stmt_execute($stmt1);

				// Store results.
				mysqli_stmt_store_result($stmt1);

				// Check # of Records Returned.
				if (mysqli_stmt_num_rows($stmt1)==1){
					// Username Found.

					// Bind result-set to variable.
					mysqli_stmt_bind_result($stmt1, $pmTo_ID);

//KEEPS RETURNING '0' ??????????
echo '<p>$pmTo_ID = ' . $pmTo_ID . '</p>';
exit();

				}else{
					// Username Not Found.
					$errors['pmTo'] = 'No such Username exists.';
				}//End of CHECK USERNAME EXISTS

Here is a snippet of my HTML Form…


<form id="sendPM" action="" method="post">
	<fieldset>
		<legend>Send Private Message (PM)</legend>

		<!-- Message To -->
		<label for="pmTo_Username">To:</label>
		<input id="pmTo_Username" name="pmTo_Username" type="text" maxlength="30"
			 value="<?php if(isset($pmTo_Username)){echo htmlentities($pmTo_Username, ENT_QUOTES);} ?>" /><!-- Sticky Field -->
		<?php
			if (!empty($errors['pmTo'])){
				echo '<span class="error">' . $errors['pmTo'] . '</span>';
			}
		?>

If I run this query in phpMyAdmin straight up…


SELECT id
FROM member
WHERE username="DoubleDee";

…then I get “19” which the correct UserID for the Username “DoubleDee”

So what is wrong with my Prepared Statement above?! (For the life of me, I can’t see what I am doing wrong?!) :mad:

Thanks,

Debbie

I forgot to add this…

// Bind result-set to variable.
mysqli_stmt_bind_result($stmt1, $pmSendTo_ID);

// Fetch record.
mysqli_stmt_fetch($stmt1);

Solved.

Debbie