nbbcj Posted August 21, 2010 Share Posted August 21, 2010 Hi there. Im a noob to sql and php not sure if this is right place to post, Im trying to get a dynamic drop down menu to show the 1st column in my sql database the column is called cat and holds category info ie audio, internet, music ect. ( i have no idea how to do lol ) it has taken me 2 days to find and edit this the bold and underline'd bit is what im trying to change with the dropdown menu. Or thinking bout it is there a way to do it with the URL. IE.. page name.php?cat=audio ? would that be easer ? is there any security issues with doing it that way ? Code================================== $db_host = '*******'; $db_user = '******'; $db_pwd = '*****; $database = 'nbbcj_co_uk'; $table = 'penapps'; if (!mysql_connect($db_host, $db_user, $db_pwd)) die("Can't connect to database"); if (!mysql_select_db($database)) die("Can't select database"); // sending query $result = mysql_query("SELECT * FROM {$table} WHERE `cat` = 'audio' LIMIT 10 "); if (!$result) { die("Query to show fields from table failed"); } $fields_num = mysql_num_fields($result); //echo "<h1>Table: {$table}</h1>"; echo "<table border='1' width='100%'><tr>"; // printing table headers for($i=0; $i<$fields_num; $i++) { $field = mysql_fetch_field($result); //echo "<td>{$field->name}</td>"; } echo "</tr>\n"; // printing table rows while($row = mysql_fetch_row($result)) { echo "<tr>"; // $row is array... foreach( .. ) puts every element // of $row to $cell variable foreach($row as $cell) echo "<td>$cell</td>"; echo "</tr>\n"; } mysql_free_result($result); ?> code end ====================== the test page can been seen here http://www.nbbcj.co.uk/testd/1/test1.php Any more questions let me know Thanks for any help you can give, a we all have to start some ware lol thanks kaine. Quote Link to comment https://forums.phpfreaks.com/topic/211360-noob-dynamic-drop-down-menu-to-change-query-or-result-help/ Share on other sites More sharing options...
trq Posted August 21, 2010 Share Posted August 21, 2010 $cat = mysql_real_escape_string($_POST['D1'])); $result = mysql_query("SELECT * FROM {$table} WHERE `cat` = '$cat' LIMIT 10 "); Quote Link to comment https://forums.phpfreaks.com/topic/211360-noob-dynamic-drop-down-menu-to-change-query-or-result-help/#findComment-1102042 Share on other sites More sharing options...
nbbcj Posted August 21, 2010 Author Share Posted August 21, 2010 hi thorpe i have entered the code here // sending query $cat = mysql_real_escape_string($_POST['D1'])); $result = mysql_query("SELECT * FROM {$table} WHERE `cat` = '$cat' LIMIT 10 "); if (!$result) { die("Query to show fields from table failed"); } and now the page is blank, is that the way to do it for the URL way ???? thanks kaine Quote Link to comment https://forums.phpfreaks.com/topic/211360-noob-dynamic-drop-down-menu-to-change-query-or-result-help/#findComment-1102047 Share on other sites More sharing options...
trq Posted August 21, 2010 Share Posted August 21, 2010 The $_POST method would work work the form you previously posted. Of course, you also need to wrap all your code within a check to see if the form has been submitted or not. Quote Link to comment https://forums.phpfreaks.com/topic/211360-noob-dynamic-drop-down-menu-to-change-query-or-result-help/#findComment-1102049 Share on other sites More sharing options...
nbbcj Posted August 21, 2010 Author Share Posted August 21, 2010 Hi again sorry im such a newbie could u take a look at this and correct as needed thank u in advance this is hard think i need to read more books lol <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>test page one</title> </head> <body> Welcome to the test page <form method="POST" action="test1.php"> <select size="1" name="D1"> <option value="audio">audio</option> <option value="internet">internet</option> <option value="file man">file man</option> <option value="games">games</option> </select><input type="submit" value="Submit"></p> </form> <?php $db_host = '****; $db_user = '****'; $db_pwd = '*****'; $cat = mysql_real_escape_string($_POST['D1'])); // when there is 2) the page is blank no html or page tital with 1) the html and tital is there but no db results and the ddm dont do ne thing // $database = 'nbbcj_co_uk'; $table = 'penapps'; if (!mysql_connect($db_host, $db_user, $db_pwd)) die("Can't connect to database"); if (!mysql_select_db($database)) die("Can't select database"); // sending query $result = mysql_query("SELECT * FROM {$table} WHERE `cat` = '$cat' LIMIT 10 "); if (!$result) { die("Query to show fields from table failed"); } $fields_num = mysql_num_fields($result); //echo "<h1>Table: {$table}</h1>"; echo "<table border='1' width='100%'><tr>"; // printing table headers for($i=0; $i<$fields_num; $i++) { $field = mysql_fetch_field($result); //echo "<td>{$field->name}</td>"; } echo "</tr>\n"; // printing table rows while($row = mysql_fetch_row($result)) { echo "<tr>"; // $row is array... foreach( .. ) puts every element // of $row to $cell variable foreach($row as $cell) echo "<td>$cell</td>"; echo "</tr>\n"; } mysql_free_result($result); ?> </body> </html> the page with only 1 ) in the above code shows this www.nbbcj.co.uk/testd/1/test1.php Quote Link to comment https://forums.phpfreaks.com/topic/211360-noob-dynamic-drop-down-menu-to-change-query-or-result-help/#findComment-1102067 Share on other sites More sharing options...
trq Posted August 21, 2010 Share Posted August 21, 2010 mysql_real_escape_string only works when there is an active connection. Move it. Your php also needs to go within a check for the forms submission, otherwise it will generate errors. eg; if (isset($_POST['submit'])) { // the rest of your php code } You need to turn on error reporting and display errors when developing. <?php error_reporting(E_ALL) ; ini_set('display_errors','1'); ?> As for this.... // when there is 2) the page is blank no html or page tital with 1) the html and tital is there but no db results and the ddm dont do ne thing // I have no idea what your talking about. Quote Link to comment https://forums.phpfreaks.com/topic/211360-noob-dynamic-drop-down-menu-to-change-query-or-result-help/#findComment-1102070 Share on other sites More sharing options...
nbbcj Posted August 21, 2010 Author Share Posted August 21, 2010 WOOP WOOP WOOP It works now THANK YOU so much so simple a little bit of code can change every thing Ill enter the code at end of post case it can help some one else. this bit of code $cat = mysql_real_escape_string($_POST['D1'])); is what i was on bout with )); at the end it didnt work but like this ); it works for me. THANK YOU!!!! again thorpe your a god among men Heres the code ########################################################### <?php error_reporting(E_ALL) ; ini_set('display_errors','1'); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>test page one</title> </head> <body> Welcome to the test page # <form method="POST" action="test1.php"> <select size="1" name="D1"> <option value="audio">audio</option> <option value="internet">internet</option> <option value="file man">file man</option> <option value="games">games</option> </select><input type="submit" value="Submit"></p> </form> <? $db_host = '*******'; $db_user = '******'; $db_pwd = '******'; $database = '*****'; $table = '*********'; if (!mysql_connect($db_host, $db_user, $db_pwd)) die("Can't connect to database"); if (!mysql_select_db($database)) die("Can't select database"); // sending query $cat = mysql_real_escape_string($_POST['D1']); $result = mysql_query("SELECT * FROM {$table} WHERE `cat` = '$cat' LIMIT 10 "); if (!$result) { die("Query to show fields from table failed"); } $fields_num = mysql_num_fields($result); //echo "<h1>Table: {$table}</h1>"; echo "<table border='1' width='100%'><tr>"; // printing table headers for($i=0; $i<$fields_num; $i++) { $field = mysql_fetch_field($result); //echo "<td>{$field->name}</td>"; } echo "</tr>\n"; // printing table rows while($row = mysql_fetch_row($result)) { echo "<tr>"; // $row is array... foreach( .. ) puts every element // of $row to $cell variable foreach($row as $cell) echo "<td>$cell</td>"; echo "</tr>\n"; } mysql_free_result($result); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/211360-noob-dynamic-drop-down-menu-to-change-query-or-result-help/#findComment-1102082 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.