Jump to content

[SOLVED] php mysql problem


phplearner2008

Recommended Posts

Hi,

I have a web page that has a drop down list with multiple select. Based on the user's selections, mysql database has to be queried for each option selected by user.

 

The name of drop down list is EDUCATION. $lines counts the number of options selected. I am storing value of each option in $mystore[$i]

 

I am using loop as I need to query database for each option the user selects from the list. The code does not work. Any suggestions?

 

<?php

$lines = 0;

$i=0;

foreach ($_POST['EDUCATION'] as $EDUCATION => $value)

{

echo "Education: $EDUCATION; Value: $value<br>";

$store[$EDUCATION] = $value;

$mystore[$i] = $store[$EDUCATION];

$lines++;

}

$eduresult = SELECT * FROM profile;

for($i=0;$i<$lines;$i++)

{

$edu = "SELECT * FROM profile WHERE EDUCATION = '$mystore[$i]'";

 

$eduresult =mysql_query(".$eduresult.UNION.$edu.")or die(mysql_error());

}

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

 

echo $row['NAME'];

echo $row['EMAIL'];

 

}

 

?>

Link to comment
Share on other sites

Why are you creating two arrays with the inverted keys/values?  You don't even use the $store array...

 

$store[$EDUCATION] = $value;
$mystore[$i] = $store[$EDUCATION];

 

As fenway said, why are you unioning a "SELECT *" with a subset of that same table?  Also, why are you looping through each $mystore when you could simply use an IN clause?

 

$eduresult = "SELECT * FROM profile";

for($i = 0; $i < $lines; $i++) {
$edu = "SELECT * FROM profile WHERE EDUCATION = '$mystore[$i]'";

$eduresult = mysql_query(".$eduresult.UNION.$edu.")or die(mysql_error());
}

 

Better way...?

 

<?php

// apply mysql_real_escape_string to all the elements...
array_walk($_POST['EDUCATION'], 'mysql_real_escape_string');

// generate the query...
$query = "SELECT * FROM profile WHERE `EDUCATION` IN('" . implode("', '", $_POST['EDUCATION']) . "')";

$result = mysql_query($query) or die("Query: " . $query . "\n\n" . mysql_error());

while ($row = mysql_fetch_assoc($result)) {
echo $row['NAME'] . " " . $row['EMAIL'];
}

Link to comment
Share on other sites

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.