Jump to content

Noob! dynamic drop down menu to change query or result help!!!!


nbbcj

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

WOOP WOOP WOOP

 

It works now THANK YOU so much  :D 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  O0

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>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.