Jump to content

[SOLVED] Got the dropdown menu populated from MySQL, but not the results after submit


suttercain

Recommended Posts

I have a dropdown menu populated from the MySQL database in a form. When I hit submit I want the results of the selection to be displayed... but I can't get it.

 

FORM CODE

<form action="comic_index_view.php" method="post">
			  <?php 
			  $result = mysql_query("SELECT title FROM comics GROUP BY title") or die(mysql_error());
				  $options="";
			  echo "<select>";
				  while($row = mysql_fetch_array($result)){
				  $title=$row["title"];
				  echo "<option value'" . $title . "'>".$title.'</option>';
					}
			  echo "</select>";
			  ?>
			  <input type="submit" name="submit">
			  </form>

 

RESULTS PAGE CODE:

<?php
require('get_connected.php');

$title = $_POST['title'];
$sql = mysql_query("SELECT title, issue_number, cover_date, comic_id FROM comics WHERE title ='" . $title . "'") or die(mysql_error());
if ($result = mysql_query($sql)) {
      if (mysql_num_rows($result)) {
        while ($row = mysql_fetch_assoc($result)) {
	echo $row['title'];
	}
      }
    }

echo $row['title'] . "<br>";
?>

 

I know this is a common thing people do with PHP but I could not find any info on the web. Can any one suggest a possible solution?

 

Thank you.

Link to comment
Share on other sites

try

<?php
require('get_connected.php');

$title = $_POST['title'];
$sql = "SELECT title, issue_number, cover_date, comic_id FROM comics WHERE title ='" . $title . "'" 
$result = mysql_query($sql)) or die(mysql_error());
      if (mysql_num_rows($result)) {
        while ($row = mysql_fetch_assoc($result)) {
	echo $row['title'];
	}
      }
echo $row['title'] . "<br>";
?>

Link to comment
Share on other sites

if ($result = [b]mysql_query[/b]($sql)) // You can not execute a query result as a query

 

try this edited version of sasa's code:

<?php

<?php
require('get_connected.php');

$title = $_POST['comics'];
$sql = sprintf("SELECT title, issue_number, cover_date, comic_id FROM comics WHERE title ='%s'", $title); 
$result = mysql_query($sql) or die(mysql_error());

if (mysql_num_rows($result)) {
  echo "<select name='comics'>";
  while ($row = mysql_fetch_assoc($result)) {
     vprintf("<option value='%s'>%s</option>", array_fill(0, 2, $row['title']));
  }
  echo "</select>";
}

echo $title;

?>

?>

Link to comment
Share on other sites

Hi Wafflestomper,

 

I think you're right. I have now added the name to the select option.

 

Here is my form code now:

<form action="comic_index_view.php" method="post">
			  <?php 
			  $result = mysql_query("SELECT title FROM comics GROUP BY title") or die(mysql_error());
				  $options="";
			  $title=$row["title"];
			  echo "<select name='title'>";
				  while($row = mysql_fetch_array($result)){
				  echo "<option value'" . $title . "'>".$title.'</option>';
					}
			  echo "</select>";
			  ?>
			  <input type="submit" name="submit">
			  </form>

 

And here is my results page now:

<?php
require('get_connected.php');


$title = $_POST['title'];
$sql = sprintf("SELECT title, issue_number, cover_date, comic_id FROM comics WHERE title ='%s'", $title); 
$result = mysql_query($sql) or die(mysql_error());

if (mysql_num_rows($result)) {
echo "<select name='title'>";
while ($row = mysql_fetch_assoc($result)) {
vprintf("<option value='%s'>%s</option>", array_fill(0, 2, $row['title']));
}
echo "</select>";
}

echo $title;
?>

 

I still can't get it to work though...

Link to comment
Share on other sites

Wow, that worked... but not how I wanted it. So I selected a comic title and hit submit. It took me to the next page and echoed another drop down menu with all the issues in that drop down. I just want it to echo the titles but not in another drop down.

 

I'll mess with the code, but any other suggestions? Thank you for your time.

Link to comment
Share on other sites

Got it:

 

This is the code:

<?php
require('get_connected.php');

$title = $_POST['title'];
$sql = sprintf("SELECT title, issue_number, cover_date, comic_id FROM comics WHERE title ='%s'", $title); 
$result = mysql_query($sql) or die(mysql_error());

if (mysql_num_rows($result)) {
while ($row = mysql_fetch_assoc($result)) {
echo $row['title'];
}

}

echo $title;
?>

 

Thanks guys for your help!

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.