Jump to content

php mysql search


andyd34

Recommended Posts

I am trying to search a table but all I am doing is pulling my hair out everytime it runs. Here is my script.

 

$whereclause = 'AccStatus = "active"';
if($_POST['chck1']) { $whereclause .= ' AND col1 = "Y"'; }
if($_POST['chck2']) { $whereclause .= ' AND col2 = "Y"'; }
if($_POST['chck3']) { $whereclause .= ' AND col3 = "Y"'; }
if($_POST['chck4']) { $whereclause .= ' AND col4 = "Y"'; }
if($_POST['chck5']) { $whereclause .= ' AND col5 = "Y"'; }
if($_POST['chck6']) { $whereclause .= ' AND col6 = "Y"'; }
if($_POST['radio']=='option1') { $whereclause .= ' AND col7 = "a"'; }
if($_POST['radio']=='option1') { $whereclause .= ' AND col7 = "b"'; }
if($_POST['radio']=='option1') { $whereclause .= ' AND col7 = "c"'; }
if($_POST['radio']=='option1') { $whereclause .= ' AND col7 = "d"'; }
if($_POST['radio']=='option1') { $whereclause .= ' AND col7 = "e"'; }
if($_POST['radio']=='option1') { $whereclause .= ' AND col7 = "f"'; }

$getProfile = mysql_query('SELECT * FROM table WHERE '.$whereclause.' ORDER BY lastLogged ASC') or die(mysql_error());

 

Can anyone please help or just give me a few pointers

 

I have echoed $whereclause and all i get is

 

AccStatus = "active"
Link to comment
https://forums.phpfreaks.com/topic/147541-php-mysql-search/
Share on other sites

This code seems to submit data perfectly. This is what displayed:

 

AccStatus = 'active' AND col1 = 'Y' AND col2 = 'Y' AND col4 = 'Y' AND col5 = 'Y' AND col6 = 'Y' AND col7 = 'a' AND col7 = 'b' AND col7 = 'c' AND col7 = 'd' AND col7 = 'e' AND col7 = 'f'

 

And bellow is the code:

 

<?php

if( isset($_POST['clause']) )
{
$where  = "AccStatus = 'active'";
$where .= (isset($_POST['chck1'])) ? " AND col1 = 'Y'" : '';
$where .= (isset($_POST['chck2'])) ? " AND col2 = 'Y'" : '';
$where .= (isset($_POST['chck3'])) ? " AND col3 = 'Y'" : '';
$where .= (isset($_POST['chck4'])) ? " AND col4 = 'Y'" : '';
$where .= (isset($_POST['chck5'])) ? " AND col5 = 'Y'" : '';
$where .= (isset($_POST['chck6'])) ? " AND col6 = 'Y'" : '';
$where .= ($_POST['radio'] == 'option1') ? " AND col7 = 'a'" : '';
$where .= ($_POST['radio'] == 'option2') ? " AND col7 = 'b'" : '';
$where .= ($_POST['radio'] == 'option3') ? " AND col7 = 'c'" : '';
$where .= ($_POST['radio'] == 'option4') ? " AND col7 = 'd'" : '';
$where .= ($_POST['radio'] == 'option5') ? " AND col7 = 'e'" : '';
$where .= ($_POST['radio'] == 'option6') ? " AND col7 = 'f'" : '';

echo $where;
/* $getProfile = mysql_query("SELECT * FROM `table` WHERE ".$where." ORDER BY lastLogged ASC") or die(mysql_error().' on line '.__LINE__); */
}
else if( !isset($_POST['clause']) )
{
echo '
<form action="'.$_SERVER['PHP_SELF'].'" method="post">
	<input type="hidden" name="clause" value="1">
	<input type="checkbox" name="chck1" value="1"> CheckBox 1 COL 1 y<br />
	<input type="checkbox" name="chck2" value="1"> CheckBox 2 COL 2 y<br />
	<input type="checkbox" name="chck3" value="1"> CheckBox 3 COL 3 y<br />
	<input type="checkbox" name="chck4" value="1"> CheckBox 4 COL 4 y<br />
	<input type="checkbox" name="chck5" value="1"> CheckBox 5 COL 5 y<br />
	<input type="checkbox" name="chck6" value="1"> CheckBox 6 COL 6 y<br />
	<input type="radio" name="radio" value="option1"> OPTION 1 COL 7 a<br />
	<input type="radio" name="radio" value="option2"> OPTION 1 COL 7 b<br />
	<input type="radio" name="radio" value="option3"> OPTION 1 COL 7 c<br />
	<input type="radio" name="radio" value="option4"> OPTION 1 COL 7 d<br />
	<input type="radio" name="radio" value="option5"> OPTION 1 COL 7 e<br />
	<input type="radio" name="radio" value="option6"> OPTION 1 COL 7 f<br />
	<input type="submit" name="Submit & Display">
</form>';
}

?>

 

Try it out at http://www.deadlykillah.com/test2.php But be quick to try it out, i use that page to test out code's and i test out codes alot.

 

Code Updated

Link to comment
https://forums.phpfreaks.com/topic/147541-php-mysql-search/#findComment-774509
Share on other sites

Here is a partial submit form

 

<form action="results.php" method="post">
<input type="checkbox" name="chck1" value="Y" /><br />
<input type="checkbox" name="chck2" value="Y" /><br />
<input type="checkbox" name="chck3" value="Y" /><br />
<input type="checkbox" name="chck4" value="Y" /><br />
<input type="checkbox" name="chck5" value="Y" /><br />
<input type="checkbox" name="chck6" value="Y" /><br />
<input type="radio" name="radio" value="option1" /><br />
<input type="radio" name="radio" value="option2" /><br />
<input type="radio" name="radio" value="option3" /><br />
<input type="radio" name="radio" value="option4" /><br />
<input type="radio" name="radio" value="option5" /><br />
<input type="radio" name="radio" value="option6" /><br />
<input type="submit" value="save" name="submit" />
</form>

 

I though that all the posted fields where not been sent so i tried the following

 

foreach($_POST as $str=>$val) {
echo '$' . $str . ' = ' . $val . ' <br />';
}

 

and all the fields and coming through ok

Link to comment
https://forums.phpfreaks.com/topic/147541-php-mysql-search/#findComment-774510
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.