Dropdown list - populated by a mysql db to search another mysql table

ok so i tried that and nothing is changing, here is my code:

[php]

<?php try { require_once("./config.php"); //--------------------------------------------------------------------------------- // Trim $_POST Array //--------------------------------------------------------------------------------- $_POST = array_map('trim', $_POST); $sql = "SELECT publisher_name FROM publishers WHERE publisher_id=?"; $stmt = $pdo->prepare($sql); $stmt->execute(array( $_POST['publisher_id']=4 ));
 $result = $stmt->fetchAll();
 
 if (count($result))
     {
     foreach ($result as $row)
         {
         echo '<pre>';
         print_r($row);
         echo '</pre>';
         }
     }
 else
     {
     echo "No rows returned.";
     }
 }

catch (PDOException $e)
{
include_once(’./config/pdo_catch_error.php’);
}
?>

[/php]

First view the page source in the browser. Click on the css link and make sure the css file opens.

If not, link to css is not right.

If you see the css file, turn back on the checksetup to 1 and turn debugging to 1. You will see colored messge boxes. Turn off the check setup. Leave debugging to 1 while you are developing.

If your project is on the web send me the url.

I turned of the check setups and left the debugging to 1

Is this what im supposed to be seeing


Yes. You can turn it back off.

This might help a little:
[php] $sql = “SELECT publisher_name FROM publishers WHERE publisher_id=:publisher_id”;
$stmt = $pdo->prepare($sql);
// Grab the corresponding record(s) from the user’s input $_POST[‘publisher_id’]
$stmt->execute(array(’:publisher_id’ => $_POST[‘publisher_id’]));[/php]

This is what i get when i use your method:

Notice: Undefined index: publisher_id in C:\xampp\htdocs\search2.php on line 17
No rows returned.

[php] $sql = “SELECT publisher_name FROM publishers WHERE publisher_id=:publisher_id”;
$stmt = $pdo->prepare($sql);
$stmt->execute(array(’:publisher_id’ => $_POST[‘publisher_id’]));[/php]

Unless i put the =4

[php] $sql = “SELECT publisher_name FROM publishers WHERE publisher_id=:publisher_id”;
$stmt = $pdo->prepare($sql);
$stmt->execute(array(’:publisher_id’ => $_POST[‘publisher_id’]=4));[/php]

This is wrong

[php] $_POST[‘publisher_id’]=4[/php]

It is expecting its data from a submitted form.

If you just want to test it and get a result without a form do this

[php]$stmt->execute(array(’:publisher_id’ =>4));[/php]

Oh right i see thanks :slight_smile:

So i just made a form with a dropdown list. It works fine but im still getting this:

Notice: Undefined index: publisher_id in C:\xampp\htdocs\search2.php on line 25
No rows returned.

[code]

DC Dark Horse <?php try { require_once("./config.php"); //--------------------------------------------------------------------------------- // Trim $_POST Array //--------------------------------------------------------------------------------- $_POST = array_map('trim', $_POST); $sql = "SELECT publisher_name FROM publishers WHERE publisher_id=?"; $stmt = $pdo->prepare($sql); $stmt->execute(array($_POST['publisher_id']));[/code]

dropdowntest.png

If that is your complete form it wont work. You have no form action. Forget the form for now. Do this:

[php] $_POST = array_map(‘trim’, $_POST);

    //Testing
    $_POST['publisher_id']=4;


   $sql  = "SELECT publisher_name FROM publishers WHERE publisher_id=?";
   $stmt = $pdo->prepare($sql);
   $stmt->execute(array($_POST['publisher_id']));[/php]

Ok i have done that.

Anything else?

Are you having a problem?

No problems but i need help changing my old search.php code to PDO or mysqli and i would like to have a working dropdown list (populated with the data from the publisher table) and search box. Im having trouble changing the code as im not familiar with PDO or mysqli.

My old code form search.php / (The dropdown list dosent work here. I couldent get it to work hence me creating this topic.)

[code]

    <select name="$dropdown" style="font-family: 'ariel'; font-size: 17px; width: 190px; height: 27px; position:absolute; left: 383px; top: 256px">
        <option>-----------</option>
		<?php query() ?>
	   </select>
        <?php close() ?>
    
      <input type="text" name="k" size="50" onfocus="if (this.name=='k') this.name=''; value="<?php echo $_GET["k"]; ?>" style="width: 203px; position:absolute; left: 586px; top: 256px; height: 27px; right: 567px; border:thin red solid" />
<input id="button" type="submit" value="Search" style="position:absolute; left: 800px; top: 256px; height: 27px;" />
<input type="reset" id="button" style="position:absolute; left: 873px; top: 256px; height: 27px; width: 57px; value="Reset"/>
[/code]

The search code.

[php] <?php
$k = $_GET[‘k’];
$terms = explode(" ", $k);
$query = "SELECT * FROM search WHERE ";

	foreach ($terms as $each){	
        $i++;
		if ($i == 1)
		    $query .= "keywords LIKE '%$each%' ";
		else
            $query .= "OR keywords LIKE '%$each%' ";
		
	}
	
	// connect
	mysql_connect(DB_HOST,DB_USER,DB_PASS);
	mysql_select_db("search");
	
	$query = mysql_query($query);
	$numrows = mysql_num_rows($query);
	if ($numrows > 0){
	    
		while ($row = mysql_fetch_assoc($query)){
		    $id = $row['id'];
			$title = $row['title'];
			$description = $row['description'];
			$keywords = $row['keywords'];
			$link = $row['link'];
			
			echo "<br /><h3><a href='$link'>$title</a></h3>
			$description";
	    }
	
	}
	else
	    echo "No results found for \"<b>$k</b>\"";
		
	//disconnect
	mysql_close();
	

?>[/php]

If all of this works i would be happy to donate :slight_smile:

I am working on something at the moment. I should be able to get to it in a few hours.

I have added a complete Bootstrapped PDO Database download here:

http://www.phphelp.com/forum/the-occasional-tutorial/beginners-pdo-bumpstart-code-use-it-now!/

Delete all the files you copied before and use the download database. It is mostly the same with some updates, added Bootstrap and a working database

Ok so i did that and im getting “Direct File Access Prohibited” when i open it up on the localhost.

In the download version, all files need to be opened in the browser from index.php. The default file that displays without doing anything is includes/default.php

You should not have to do anything for it to display an array from the users table. I just uploaded a minor change to the download. Either re-download the zip or remove the two comment slashes at the bottom of default.php

Change
[php]//include_once(’./config/pdo_catch_error.php’);[/php]

TO [php]include_once(’./config/pdo_catch_error.php’);[/php]

Other than that, it should just work. I am working on a version with more functionality to learn from. Keep an eye on the download page once in awhile. Current version is 1.1

ok so iv tried to do everything you have said.

Is this what the index.php is supposed to look like.


Yes. What you downloaded is just basic to get you instantly running with PDO. Now that you have it working, you can customize it to your needs. Add your tables to the same database for now so you dont have to change any settings. Experiment outputting your own tables on page 2. It is in includes/page2.php. It is already linked up in the menu so you can view results from there. You cannot call any pages directly.

Sponsor our Newsletter | Privacy Policy | Terms of Service