Jump to content

Search only works with Table column name


rgrady3

Recommended Posts

I have tried several different codes to create a database search of a table that includes column name "Product," "Price" and "InStock."  I am trying to search the "Product column and in each code when I enter a term I know to be in the table it comes up with an error (usually "Unknown Column 'input' in 'where' or

mysql_fetch_array(): supplied argument is not a valid MySQL result resource in").

 

The only way the search works is if I enter the actual name of the Column (in this case "Product") and it displays the entire table.

 

Please help....

Link to comment
https://forums.phpfreaks.com/topic/41576-search-only-works-with-table-column-name/
Share on other sites

The old code with the original problem looked like this:

 

$Link = mysql_connect($Host, $User, $Password);

$Query="SELECT * from $TableName WHERE Product = $productid";

$Result= mysql_db_query ($DBName, $Query, $Link);

 

Here is the new code per the suggestion:

 

$Link = mysql_connect($Host, $User, $Password);

$Query="SELECT Product, Price, InStock from $TableName WHERE Product = '$productid'";

$Result= mysql_db_query ($DBName, $Query, $Link);

 

Now I get a blank page when I search for anything but "Product."  When I search "Product" I get the following output:

 

Product:Product, Price: Price In Stock: InSto

 

Print Code is as follows:

 

while ($Row = mysql_fetch_array ($Result))

{

print (" Product:$Row[Product], Price: $Row[Price] In Stock: $Row[inStock]<BR> ");

}

CORRECTION: I now get no output or error except the echoed input from the search form.  Here is the entire code:

 

<?php

 

echo "$productid";

 

$Host="localhost";

$User="xxxx";

$Password="xxxx";

$DBName="xxxx";

$TableName="xxxxxx";

 

$Link = mysql_connect($Host, $User, $Password);

$Query= "SELECT Product, Price, InStock from $TableName WHERE Product = '$productid'";

$Result= mysql_db_query ($DBName, $Query, $Link);

 

while ($Row = mysql_fetch_array ($Result))

 

{

print (" Product: $Row[Product], Price: $Row[Price] In Stock: $Row[inStock]<BR> ");

}

mysql_close ($Link);

Does this work? String values as array indexes should be quoted.

while ($Row = mysql_fetch_array ($Result))

{
printf (" Product: %s, Price: %8.2f  In Stock: %s<BR> ", $Row['Product'], $Row['Price'], $Row['InStock']);
}

Still get the same result.  To make sure things were working I changed "Product" in my $Query line to include a typo and got back an error "Unknown column 'Produc' in 'where clause' so at least that part is working.  Should I attempt a different print method?

I tried it without the where clause and it printed the entire table.

 

What I do not understand is I echo $productid as the first command and it displays every time including this time when I inputted the search value but did not have the WHERE clause in my SELECT line.

DECIMAL (8,2) would be a better choice for a currency field, however, change print line to

 

 

printf (" Product: %s, Price: %s  In Stock: %s<BR> ", $Row['Product'], $Row['Price'], $Row['InStock']);

 

 

and see if that helps

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.