smd8061 Posted January 11, 2008 Share Posted January 11, 2008 I am trying to search my database and show the results, but I don't really know what I am doing. There are not any error messages showing up in PUTTY but it will not search and bring back my information. WHAT AM I DOING WRONG!?!?!?!?!? Please Help Me! Here is my code: <html> <body> <form id="form1" name="form1" method="post" action=""> <label> <select name="$select" size="1"> <option value="PO Number" selected="selected">PO Number</option> <option value="Invoice ID">Invoice ID</option> <option value="Vendor ID">Vendor ID</option> <option value="Material ID">Material ID</option> <option value="Date">Date</option> <option value="Quantity">Quantity</option> <option value="Estimated Total">Estimated Total</option> <option value="Total Price">Total Price</option> <option value="Due Date">Due Date</option> </select> <br /> <br /> <input type="text" name="textfield" /> </label> <label> <input type="submit" name="Submit" value="Submit" /> </label> <label> <input type="reset" name="Submit2" value="Reset" /> </label> </form> <?php // Connecting, selecting database $link = mysql_connect('HOST', 'USERNAME', 'PASSWORD') or die('Could not connect: ' . mysql_error()); mysql_select_db('software') or die('Could not select database'); // Performing SQL query $select = '$GET[$select]'; $query = "SELECT * FROM Master WHERE 'HEADING' = '$select'"; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); // Printing Results echo "<table border=1 padding=10>\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "\t<tr>\n"; foreach ($line as $col_value) { echo "\t\t<td>$col_value</td>\n"; } echo "\t</tr>\n"; } echo "</table>\n"; // Free resultset mysql_free_result($result); // Closing connection mysql_close($link); ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
revraz Posted January 11, 2008 Share Posted January 11, 2008 Remove the single quotes around your field name here for one $query = "SELECT * FROM Master WHERE HEADING = '$select'"; Quote Link to comment Share on other sites More sharing options...
twostars Posted January 11, 2008 Share Posted January 11, 2008 <html> <body> <form id="form1" name="form1" method="post" action=""> <label> <select name="$select" size="1"> <option value="PO Number" selected="selected">PO Number</option> <option value="Invoice ID">Invoice ID</option> <option value="Vendor ID">Vendor ID</option> <option value="Material ID">Material ID</option> <option value="Date">Date</option> <option value="Quantity">Quantity</option> <option value="Estimated Total">Estimated Total</option> <option value="Total Price">Total Price</option> <option value="Due Date">Due Date</option> </select> <input type="text" name="textfield" /> </label> <label> <input type="submit" name="Submit" value="Submit" /> </label> <label> <input type="reset" name="Submit2" value="Reset" /> </label> </form> <?php // Connecting, selecting database $link = mysql_connect('HOST', 'USERNAME', 'PASSWORD') or die('Could not connect: ' . mysql_error()); mysql_select_db('software') or die('Could not select database'); // Performing SQL query $select = $GET[$select]; $query = "SELECT * FROM Master WHERE HEADING = '$select'"; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); // Printing Results echo "<table border=1 padding=10>\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "\t<tr>\n"; foreach ($line as $col_value) { echo "\t\t<td>$col_value</td>\n"; } echo "\t</tr>\n"; } echo "</table>\n"; // Free resultset mysql_free_result($result); // Closing connection mysql_close($link); ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
smd8061 Posted January 11, 2008 Author Share Posted January 11, 2008 I removed the single quotes from around HEADING and it showed this under my forms. Query failed: Unknown column 'HEADING' in 'where clause' Quote Link to comment Share on other sites More sharing options...
revraz Posted January 11, 2008 Share Posted January 11, 2008 And what is your field name? Probably not HEADING Quote Link to comment Share on other sites More sharing options...
nikefido Posted January 11, 2008 Share Posted January 11, 2008 $select = '$GET[$select]'; If i'm not mistaken, you are making $select a string here, not the actual $_GET value you are trying for. Also, the syntax is $_GET not $GET Quote Link to comment Share on other sites More sharing options...
smd8061 Posted January 11, 2008 Author Share Posted January 11, 2008 i will try the $_GET, and revraz I do not get what you mean by field name. Quote Link to comment Share on other sites More sharing options...
smd8061 Posted January 11, 2008 Author Share Posted January 11, 2008 I thought that there might be a syntax that lets you search by the headings of the databases. that is why i tried HEADING. Quote Link to comment Share on other sites More sharing options...
twostars Posted January 11, 2008 Share Posted January 11, 2008 $select = '$GET[$select]'; If i'm not mistaken, you are making $select a string here, not the actual $_GET value you are trying for. Also, the syntax is $_GET not $GET Nice pick up there. I fixed the quotes around it, but failed to notice it was missing a _ D'oh. Quote Link to comment Share on other sites More sharing options...
twostars Posted January 11, 2008 Share Posted January 11, 2008 I thought that there might be a syntax that lets you search by the headings of the databases. that is why i tried HEADING. The correct term for that, is 'column'. A database is consisted of tables, each of which is made up by rows (your data) and columns (what your data is sorted into). Quote Link to comment Share on other sites More sharing options...
smd8061 Posted January 11, 2008 Author Share Posted January 11, 2008 ok so if i change HEADING for COLUMN it should work? Quote Link to comment Share on other sites More sharing options...
revraz Posted January 11, 2008 Share Posted January 11, 2008 No, doesn't work like that. Quote Link to comment Share on other sites More sharing options...
nikefido Posted January 11, 2008 Share Posted January 11, 2008 ok so if i change HEADING for COLUMN it should work? nope. can you show us how your table is structured? And what info you are trying to get out of it? Quote Link to comment Share on other sites More sharing options...
smd8061 Posted January 11, 2008 Author Share Posted January 11, 2008 OK, i guess i wasn't clear on what i was trying to get it to do. I have a drop down box and i want to be able to select a columns name and then in the text box input a number, and when you hit search it will search that number in one of the columns IE: the one you selected in the drop down box. That is where the $select comes from it is the drop down box. Quote Link to comment Share on other sites More sharing options...
twostars Posted January 11, 2008 Share Posted January 11, 2008 ok so if i change HEADING for COLUMN it should work? Say my table is called CATS. My CATS table consists of these columns: - Name - Color - Breed - DoB If I wanted to select all the names, I would use: SELECT Name FROM CATS However, if I wanted to selet the Color and Breed, I would use: SELECT Color, Breed FROM CATS Finally, if I wanted to select all the data I could either do: SELECT Name, Color, Breed, DoB FROM CATS or SELECT * FROM CATS Hope that helps in some way. Its all dependant on your column names. Quote Link to comment Share on other sites More sharing options...
smd8061 Posted January 11, 2008 Author Share Posted January 11, 2008 Say my table is called CATS. My CATS table consists of these columns: - Name - Color - Breed - DoB If I wanted to select all the names, I would use: SELECT Name FROM CATS However, if I wanted to selet the Color and Breed, I would use: SELECT Color, Breed FROM CATS Finally, if I wanted to select all the data I could either do: SELECT Name, Color, Breed, DoB FROM CATS or SELECT * FROM CATS I get that, I just don't know how I would pull the info using forms to select my column name. my columns are: - PO Number - Invoice ID - Vendor ID - Material ID - Date - Quantity - Estimated Total - Total Price - Due Date Quote Link to comment Share on other sites More sharing options...
smd8061 Posted January 11, 2008 Author Share Posted January 11, 2008 Hello Any Body????????? Quote Link to comment Share on other sites More sharing options...
twostars Posted January 11, 2008 Share Posted January 11, 2008 Say my table is called CATS. My CATS table consists of these columns: - Name - Color - Breed - DoB If I wanted to select all the names, I would use: SELECT Name FROM CATS However, if I wanted to selet the Color and Breed, I would use: SELECT Color, Breed FROM CATS Finally, if I wanted to select all the data I could either do: SELECT Name, Color, Breed, DoB FROM CATS or SELECT * FROM CATS I get that, I just don't know how I would pull the info using forms to select my column name. my columns are: - PO Number - Invoice ID - Vendor ID - Material ID - Date - Quantity - Estimated Total - Total Price - Due Date Okay. If I understand you correctly, something like this?.. <html> <body> <form id="form1" name="form1" method="post" action=""> <label> <select name="select" size="1"> <option value="PO Number" selected="selected">PO Number</option> <option value="Invoice ID">Invoice ID</option> <option value="Vendor ID">Vendor ID</option> <option value="Material ID">Material ID</option> <option value="Date">Date</option> <option value="Quantity">Quantity</option> <option value="Estimated Total">Estimated Total</option> <option value="Total Price">Total Price</option> <option value="Due Date">Due Date</option> </select> <input type="text" name="textfield" /> </label> <label> <input type="submit" name="Submit" value="Submit" /> </label> <label> <input type="reset" name="Submit2" value="Reset" /> </label> </form> <?php // Connecting, selecting database $link = mysql_connect('HOST', 'USERNAME', 'PASSWORD') or die('Could not connect: ' . mysql_error()); mysql_select_db('software') or die('Could not select database'); // Performing SQL query $table_name = "invoices"; #Change $select = $_POST["select"]; $textfield = $_POST["textfield"]; $query = "SELECT * FROM $table_name WHERE $select = '$textfield'"; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); // Printing Results echo "<table border=1 padding=10>\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "\t<tr>\n"; foreach ($line as $col_value) { echo "\t\t<td>$col_value</td>\n"; } echo "\t</tr>\n"; } echo "</table>\n"; // Free resultset mysql_free_result($result); // Closing connection mysql_close($link); ?> </body> </html> A little unclear about your "WHERE" though. Change $table_name to the table name, if its different. Its not very wise naming an element like a PHP variable. Its not going to work at any time. Also, $_GET is for retrieving things in the URL (ie: index.php?select=BLAH) as opposed to straight through the form ($_POST). Edit: added some follow-up info. Quote Link to comment Share on other sites More sharing options...
revraz Posted January 11, 2008 Share Posted January 11, 2008 You need to give people more than 5 minutes to reply. We're not all just sitting here waiting for you to ask a question. Hello Any Body????????? Quote Link to comment Share on other sites More sharing options...
smd8061 Posted January 11, 2008 Author Share Posted January 11, 2008 You need to give people more than 5 minutes to reply. We're not all just sitting here waiting for you to ask a question. Quote from: smd8061 on Today at 10:03:23 AM Hello Any Body?HuhHuh?? Sorry I just am tired of trying to get this to work. and thank you very much twostars I will try this. Quote Link to comment Share on other sites More sharing options...
revraz Posted January 11, 2008 Share Posted January 11, 2008 May want to read this http://www.phpfreaks.com/forums/index.php/topic,6264.0.html Sorry I just have a deadline that I must meet, this is for a school project. Quote Link to comment Share on other sites More sharing options...
smd8061 Posted January 11, 2008 Author Share Posted January 11, 2008 Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Number = '1'' at line 1 Twostars I tried it and it says this. Quote Link to comment Share on other sites More sharing options...
adam291086 Posted January 11, 2008 Share Posted January 11, 2008 echo out $query and copy in the result so we can see the sql command Quote Link to comment Share on other sites More sharing options...
smd8061 Posted January 11, 2008 Author Share Posted January 11, 2008 echo out $query and copy in the result so we can see the sql command I did that and this is what it says: Query failed: Query was empty Quote Link to comment Share on other sites More sharing options...
twostars Posted January 11, 2008 Share Posted January 11, 2008 echo out $query and copy in the result so we can see the sql command I did that and this is what it says: Query failed: Query was empty [quote author=smd8061 link=topic=176679.msg784231#msg784231 date=1200063536] [quote]Say my table is called CATS. My CATS table consists of these columns: - Name - Color - Breed - DoB If I wanted to select all the names, I would use: SELECT Name FROM CATS However, if I wanted to selet the Color and Breed, I would use: SELECT Color, Breed FROM CATS Finally, if I wanted to select all the data I could either do: SELECT Name, Color, Breed, DoB FROM CATS or SELECT * FROM CATS[/quote] I get that, I just don't know how I would pull the info using forms to select my column name. my columns are: - PO Number - Invoice ID - Vendor ID - Material ID - Date - Quantity - Estimated Total - Total Price - Due Date [/quote] Okay. If I understand you correctly, something like this?.. [code] <html> <body> <form id="form1" name="form1" method="post" action=""> <label> <select name="select" size="1"> <option value="PO Number" selected="selected">PO Number</option> <option value="Invoice ID">Invoice ID</option> <option value="Vendor ID">Vendor ID</option> <option value="Material ID">Material ID</option> <option value="Date">Date</option> <option value="Quantity">Quantity</option> <option value="Estimated Total">Estimated Total</option> <option value="Total Price">Total Price</option> <option value="Due Date">Due Date</option> </select> <input type="text" name="textfield" /> </label> <label> <input type="submit" name="Submit" value="Submit" /> </label> <label> <input type="reset" name="Submit2" value="Reset" /> </label> </form> <?php // Connecting, selecting database $link = mysql_connect('HOST', 'USERNAME', 'PASSWORD') or die('Could not connect: ' . mysql_error()); mysql_select_db('software') or die('Could not select database'); // Performing SQL query $table_name = "invoices"; #Change $select = $_POST["select"]; $textfield = $_POST["textfield"]; $query = "SELECT * FROM $table_name"; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); // Printing Results echo "<table border=1 padding=10>\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "\t<tr>\n"; foreach ($line as $col_value) { echo "\t\t<td>$col_value</td>\n"; } echo "\t</tr>\n"; } echo "</table>\n"; // Free resultset mysql_free_result($result); // Closing connection mysql_close($link); ?> </body> </html> I took off the WHERE bit. Told you I was unsure if you wanted that. lol[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.