...

View Full Version : Retrieving records - return all fields in row



mike_tw
04-04-2007, 10:15 PM
Hello, I am making a drop down box to filter a sql database.


MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
@mysql_select_db( "$dbName") or die( "Unable to select database");

function secured($val)
{
$val = strip_tags(trim(($val))) ;
$val = escapeshellcmd($val);
return stripslashes($val);
}
if (get_magic_quotes_gpc())
{
$Manufacturer = $_POST['Manufacturer'];
}
else
{
$Manufacturer = addslashes($_POST['Manufacturer']);
}
$Manufacturer = secured($Manufacturer);

if ($Manufacturer != "SelectManufacturer") //The Default select value
{
$query = "SELECT * FROM bricks WHERE Manufacturer='$Manufacturer'";
//take off the '1' on Manufacturer as suggested my Nightfire
//make sure you getting the right values
$result = mysql_query($query) or die("failed: ".mysql_error());
$num_rows = mysql_num_rows($result);
if(!$num_rows)
{
echo "<table><tr><td align='center'>No data found 0 record returned</td></tr></table>";
}
else
{
echo "<table align='center'><tr>";
echo "<td align='center'><b>Manufacturer</b></td>";
echo "<td align='center'><b>Colour</b></td>";
echo "<td align='center'><b>Texture</b></td>";
echo "<td align='center'><b>Manufacturing Process</b></td>";
echo "</tr>";

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
echo "<tr><td align='center'>".$row["Manufacturer"]."</td>";
echo "<td align='center'>".$row["Colour"]."</td>";
echo "<td align='center'>".$row["Texture"]."</td>";
echo "<td align='center'>".$row["Manufacturing Process"]."</td>";
echo "</tr>";

}
echo "</table>";
}
?>

At the moment it only returns the manufacturer name and not the rest of the details in that row. How can i make it do that?

.........

Choose Manufacturer 1 and it will bring the results up.. but only the manufacturer name.

Any help appreciated :)

iLLin
04-04-2007, 10:17 PM
Isn't this a repost?

Verify the data exists in the database and make sure your fields are correct. Caps count.

iLLin
04-04-2007, 10:18 PM
Do this, do a print_r($result) and post that information up.

mike_tw
04-04-2007, 10:28 PM
Resource id #2

thats what comes up

iLLin
04-04-2007, 10:30 PM
sorry :( do print_r($row) in your loop

mike_tw
04-04-2007, 10:35 PM
Same table .... nothing else printed but the Manufacturer

iLLin
04-04-2007, 10:37 PM
Show me the results.

mike_tw
04-04-2007, 10:42 PM
Manufacturer Colour Texture Manufacturing Process
Manufacturer1
Manufacturer1
Manufacturer1

iLLin
04-04-2007, 10:42 PM
OHH i think I see your problem

Change this


while($row = mysql_fetch_array($result, MYSQL_ASSOC))
echo "<tr><td align='center'>".$row["Manufacturer"]."</td>";
echo "<td align='center'>".$row["Colour"]."</td>";
echo "<td align='center'>".$row["Texture"]."</td>";
echo "<td align='center'>".$row["Manufacturing Process"]."</td>";
echo "</tr>";

//to this
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {//notice the bracket
echo "<tr><td align='center'>".$row["Manufacturer"]."</td>";
echo "<td align='center'>".$row["Colour"]."</td>";
echo "<td align='center'>".$row["Texture"]."</td>";
echo "<td align='center'>".$row["Manufacturing Process"]."</td>";
echo "</tr>";
}//notice the bracket

iLLin
04-04-2007, 10:43 PM
format you code better so you can keep track of brackets. I'm :confused: how did that even work?

iLLin
04-04-2007, 10:44 PM
Also do this to check your values


while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
print_r($row);
}


Then paste that up too.

mike_tw
04-04-2007, 10:50 PM
lol gotta love php


