Jump to content

while loop to pick a certain type from db??


rdkd1970

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='';

 

::)

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

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))

 

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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"])."',

Link to comment
Share on other sites

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? ^^^

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

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 //////////////

Link to comment
Share on other sites

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 = '';

Link to comment
Share on other sites

I tried this put the if statement at the beginning of the script with the echo to be need looking_for in case the if statement did not work. the results is the page shows that I have 10 names in the db but does not show the names.

Link to comment
Share on other sites

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;
}


Link to comment
Share on other sites

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>';
     }
}
?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.