Jump to content

[SOLVED] mysql printf to a list.


Mr P!nk

Recommended Posts

im having a problem printing results from my database to a drop down list.

 

this is the insert into database witch works fine.

<?php
include 'db.php';	
$sql = mysql_query("SELECT * FROM list WHERE list_name='$list_name'"); 	
$list_check = mysql_num_rows($sql);
if(($list_check > 0)){
	echo "The name $list_name is already being used.";
		unset($list_name,$list_content);
	 	 	}
		else{			
	 $sql = "insert into * (list_name,list_content) values ('$list_name', '$list_content')";
$result = mysql_query($sql);
echo "$list_name has been uploaded.";
}
?>

 

but here i want it to show the results in a list so the user can select one and the contents will be placed into a text area

 
<form id="form1" action"edit.php?action=listname" name"form1" method="post">
        <label>List</label><br />
         <?php	   
   $sql = "select list_name,list_content from list order by id DESC";
   $query = mysql_query($sql);
   $result = mysql_fetch_array($query);
   do{
			printf("<select name=\"list\" size=\"5\" multiple=\"multiple\" id=\"list\">
        <option>Please Select a database</option>
       <option>%s</option>
</select>",$result['list_name'], $result['id'], $result['list_content']);
		} while($result = mysql_fetch_array($query));

   ?>
		<input type="submit" name="Submit" value="Submit" />
    </form>
	<br />
	<br />
Mailing List<br />
        <label>
        <textarea name="list_content" cols="50" rows="3" class="formData" id="list_content">
	<?php if ( $action == 'listname') {printf("email_list"); } ?>
	</textarea>

 

so far it prints a list for every value in the database, i need them all to just go into one, and when the user presses submit the contents of their selected list goes into the text area.

 

any ideas of how i would accomplish this task ?

 

Many thanks

 

P!nk

Link to comment
https://forums.phpfreaks.com/topic/76248-solved-mysql-printf-to-a-list/
Share on other sites

try this for your select

 

 

echo "<select name=\"list\" size=\"5\" multiple=\"multiple\" id=\"list\">";
echo "<option>Please Select a database</option>";
while($result = mysql_fetch_array($query))
{
echo "<option value='{$result['id']}'>{$result['list_name']}</option>";
}
echo "</select>";

Thanks for your help rajivgonsalves

 

but using that i get thrown this error : Parse error: parse error, unexpected $ in /public_html/mercury/edit.php on line 461

 

ive checked the code for any missing {}();" ' but all looks in place ;/

 

any ideas?

 

Thanks again

 

 

 

 

sure

<?php	   
   $sql = "select list_name,list_content from list order by id DESC";
   $query = mysql_query($sql);
   $result = mysql_fetch_array($query);
   do{
  	   echo "<select name=\"list\" size=\"5\" multiple=\"multiple\" id=\"list\">";
echo "(<option>Please Select a database</option>)";
while($result = mysql_fetch_array($query))
{
echo "<option value='{$result['id']}'>{$result['list_name']}</option>";
}
echo "</select>";

 

when i close the do{

 

i get errors Parse error: parse error, unexpected ';', expecting T_WHILE in /home/acmeart/public_html/mercury/tabloid.php on line 143

 

Thanks

well you do not require the do try this code (modified)

 

<?php	   
   $sql = "select list_name,list_content from list order by id DESC";
   $query = mysql_query($sql);
   $result = mysql_fetch_array($query);
	   echo "<select name=\"list\" size=\"5\" multiple=\"multiple\" id=\"list\">";
echo "(<option>Please Select a database</option>)";
while($result = mysql_fetch_array($query))
{
	echo "<option value='{$result['list_name']}'>{$result['list_name']}</option>";
}
echo "</select>";

Archived

This topic is now archived and is closed to further replies.

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