Populate textbox based off of scroll down list using php and mysql

I’m trying to populate a textbox based off of the drop down list. However, it’s only grabing one thing from the database in a for loop. Here is the code for more information. It also, have comments in them. Also, I’m using PDO.

<?php
	try
	{
		//Database config
		$db_host = 'localhost';
		$db_user = 'root';
		$db_pass = 'password01';
		$db_database = 'scroll';

		//end config

		$db = new PDO('mysql:host='.$db_host.';
										dbname='.$db_database, $db_user, $db_pass);
		$db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	}
	catch(PDOException $e)
	{
		echo $e->getMessage();
	}
?>
    <select>
        <option value="0">Select</option>
    <?php
    //This is connected to a database
	include('connect.php');
    //Using query from phpmyadmin to grab the information
	$result = $db->prepare("SELECT title FROM down");
	$result->execute();
    //It's running in a loop of the selected query
	for($i=0; $row = $result->fetch(); $i++)
	{
		$title = $row['title'];
        //Below is showing the specific title like
        //Java, PHP, JSON, language name in a scroll down list
    ?>
        <option value="<?php echo $title; ?>"><?php echo $title; ?></option>
    <?php
        }
    ?>
    </select>
    <?php
    //Trying to grab the rest of the information from the same table
    //By it's title name which is from above
    //Also, running from a loop too
	$result = $db->prepare("SELECT content, ref FROM down WHERE title = '$title'");
	$result->execute();
	for($i=0; $row = $result->fetch(); $i++)
	{
        $content = $row['content'];
        $ref = $row['ref'];
    ?>
		<form method="post" action="">
			<h3>Modifiy Content</h3>
            
            <!--
            Below where the php code is, where I'm supposed to output the content and all from the
            database into there.
            -->
				<textarea readonly type="text" rows="6" cols="60" class="format" name="myInp" id="myInp"><?php echo $content; ?></textarea>
				<textarea name="java" type="text" rows="6" cols="60" class="format"><?php echo $content; ?></textarea>
			<br/>
			<h3>Modifiy Links</h3>
				<textarea readonly type="text" rows="6" cols="60" class="format" name="myInp" id="myInp"><?php echo $ref; ?></textarea>
				<textarea name="java" type="text" rows="6" cols="60" class="format"><?php echo $ref; ?></textarea>
			<br/>
            
            <!--
            Like I said where the drop down list is suppose to show the language and then click on the language.
            Then it shows the different values for each language in content and references.
            -->
				<button name="submit" onclick="if (confirm('Are you sure you want to continue?')) commentDelete(1); return false" >Submit</button>
				<br/>
				<br/>
		</form>
    <?php
    }
    ?>

My advice is you’re trying to do too many things at once. I first would populate the dropdown menu ( tags) first with straight php (easiest) or php, javascript, ajax and json (this is what I think you’re attempting to do?)

The populate the textbox with straight php (easiest) or php, javascript, ajax and json (once again). If I find time I’ll help you out with a script or maybe (probably) someone else will in the meantime.

Is the select in a form? How are you deciding which values was chosen?

[php]$result = $db->prepare("SELECT content, ref FROM down WHERE title = '$title'");[/php]
You are using prepared statements. Use them properly, [php] $result = $db->prepare("SELECT content, ref FROM down WHERE title = ?"); $result->execute([$title]); [/php]

I would advise using the id, not the title.

[php]for($i=0; $row = $result->fetch(); $i++)[/php]
This should either be a while loop,

[php]while( $row = $result->fetch() ) {[/php]
or just use fetchAll to grab all of the result set and iterate over them.

Sponsor our Newsletter | Privacy Policy | Terms of Service