Page 1 of 1

Passing variables from a Function into a DB

Posted: Wed Jun 01, 2005 8:07 pm
by facets
Hi All,

The following code populates a Dropdown menu from the DB and now I wish to post these setting into another table. I'm guessing I need to somehow transform the Select statement into an input statement with a name. But i'm unsure on how to do this. Does anyone have any pointers?

TAI, Will.

Code: Select all

<?

include "../includes/functions.inc";
include "../includes/common_db.inc";

$link_id = db_connect($db_materials);

if(isset($_GET['commented']))
{
        echo('Your comment has been posted.');

$link_id = db_connect($db_materials);
	
	$category = $_GET['category'];
	$colloPaperName = $_GET['colloPaperName'];

$sql_add = "INSERT INTO summary values('','$category','$paperName')";

mysql_query($sql_add) or die(mysql_error());

}
else {
	
?>

<form method='get' action='<? echo $_SERVER['PHP_SELF']; ?>'>
<input type='hidden' name='commented' value='set'>

<?

echo "<table class=\"materials\"><tr>";

$addSummary=array('Category','Paper Name');

$sumTitles = array(listCat1(),'paperName');
	
for($x = 0; $x<count($addSummary); $x++) {
	echo "<tr><td width=\"200px\" colspan=\"2\" valign=\"top\">".$addSummary[$x]."</td>\n";
	echo "<td width=\"200px\" colspan=\"2\">$sumTitles[$x]</td></tr>\n";
	}
	
?>
<tr><td width=\"200px\" colspan=\"2\" valign=\"top\"><input type='reset' class='btn' value='Reset'></td>
<td width=\"200px\" colspan=\"2\"><input type='submit' class='btn' value='Add'></td></tr>
</form></table>

<?
}
?>

Posted: Wed Jun 01, 2005 8:58 pm
by Grim...
Okay, maybe I'm blind, but I can't see a SELECT in the code you posted...

Posted: Wed Jun 01, 2005 9:00 pm
by Grim...
Or a dropdown menu, for that matter - are you sure you have posted the correct code?

Posted: Wed Jun 01, 2005 9:37 pm
by facets
Sorry.. Here's the function that create the Select etc

Code: Select all

function listCat() {
	$sql_query = mysql_query("SELECT category, categoryId FROM aupapercategory");
	echo "<select name=\"categoryId\">";
    echo "<option value=\"\">-- Select Option --</option>"; 
    	while(list($catname, $categoryId)=mysql_fetch_array($sql_query)) {
		$catname = stripslashes($catname);
		echo "<option value=\"$categoryId\">$catname</option>";
	}
	echo "</select>";
	mysql_free_result($sql_query);
}

Posted: Wed Jun 01, 2005 11:09 pm
by infolock
well, here is an example of something you could do with your function..

Code: Select all

<?php
function listCat(){
       Global $catname, $categoryId;
	$sql_query = mysql_query("SELECT category, categoryId FROM aupapercategory");
	echo "<select name=\"categoryId\">";
    echo "<option value=\"\">-- Select Option --</option>"; 
    	while($row=mysql_fetch_array($sql_query)) {
		$catname = stripslashes($row['category']);
              $categoryId = $row['categoryId'];
		echo "<option value=\"$categoryId\">$catname</option>";
	}
	echo "</select>";
	mysql_free_result($sql_query);
}
$sql = mysql_query("insert into mytable (myfield1, myfield2) VALUES ('".$catname."','".$categoryId."'")") or die(MySQL_Error());