Parse error: syntax error, unexpected $end in /************************8/results.php on line 56

i changed the above to

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {//notice the bracket
echo "<tr><td align='center'>".$row["Manufacturer"]."</td>";
echo "<td align='center'>".$row["Colour"]."</td>";
echo "<td align='center'>".$row["Texture"]."</td>";
echo "<td align='center'>".$row["Manufacturing Process"]."</td>";
echo "</tr>";

Do I need to close bracket?

mike_tw
04-04-2007, 10:52 PM
YES! i closed the bracket and it worked thanks guys

mike_tw
04-04-2007, 10:56 PM
How hard would it be for me to move on to making the drop downs for colour , texture to work also? Or should i just quit while im ahead and settle for a manufacturer drop down only.

iLLin
04-04-2007, 11:04 PM
I posted that info on your other thread, you need to build out your query. Check out the other thread.

mike_tw
04-04-2007, 11:15 PM
This?


<?php

$where = " WHERE ";
if(isset(manufacture)) {
$where .= " manufacturer = $manu";
}
if(isset(color)) {
$where .= " AND color = $color ";
}

//you get the idea

//Then your query would be something like this
$query = "select * from table ".$where;

?>

Where in the current script would this go? inside the <? ?> or start a new <? ?>

iLLin
04-04-2007, 11:17 PM
Before this:


$query = "SELECT * FROM bricks WHERE Manufacturer='$Manufacturer'";


Basically you will take out your whole where statement in that query and build you own before you query so you know what your querying for.

mike_tw
04-04-2007, 11:20 PM
I have to do this for every possible combination ... Manufacturer AND colour AND texture AND process

colour AND texture AND process etc?

iLLin
04-04-2007, 11:30 PM
No you query will end up like this:

SELECT * FROM manufacturers WHERE color = '$color' AND process = '$process' AND texture = '$texture'

Only if there set. If there not set it will not filter your result set by that. Now what is your overall goal? To filter by each of these things, for instance I want a manufacturer that has red brick. So your query would be like this

SELECT * FROM manufacturers WHERE color = 'red' AND texture = 'brick'

Thats what I mean by building your where statement. You check if they choose a filter and add it in, if not then it never shows up.

mike_tw
04-04-2007, 11:40 PM
my goal is to have someting like this :

http://www.jewson.co.uk/en/templates/bricklibrary/main.jsp?_requestid=2320080

Have a go at that one, you will see that if you choose a manufacturer + colour red it will display all red from that manufacturer. Same as for the other drop downs.

What happens with this part:


function secured($val)
{
$val = strip_tags(trim(($val))) ;
$val = escapeshellcmd($val);
return stripslashes($val);
}
if (get_magic_quotes_gpc())
{
$Manufacturer = $_POST['Manufacturer'];
}
else
{
$Manufacturer = addslashes($_POST['Manufacturer']);
}
$Manufacturer = secured($Manufacturer);

if ($Manufacturer != "SelectManufacturer") //The Default select value
{

Do I do that x4 and then move on to building the query? Because atm the php page isnt reading the POST info of the other 3 drop downs is it?

Thanks... you are very kind:thumbsup:

I am learning more every day, im comfortable with the basics of PHP and SQL and things are becoming more clearer and obvious to me now.

iLLin
04-04-2007, 11:47 PM
Na dont use this:


if ($Manufacturer != "SelectManufacturer") //The Default select value
{


Set your form up with a hidden field with a value and do this



if(isset($_POST['myfield'])) {
//start from nothing just in case nothing is selected :)
$where = "";
//start your separator within the where statement.
$sep = " WHERE ";
if($Manufacturer != "SelectManufacturer") {
$where .= $sep." manufacturer = '".$Manufacturer."'";
//update your sep to AND for the next values
$sep = " AND ";
}

if($Colour != "whatever") {
$where .= $sep." colour = '".$Colour."'";
//update your sep to AND for the next values and just incase no manu was selected
$sep = " AND ";
}

//Do this for all your checkboxes then go onto this.

//so if no checkboxes were selected then you where statement will be blank and will return ALL rows. Keep that in mind.
$query = "SELECT * FROM manufacturers ".$where;
$result = mysql_query($query) or die;


Maybe that will set you straight :)

mike_tw
04-04-2007, 11:53 PM
Must go now, must sleep :)

I will work on this tomorrow and report back when i get stuck

Thanks for all your help :D

mike_tw
04-05-2007, 07:59 AM
MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
@mysql_select_db( "$dbName") or die( "Unable to select database");

function secured($val)
{
$val = strip_tags(trim(($val))) ;
$val = escapeshellcmd($val);
return stripslashes($val);
}
if (get_magic_quotes_gpc())
{
$Manufacturer = $_POST['Manufacturer'];
}
else
{
$Manufacturer = addslashes($_POST['Manufacturer']);
}
$Manufacturer = secured($Manufacturer);

function secured($val)
{
$val = strip_tags(trim(($val))) ;
$val = escapeshellcmd($val);
return stripslashes($val);
}
if (get_magic_quotes_gpc())
{
$Color = $_POST['Color'];
}
else
{
$Color = addslashes($_POST['Color']);
}
$Color = secured($Color);

function secured($val)
{
$val = strip_tags(trim(($val))) ;
$val = escapeshellcmd($val);
return stripslashes($val);
}
if (get_magic_quotes_gpc())
{
$Texture = $_POST['Texture'];
}
else
{
$Texture = addslashes($_POST['Texture']);
}
$Texture = secured($Texture);

function secured($val)
{
$val = strip_tags(trim(($val))) ;
$val = escapeshellcmd($val);
return stripslashes($val);
}
if (get_magic_quotes_gpc())
{
$Process = $_POST['Process'];
}
else
{
$Process = addslashes($_POST['Process']);
}
$Process = secured($Process);

if(isset($_POST['myfield'])) {
//start from nothing just in case nothing is selected :)
$where = "";
//start your separator within the where statement.
$sep = " WHERE ";
if($Manufacturer != "SelectManufacturer") {
$where .= $sep." manufacturer = '".$Manufacturer."'";
//update your sep to AND for the next values
$sep = " AND ";
}

if($Colour != "SelectColour") {
$where .= $sep." colour = '".$Colour."'";
$sep = " AND ";
}

if($Texture != "Texture") {
$where .= $sep." Texture = '".$Texture."'";
$sep = " AND ";
}

if($Process != "SelectManufacturingProcess") {
$where .= $sep." process = '".$Process."'";
$sep = " AND ";
}

$query = "SELECT * FROM manufacturers ".$where;
$result = mysql_query($query) or die;
print_r($row);
$num_rows = mysql_num_rows($result);

if(!$num_rows)
{
echo "<table><tr><td align='center'>No data found 0 record returned</td></tr></table>";
}
else
{
echo "<table align='center'><tr>";
echo "<td align='center'><b>Manufacturer</b></td>";
echo "<td align='center'><b>Colour</b></td>";
echo "<td align='center'><b>Texture</b></td>";
echo "<td align='center'><b>Manufacturing Process</b></td>";
echo "</tr>";

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr><td align='center'>".$row["Manufacturer"]."</td>";
echo "<td align='center'>".$row["Colour"]."</td>";
echo "<td align='center'>".$row["Texture"]."</td>";
echo "<td align='center'>".$row["Manufacturing Process"]."</td>";
echo "</tr>";
}
}
echo "</table>";
}
?>

Is that on the right track

With the hidden field, what needs to be there as id,name.. the default values?

Will this work for other combinations too? example, Red with Smooth Texture (no manufacturer selected)

iLLin
04-05-2007, 02:38 PM
You can only define a function once. Why not do this:



if (get_magic_quotes_gpc()) {
$Manufacturer = $_POST['Manufacturer'];
$Process = $_POST['Process'];
//other values
} else {
$Manufacturer = addslashes($_POST['Manufacturer']);
$Process = addslashes($_POST['Process']);
//other values
}


Start thinking logically about it. You have other stuff you can combine too, just need to think about it.

Also indent your code, makes for easier reading and wrap them in [php] tags next time you post it up :)

iLLin
04-05-2007, 02:42 PM
Also it seems your defeating the purpose of your "secured" function. You addslashes before you run that function on the string, and then strip them out? You should just strip_tags, and use mysql_real_escape.

mike_tw
04-05-2007, 02:57 PM
I've put some shape to it today, i've inserted images for each record and layed them out in a tidy table.


like this:?

if (get_magic_quotes_gpc()) {
$Manufacturer = $_POST['Manufacturer'];
$Process = $_POST['Process'];
$Texture = $_POST['Texture'];
$Color = $_POST['Color'];

} else {
$Manufacturer = addslashes($_POST['Manufacturer']);
$Process = addslashes($_POST['Process']);
$Texture = addslashes($_POST['Texture']);
$Color = addslashes($_POST['Color']);

}

out of interest, what does this feature do? $val = escapeshellcmd($val);

Thanks :D

iLLin
04-05-2007, 03:01 PM
Its used if you were using that string to execute a command. Look it up :) And yes that looks better. wrap your code in [php] tags and indent!

mike_tw
04-05-2007, 03:24 PM
Current code:



MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
@mysql_select_db( "$dbName") or die( "Unable to select database");

function secured($val)
{
$val = strip_tags(trim(($val))) ;
$val = escapeshellcmd($val);
return stripslashes($val);
}
if (get_magic_quotes_gpc()) {
$Manufacturer = $_POST['Manufacturer'];
$Process = $_POST['Process'];
$Texture = $_POST['Texture'];
$Color = $_POST['Color'];

} else {
$Manufacturer = addslashes($_POST['Manufacturer']);
$Process = addslashes($_POST['Process']);
$Texture = addslashes($_POST['Texture']);
$Color = addslashes($_POST['Color']);


}
echo "$Texture";
$query = "SELECT * FROM bricks WHERE Manufacturer='$Manufacturer'";
$result = mysql_query($query) or die("failed: ".mysql_error());
print_r($row);
$num_rows = mysql_num_rows($result);

if(!$num_rows)
{
echo "<table><tr><td align='center'>Sorry, no bricks matching your selection were found. </td></tr></table>";
}
else
{
echo "<table align='center' border=1><tr>";
echo "<td align='left' width=150><b>Manufacturer</b></td>";
echo "<td align='left' width=150><b>Color</b></td>";
echo "<td align='left' width=150><b>Texture</b></td>";
echo "<td align='left' width=150><b>Manufacturing Process</b></td>";
echo "<td align='center'><b>Picture</b></td>";
echo "</tr>";

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr><td align='left'>".$row["Manufacturer"]."</td>";
echo "<td align='left'>".$row["Colour"]."</td>";
echo "<td align='left'>".$row["Texture"]."</td>";
echo "<td align='left'>".$row["Manufacturing Process"]."</td>";
echo "<td align='left'>".$row["Image"]."</td>";
echo "</tr>";
}
}
echo "</table>";


?>


The info for the other drop down boxes arent getting through. I test it by echoing $manufacturer that works, echo $texture doesnt bring up anything.

any ideas?

Nightfire
04-05-2007, 03:29 PM
Because you've not named the dropdown as Texture. You've named it 'rough'

mike_tw
04-05-2007, 03:38 PM
Brilliant... all the info is getting through now i tested it with echo "$

mike_tw
04-05-2007, 03:43 PM
Everythings coming together now, i edited the query also . I added AND Colour=.$Color and it brought up the correct row.

Now I just need to build up the query:)

mike_tw
04-05-2007, 03:49 PM
Set your form up with a hidden field with a value and do this

PHP Code:
if(isset($_POST['myfield'])) {
//start from nothing just in case nothing is selected :)
$where = "";
//start your separator within the where statement.
$sep = " WHERE ";
if($Manufacturer != "SelectManufacturer") {
$where .= $sep." manufacturer = '".$Manufacturer."'";
//update your sep to AND for the next values
$sep = " AND ";
}

if($Colour != "whatever") {
$where .= $sep." colour = '".$Colour."'";
//update your sep to AND for the next values and just incase no manu was selected
$sep = " AND ";
}

//Do this for all your checkboxes then go onto this.

//so if no checkboxes were selected then you where statement will be blank and will return ALL rows. Keep that in mind.
$query = "SELECT * FROM manufacturers ".$where;
$result = mysql_query($query) or die;

Do I still use the above template since the code has changed a little?

mike_tw
04-05-2007, 03:57 PM
<?

MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
@mysql_select_db( "$dbName") or die( "Unable to select database");

function secured($val)
{
$val = strip_tags(trim(($val))) ;
$val = escapeshellcmd($val);
return stripslashes($val);
}
if (get_magic_quotes_gpc()) {
$Manufacturer = $_POST['Manufacturer'];
$Process = $_POST['Process'];
$Texture = $_POST['Texture'];
$Color = $_POST['Color'];

} else {
$Manufacturer = addslashes($_POST['Manufacturer']);
$Process = addslashes($_POST['Process']);
$Texture = addslashes($_POST['Texture']);
$Color = addslashes($_POST['Color']);

if(isset($_POST['myfield'])) {
//start from nothing just in case nothing is selected
$where = "";
//start your separator within the where statement.
$sep = " WHERE ";
if($Manufacturer != "SelectManufacturer") {
$where .= $sep." manufacturer = '".$Manufacturer."'";
//update your sep to AND for the next values
$sep = " AND ";
}

if($Colour != "SelectColour") {
$where .= $sep." colour = '".$Colour."'";
$sep = " AND ";
}

if($Texture != "Texture") {
$where .= $sep." Texture = '".$Texture."'";
$sep = " AND ";
}

if($Process != "SelectManufacturingProcess") {
$where .= $sep." process = '".$Process."'";
$sep = " AND ";
}

$query = "SELECT * FROM manufacturers ".$where;
$result = mysql_query($query) or die;
print_r($row);
$num_rows = mysql_num_rows($result);
}
echo "$Texture";
echo "$Color";
echo "$Process";
echo "$Manufacturer";
$query = "SELECT * FROM bricks WHERE Manufacturer='$Manufacturer'";
$result = mysql_query($query) or die("failed: ".mysql_error());
print_r($row);
$num_rows = mysql_num_rows($result);

if(!$num_rows)
{
echo "<table><tr><td align='center'>Sorry, no bricks matching your selection were found. </td></tr></table>";
}
else
{
echo "<table align='center' border=1><tr>";
echo "<td align='left' width=150><b>Manufacturer</b></td>";
echo "<td align='left' width=150><b>Color</b></td>";
echo "<td align='left' width=150><b>Texture</b></td>";
echo "<td align='left' width=150><b>Manufacturing Process</b></td>";
echo "<td align='center'><b>Picture</b></td>";
echo "</tr>";

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr><td align='left'>".$row["Manufacturer"]."</td>";
echo "<td align='left'>".$row["Colour"]."</td>";
echo "<td align='left'>".$row["Texture"]."</td>";
echo "<td align='left'>".$row["Manufacturing Process"]."</td>";
echo "<td align='left'>".$row["Image"]."</td>";
echo "</tr>";
}
}
echo "</table>";
}


?>


Tried it like that , blank screen

Nightfire
04-05-2007, 03:59 PM
Chuck a closing bracket on after here


} else {
$Manufacturer = addslashes($_POST['Manufacturer']);
$Process = addslashes($_POST['Process']);
$Texture = addslashes($_POST['Texture']);
$Color = addslashes($_POST['Color']);
}

aedrin
04-05-2007, 04:02 PM
Tried it like that , blank screen

Please:

- turn on display_errors (search for it, as it's a common thing)
- indent properly

mike_tw
04-05-2007, 04:07 PM
MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
@mysql_select_db( "$dbName") or die( "Unable to select database");

function secured($val)
{
$val = strip_tags(trim(($val))) ;
$val = escapeshellcmd($val);
return stripslashes($val);
}
if (get_magic_quotes_gpc()) {
$Manufacturer = $_POST['Manufacturer'];
$Process = $_POST['Process'];
$Texture = $_POST['Texture'];
$Color = $_POST['Color'];

} else {
$Manufacturer = addslashes($_POST['Manufacturer']);
$Process = addslashes($_POST['Process']);
$Texture = addslashes($_POST['Texture']);
$Color = addslashes($_POST['Color']);
}
if(isset($_POST['myfield'])) {
//start from nothing just in case nothing is selected
$where = "";
//start your separator within the where statement.
$sep = " WHERE ";
if($Manufacturer != "SelectManufacturer") {
$where .= $sep." manufacturer = '".$Manufacturer."'";
//update your sep to AND for the next values
$sep = " AND ";
}

if($Colour != "SelectColour") {
$where .= $sep." colour = '".$Colour."'";
$sep = " AND ";
}

if($Texture != "Texture") {
$where .= $sep." Texture = '".$Texture."'";
$sep = " AND ";
}

if($Process != "SelectManufacturingProcess") {
$where .= $sep." process = '".$Process."'";
$sep = " AND ";
}

$query = "SELECT * FROM manufacturers ".$where;
$result = mysql_query($query) or die;
print_r($row);
$num_rows = mysql_num_rows($result);

if(!$num_rows)
{
echo "<table><tr><td align='center'>Sorry, no bricks matching your selection were found. </td></tr></table>";
}
else
{
echo "<table align='center' border=1><tr>";
echo "<td align='left' width=150><b>Manufacturer</b></td>";
echo "<td align='left' width=150><b>Color</b></td>";
echo "<td align='left' width=150><b>Texture</b></td>";
echo "<td align='left' width=150><b>Manufacturing Process</b></td>";
echo "<td align='center'><b>Picture</b></td>";
echo "</tr>";

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr><td align='left'>".$row["Manufacturer"]."</td>";
echo "<td align='left'>".$row["Colour"]."</td>";
echo "<td align='left'>".$row["Texture"]."</td>";
echo "<td align='left'>".$row["Manufacturing Process"]."</td>";
echo "<td align='left'>".$row["Image"]."</td>";
echo "</tr>";
}
}
echo "</table>";
}
?>


I havent made a hidden form in the search form. What should the value be in this hidden form? This is probably why it is giving me a blank screen now :)

iLLin
04-05-2007, 04:08 PM
Just create it, your not checking the value.

aedrin, is there a better way to build that query out? Maybe put it all in an array?

iLLin
04-05-2007, 04:11 PM
$array = (postfield);
foreach($array as $post) {
if(isset($_POST[$post] and not empty) {
//build query
}
}


I guess it would compact it a lot :) I been trying to make my code more powerful and use a lot less of it as of late. ;)

mike_tw
04-05-2007, 04:14 PM
<input type="hidden" name="brick" value="brick">
<p><select size="1" name="Manufacturer">
<option value="Manufacturer1">Manufacturer1</option>
<option value="Manufacturer2">Manufacturer2</option>
<option value="Manufacturer3">Manufacturer3</option>
<option selected value="SelectManufacturer">SelectManufacturer</option>
</select></p>
<p><select size="1" name="Color">
<option selected value="SelectColor">Color</option>
<option value="Red">Red</option>
<option value="Blue">Blue</option>
<option value="Brown">Brown</option>
</select></p>
<p><select size="1" name="Texture">
<option selected value="Texture">Texture</option>
<option value="smooth">smooth</option>
<option value="rough">rough</option>
</select></p>
<p><select size="1" name="Process">
<option selected value="SelectManufacturingProcess">ManufacturingProcess
</option>
<option value="Wire Cut">Wire Cut</option>
<option value="Hand Made">Hand Made</option>
<option value="Stock">Stock</option>
<option value="Pressed">Pressed</option>
</select></p>
<p><input type="submit" value="Submit" name="B1"></p>
</form>




MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
@mysql_select_db( "$dbName") or die( "Unable to select database");

function secured($val)
{
$val = strip_tags(trim(($val))) ;
$val = escapeshellcmd($val);
return stripslashes($val);
}
if (get_magic_quotes_gpc()) {
$Manufacturer = $_POST['Manufacturer'];
$Process = $_POST['Process'];
$Texture = $_POST['Texture'];
$Color = $_POST['Color'];

} else {
$Manufacturer = addslashes($_POST['Manufacturer']);
$Process = addslashes($_POST['Process']);
$Texture = addslashes($_POST['Texture']);
$Color = addslashes($_POST['Color']);
}
if(isset($_POST['brick'])) {
//start from nothing just in case nothing is selected
$where = "";
//start your separator within the where statement.
$sep = " WHERE ";
if($Manufacturer != "SelectManufacturer") {
$where .= $sep." manufacturer = '".$Manufacturer."'";
//update your sep to AND for the next values
$sep = " AND ";
}

if($Colour != "SelectColour") {
$where .= $sep." colour = '".$Colour."'";
$sep = " AND ";
}

if($Texture != "Texture") {
$where .= $sep." Texture = '".$Texture."'";
$sep = " AND ";
}

if($Process != "SelectManufacturingProcess") {
$where .= $sep." process = '".$Process."'";
$sep = " AND ";
}

$query = "SELECT * FROM manufacturers ".$where;
$result = mysql_query($query) or die;
print_r($row);
$num_rows = mysql_num_rows($result);

if(!$num_rows)
{
echo "<table><tr><td align='center'>Sorry, no bricks matching your selection were found. </td></tr></table>";
}
else
{
echo "<table align='center' border=1><tr>";
echo "<td align='left' width=150><b>Manufacturer</b></td>";
echo "<td align='left' width=150><b>Color</b></td>";
echo "<td align='left' width=150><b>Texture</b></td>";
echo "<td align='left' width=150><b>Manufacturing Process</b></td>";
echo "<td align='center'><b>Picture</b></td>";
echo "</tr>";

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr><td align='left'>".$row["Manufacturer"]."</td>";
echo "<td align='left'>".$row["Colour"]."</td>";
echo "<td align='left'>".$row["Texture"]."</td>";
echo "<td align='left'>".$row["Manufacturing Process"]."</td>";
echo "<td align='left'>".$row["Image"]."</td>";
echo "</tr>";
}
}
echo "</table>";
}
?>



I've added the hidden field now, still blank

iLLin
04-05-2007, 04:26 PM
Due what aedrin said so you can see your errors.

mike_tw
04-05-2007, 04:36 PM
if(isset($_POST['brick'])) {
//start from nothing just in case nothing is selected
$where = "" OR DIE( "error1" );

Error comes up there

iLLin
04-05-2007, 04:46 PM
I didnt write the code to be copied and pasted... look up how you die out a query.

mike_tw
04-05-2007, 04:54 PM
sorry i didnt see youor port aerin

I turned on the display_errors, fixed the errors is broguth up ( undefined variables ) but still blank page

the display error does not report anything else

iLLin
04-05-2007, 05:04 PM
$result = mysql_query($query) or die;
//to this
$result = mysql_query($query) or die("Failed: ".mysql_error());

mike_tw
04-05-2007, 05:35 PM
I added that :



MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
@mysql_select_db( "$dbName") or die( "Unable to select database");

function secured($val)
{
$val = strip_tags(trim(($val))) ;
$val = escapeshellcmd($val);
return stripslashes($val);
}
if (get_magic_quotes_gpc()) {
$Manufacturer = $_POST['Manufacturer'];
$Process = $_POST['Process'];
$Texture = $_POST['Texture'];
$Color = $_POST['Color'];

} else {
$Manufacturer = addslashes($_POST['Manufacturer']);
$Process = addslashes($_POST['Process']);
$Texture = addslashes($_POST['Texture']);
$Color = addslashes($_POST['Color']);

}
if(isset($_POST['brick'])) {
$where = ""OR DIE( "error0" );

$sep = " WHERE " OR DIE( "error1.5" );
}
if($Manufacturer != "SelectManufacturer") {
$where .= $sep." manufacturer = '".$Manufacturer."'" OR DIE( "error2" );

$sep = " AND ";
}

if($Color != "SelectColour") {
$where .= $sep." colour = '".$Color."'" OR DIE( "error3" );
$sep = " AND ";
}

if($Texture != "Texture") {
$where .= $sep." Texture = '".$Texture."'";
$sep = " AND ";
}

if($Process != "SelectManufacturingProcess") {
$where .= $sep." process = '".$Process."'";
$sep = " AND ";
}

$query = "SELECT * FROM manufacturers ".$where;
$result = mysql_query($query) or die("Failed: ".mysql_error());
print_r($row);
$num_rows = mysql_num_rows($result);



But the "error0" comes up which is in these lines#



$Process = addslashes($_POST['Process']);
$Texture = addslashes($_POST['Texture']);
$Color = addslashes($_POST['Color']);

}
if(isset($_POST['brick'])) {
$where = ""OR DIE( "error0" );

aedrin
04-05-2007, 05:38 PM
$where = ""OR DIE( "error0" );


This is supposed to do something?

mike_tw
04-05-2007, 06:38 PM
Okay , dont know what i did but it looks to be fine now but it does not return all records if you only choose Manufacturer. It comes back with "Sorry no records found".

3 of the dropdowns dont work if they are selected alone. If i select Manufacturer1 and nothing else, it says no records found. If i select red as colour and nothing else, it works and brings back the records. If i select smooth as texture and nothing else it doesnt work and same for process.

However, if i select more than one, it works perfectley. Lets say i want to select the colour red and smooth texture... it works perferctley. Just 3 of them wont work if they are the only ones selected. Looking at the code, i dont see any difference between the Color , which works, and the rest.

It doesnt work either if nothing at all is selected



}
if(isset($_POST['brick'])) {
$where = "";
$sep = " WHERE " OR DIE( "error1.5" );
}
if($Manufacturer != "SelectManufacturer") {
$where .= $sep." Manufacturer = '".$Manufacturer."'" OR DIE( "error2" );

$sep = " AND ";
}

if($Color != "SelectColour") {
$where .= $sep." colour = '".$Color."'" OR DIE( "error3" );
$sep = " AND ";
}

if($Texture != "Texture") {
$where .= $sep." Texture = '".$Texture."'";
$sep = " AND ";
}

if($Process != "SelectManufacturingProcess") {
$where .= $sep." ManufacturingProcess = '".$Process."'";
$sep = " AND ";
}

$query = "SELECT * FROM bricks ".$where;
$result = mysql_query($query) or die("Failed: ".mysql_error());
$num_rows = mysql_num_rows($result);

iLLin
04-05-2007, 06:43 PM
I still think your fields are not right.

In your while($row... ) statement. Put this
print_r($row)
So it will echo the results out. I think our fields are all wrong.

mike_tw
04-05-2007, 06:46 PM
Array ( [primary] => [Manufacturer] => Manufacturer1 [Brick Name] => [Colour] => Red [Texture] => Smooth [ManufacturingProcess] => Hand Made

Thats if i select a colour

If i select only Manufacturer, it doesnt print anything but "Sorry no records"

iLLin
04-05-2007, 06:47 PM
also add in a print_r($_POST); somewhere

iLLin
04-05-2007, 06:49 PM
Also take all DIE stuff out

$where .= $sep." colour = '".$Color."'" OR DIE( "error3" );

You dont put in OR DIE there. I dunno where you got that. And look at your field "Colour" is that the same as "colour"?

mike_tw
04-05-2007, 06:54 PM
selecting manufacturer1 alone brings this:

Array ( [brick] => brick [Manufacturer] => Manufacturer1 [Color] => SelectColor [Texture] => Texture [Process] => SelectManufacturingProcess [B1] => Submit )
Sorry, no bricks matching your selection were found.

iLLin
04-05-2007, 07:01 PM
Also put in some debugging, print your query out and see how its built. Make sure its correct, print values out here and there. w/e Just so you can see what the code is actually doing.

mike_tw
04-10-2007, 12:57 PM
if(isset($_POST['brick'])) {
$where = "";
$sep = " WHERE ";
}

if($Manufacturer != "SelectManufacturer") {
$where .= $sep." Manufacturer = ".$Manufacturer;

if($Color != "Colour" || ($Texture != "Texture") || ($Process != "ManufacturingProcess") {
$sep = " AND ";
}
}
if($Color != "Colour") {
$where .= $sep." colour = ".$Color;
if($Texture != "Texture") || ($Process != "ManufacturingProcess") {
$sep = " AND ";
}
}
if($Texture != "Texture") {
$where .= $sep." Texture =".$Texture;
if($Process != "ManufacturingProcess") {
$sep = " AND ";
}
}
if($Process != "ManufacturingProcess") {
$where .= $sep." ManufacturingProcess =".$Process;
}
}
if($Color == "Colour" || ($Texture == "Texture") || ($Process == "ManufacturingProcess") {
echo "<table align='center'><tr><td><b>Missing Input can not perform empty query";
}
else{
$query = "SELECT * FROM bricks ".Print($where);
$result = mysql_query($query) or die("Failed: ".mysql_error());
$num_rows = mysql_num_rows($result);


I tried it like this but it brings up

Parse error: syntax error, unexpected '{' in /home/thetdco/public_html/brick/results.php on line 41

tried allsorts to fix that one... where did i miss a { or whatever?

iLLin
04-10-2007, 02:22 PM
Man that is nothing like I showed you. And you can't just print within your query.

aedrin
04-10-2007, 03:05 PM
I realized we also need a topic that explains you can't try coding more advanced stuff, without knowing the basics.

Buy/find a book or take a class. It would be fruitless for us to explain basic programming to you as there are plenty free resources available to do it for us.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum