Jump to content

Recommended Posts

I am not getting any error messages as I just think the codes are wrong. I am trying to get the $sql(s) to choose and print out the the looking_for in the database. However it is not printing anything. Any ideas of where I am need to fix this. I have tried a few ways with no luck.

 

$sql = mysql_query("SELECT id,gender,looking_for FROM Members");
$nr = mysql_num_rows($sql);

$sql2 = mysql_query("SELECT * FROM Members WHERE id='".($_POST['$looking_for'])."' LIMIT $pn, $itemsPerPage"); 
$outputList = '';
while($row = mysql_fetch_array($sql2)) { 

$id = $row["id"];
$username = $row["username"];
$firstname = $row["firstname"];
if (!$firstname) {
	$firstname = $username;
}

$gender='';
$looking_for='';

 

::)

Separate the query string from the query execution, store it in a variable, and use the variable in the execution. Then echo the query string so you can see if it contains the values you'd expect it to contain. If it does, paste it into phpMyAdmin, and see what results are returned.

 

$query = "SELECT field FROM table WHERE pk = $value";
$result = mysql_query($query);

// ETC.

echo "<br>$query<br>";

I removed the $ and also placed the codes into the database to see the results and it actually picked the only ones where I do not have anything in the gender or looking_for row. Were I need it to pick up on the person doing the looking_for their gender.

 

$sql = mysql_query("SELECT * FROM Members WHERE gender='".($_POST['looking_for'])."'");
$nr = mysql_num_rows($sql)

$sql2 = mysql_query("SELECT * FROM Members WHERE gender='".($_POST['looking_for'])."' LIMIT $pn, $itemsPerPage"); 
while($row = mysql_fetch_array($sql2))

 

I have this is my while loop and it is not echoing anything you are right.

 

while($row = mysql_fetch_array($sql2)) { 

$id = $row["id"];
$gender = $row["gender"];
$looking_for=$row["looking_for"];
$firstname = $row["firstname"];
if (!$firstname) {
	$firstname = $username;
}

So now the problem will be to see why your post variable is empty. Are you sure it has the right name? Can you post the code for the form that is sumbitting that post data? Try doing a print_r on $_POST and see if it is structured like you expect it to be

This is my registration info I just put the gender and looking_for in the values for this question. I also did the print and got nothing.???

 

$results = mysql_query("INSERT INTO Members (username,password,firstname,lastname,email,gender,looking_for,full_birthday,
   				maritalStatus,country,sign_up_date)
			VALUES	'".mysql_real_escape_string($_POST["gender"])."',
				'".mysql_real_escape_string($_POST["looking_for"])."',

Separate the query string from the query execution, store it in a variable, and use the variable in the execution. Then echo the query string so you can see if it contains the values you'd expect it to contain. If it does, paste it into phpMyAdmin, and see what results are returned.

 

$query = "SELECT field FROM table WHERE pk = $value";
$result = mysql_query($query);

// ETC.

echo "<br>$query<br>";

 

Did you bother to do this? ^^^

OK. Please post the markup for the form, and in your script that processes the form, add this at the top then post the output it generates after the form has been submitted:

 

echo '<br>///////////// POST ARRAY //////////////<br><pre>';
print_r($_POST);
echo '</pre><br>///////////// END POST ARRAY //////////////<br>';

Here is the results from the adding this to the top of my form

///////////// POST ARRAY //////////////

Array
(
    [username] => nathalie
    [password] => nathalie1
    [firstname] => Nathalie
    [lastname] => Davis
    [email] => nathalie@davis.com
    [gender] => Female
    [looking_for] => Male
    [birth_month] => 06
    [birth_day] => 24
    [birth_year] => 1940
    [maritalStatus] => Divorced
    [country] => Falkland Islands
    [humanCheck] => 
    [submit] => go to next page
)


///////////// END POST ARRAY //////////////

I think I just spotted the issue. The parentheses are within the single quotes in the query string, thus the value in the table would need to literally be "(Male)" to match. Change the query strings to eliminate the parentheses and string concatenation. You should also be validating and sanitizing the value before using it in a db query string.

 

// VALIDATE THE VALUE, SANITIZE AND ASSIGN IF VALID
$valid = array( 'Male', 'Female');
if( in_array($valid, $_POST['looking_for']) ) {
     $looking_for = mysql_real_escape_string($_POST['looking_for']);
} else {
     // an invalid value was submitted, so throw an error, or whatever.
}

$sql2 = mysql_query("SELECT * FROM Members WHERE id = '$looking_for' LIMIT $pn, $itemsPerPage"); 
$outputList = '';

This is code presently. I was not sure to leave out the pagination codes as they are working fine.

 

// VALIDATE THE VALUE, SANITIZE AND ASSIGN IF VALID
$valid = array( 'Male', 'Female');
if( in_array($_POST['looking_for'],$valid) ) { 
    $looking_for = mysql_real_escape_string($_POST['looking_for']);
} else {    
 // an invalid value was submitted, so throw an error, or whatever.
 }

// DEAFAULT QUERY STRING
$queryString = "WHERE gender= '$looking_for' ORDER BY looking_for ASC";
// DEFAULT MESSAGE ON TOP OF RESULT DISPLAY
$queryMsg = "Showing looking_for";

$sql = mysql_query("SELECT * FROM Members ORDER BY gender = '$looking_for'");

$nr = mysql_num_rows($sql);

$sql2 = mysql_query("SELECT * FROM Members ORDER BY gender = '$looking_for' LIMIT $pn, $itemsPerPage");

$outputList = '';
while($row = mysql_fetch_array($sql2)) { 

$id = $row["id"];
$gender = $row["gender"];
$looking_for=$row["looking_for"];
$firstname = $row["firstname"];
if (!$firstname) {
	$firstname = $username;
}


Obviously we're not making a lot of progress with this, so let's do some debugging. Paste this code into a new script and post the output so we can start to figure out where the root of the problem is.

 

<?php
// include your database connection credentials here

$query = "SELECT gender FROM Members";
if( !result = mysql_query($query) ) {
     echo "Query $query<br>Failed with error: " . mysql_error());
} else {
     while( $array = mysql_fetch_assoc($result) ) {
          var_dump($array['gender']);
          echo '<br>';
     }
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.