form edits from retrieved data.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

form edits from retrieved data.

Post by facets »

Hi, I have a page which creates a new summary(summaryId) from a series of inputs.
Part of these inputs are drop downs using the following code

Code: Select all

function listCat() {
    $sql_query = mysql_query("SELECT paperCategory, paperCategoryId FROM aupapercategory");
    $output = "<select name=\"paperCategoryId\">\n";
    $output .= "<option value=\"\">-- Select Option --</option>\n"; 
        while(list($catname, $paperCategoryId)=mysql_fetch_array($sql_query)) {
        $catname = stripslashes($catname);
        $output .= "<option value=\"$paperCategoryId\">$catname</option>\n";
    }
    $output .= "</select>";
    mysql_free_result($sql_query);
return $output;
}
Now i'd like to create an edit page. How would I set the value of paperCatergoryId when retieving summaryId=2 (for example)
ie, Line 2 in the table has summaryId=2, paperCatergoryId=5.
I'm guessing it may have something to do with this line..

Code: Select all

$output .= "<option value=\"\">-- Select Option --</option>\n";
but i'm unsure.
any pointers would be great.. tia, will.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: form edits from retrieved data.

Post by timvw »

Depending on your form method being post or get the paperCategoryId should be in
$_POST['paperCategoryId'] or $_GET['paperCategoryId'];

But most people don't like the fact that your column names are exposed to the public. Another issue is that it's hard to handle tables with a primary key that exists out more than one column.

I usually end up with code like

Code: Select all

session_start();
$rows = array();
..

echo "<select name='someid'>";
$i = 0;
while ($row = mysql_fetch_assoc($rs))
{
  $rows[] = $row;
  echo "<option value='$i'>{$row['name']}</option>";
}
$_SESSION['rows'] = $rows;
Now in edit.php

Code: Select all

session_start();
$row = $_SESSION['rows'][$_POST['someid']];

...
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

thanks for the response tim.
one question though. why does example First return outpu to the page but Second does not?
any pointers?

Code: Select all

// First
while ($row = mysql_fetch_assoc($result)) {
   echo $row["summaryId"];
   echo $row["paperCategoryId"];
}

// Second
echo "<select name='summaryId'>";
$i = 0;
while ($row = mysql_fetch_assoc($result)) {
  echo "<option value='$i'>{$row['summaryId']}</option>";
}
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I notice i forgot to ++$i in the loop...


My guess is that after the first loop you need to reset the row pointer of the resultset. http://www.php.net/mysql_data_seek (Return to row 0)
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

Very close here. All I get in the main option is the word "Array" with other drop down contents below.
Any ideas on what I've got wrong?

Code: Select all

function adhesiveCategory2($paperCategoryId) {
	
	if ($paperCategoryId &lt; 0) {
			
    $sql_query = mysql_query("SELECT paperCategory, paperCategoryId FROM aupapercategory");
    echo "<select name=\"paperCategoryId\">";
    echo "<option value=\"\">-- Select Option --</option>\n"; 
        while(list($paperCategory, $paperCategoryId)=mysql_fetch_array($sql_query)) {
        $paperCategory = stripslashes($paperCategory);
        echo "<option value=\"$paperCategoryId\">$paperCategory</option>\n";
    }
    echo "</select>";
    mysql_free_result($sql_query);
	}
	else {
			$sql = mysql_query("SELECT paperCategory, paperCategoryId FROM aupapercategory WHERE paperCategoryId = $paperCategoryId");
			$sql_query = mysql_query("SELECT paperCategory, paperCategoryId FROM aupapercategory");
    		echo "<select name=\"paperCategoryId\">";
    		echo "<option value=\"\">" .list($paperCategory, $paperCategoryId)=mysql_fetch_array($sql). "</option>\n"; 
        	while(list($paperCategory, $paperCategoryId)=mysql_fetch_array($sql_query)) {
        	$paperCategory = stripslashes($paperCategory);
        	echo "<option value=\"$paperCategoryId\">$paperCategory</option>\n";
    }
    echo "</select>";
    mysql_free_result($sql_query);
	}
}
PS, Thank you for your advice on "column exposed to the public" I will certainly have to look into that. Although this is only an internal app. i'm sure that security will eventually become a concern. tia, will.
if ($paperCategoryId < 0) {

$sql_query = mysql_query("SELECT paperCategory, paperCategoryId FROM aupapercategory&quote;);
echo &quote;&lt;select name=\&quote;paperCategoryId\&quote;&gt;&quote;;
echo &quote;&lt;option value=\&quote;\&quote;&gt;-- Select Option --</option>\n";
while(list($paperCategory, $paperCategoryId)=mysql_fetch_array($sql_query)) {
$paperCategory = stripslashes($paperCategory);
echo "<option value=\"$paperCategoryId\">$paperCategory</option>\n";
}
echo "</select>";
mysql_free_result($sql_query);
}
else {
$sql = mysql_query("SELECT paperCategory, paperCategoryId FROM aupapercategory WHERE paperCategoryId = $paperCategoryId");
$sql_query = mysql_query("SELECT paperCategory, paperCategoryId FROM aupapercategory");
echo "<select name=\"paperCategoryId\">";
echo "<option value=\"\">" .list($paperCategory, $paperCategoryId)=mysql_fetch_array($sql). "</option>\n";
while(list($paperCategory, $paperCa {
$paperCategory = stripslashes($paperCategory);
echo "<option value=\"$paperCategoryId\">$paperCategory</option>\n";
}
echo "</select>";
mysql_free_result($sql_query);
}
else {
$sql = mysql_query("SELECT paperCategory, paperCategoryId FROM aupapercategory WHERE paperCategoryId = $paperCategoryId");
$sql_query = mysql_query("SELECT paperCategory, paperCategoryId FROM aupapercategory");
echo "<select name=\"paperCategoryId\">";
echo "<option value=\"\">" .list($paperCategory, $paperCategoryId)=mysql_fetch_array($sql). "</option>\n";
while(list($paperCategory, $paperCategoryId)=mysql_fetch_array($sql_query)) {
$paperCategory = stripslashes($paperCategory);
echo "<option value=\"$paperCategoryId\">$paperCategory</option>\n";
}
echo "</select>";
mysql_free_result($sql_query);
}
}

PS, Thank you for your advice on "column exposed to the public" I will certainly have to look into that. Although this is only an internal app. i'm sure that security will eventually become a concern. tia, will.ther drop down contents below.
Any ideas on what I've got wrong?

Code: Select all

function adhesiveCategory2($paperCategoryId) {
	
	if ($paperCategoryId < 0) {
			
    $sql_query = mysql_query("SELECT paperCategory, paperCategoryId FROM aupapercategory");
    echo "<select name=\"paperCategoryId\">";
    echo "<option value=\"\">-- Select Option --</option>\n"; 
        while(list($paperCategory, $paperCategoryId)=mysql_fetch_array($sql_query)) {
        $paperCategory = stripslashes($paperCategory);
        echo "<option value=\"$paperCategoryId\">$paperCategory</option>\n";
    }
    echo "</select>";
    mysql_free_result($sql_query);
	}
	else {
			$sql = mysql_query("SELECT paperCategory, paperCategoryId FROM aupapercategory WHERE paperCategoryId = $paperCategoryId");
			$sql_query = mysql_query("SELECT paperCategory, paperCategoryId FROM aupapercategory");
    		echo "<select name=\"paperCategoryId\">";
    		echo "<option value=\"\">" .list($paperCategory, $paperCategoryId)=mysql_fetch_array($sql). "</option>\n"; 
        	while(list($paperCategory, $paperCategoryId)=mysql_fetch_array($sql_query)) {
        	$paperCategory = stripslashes($paperCategory);
        	echo "<option value=\"$paperCategoryId\">$paperCategory</option>\n";
    }
    echo "</select>";
    mysql_free_result($sql_query);
	}
}
Last edited by facets on Sun Jan 14, 2007 1:10 am, edited 1 time in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Your snippet, line 19

Code: Select all

echo &quote;&lt;option value=\&quote;\&quote;&gt;&quote; .list($paperCategory, $paperCategoryId)=mysql_fetch_array($sql). &quote;&lt;/option&gt;\n&quote;;
Probably want something like

Code: Select all

while ($row = mysql_fetch_assoc($sql))
{
  $row['paperCategory'] = stripslashes($row['paperCategory']);
  echo "<option value='{$row['paperCategoryId']}'>{$row['paperCategory']}</option>";
}
</option>\n";

Probably want something like

Code: Select all

while ($row = mysql_fetch_assoc($sql))
{
  $row['paperCategory'] = stripslashes($row['paperCategory']);
  echo "<option value='{$row['paperCategoryId']}'>{$row['paperCategory']}</option>";
}
;option value=\"\">" .list($paperCategory, $paperCategoryId)=mysql_fetch_array($sql). "</option>\n";

Probably want something like

Code: Select all

while ($row = mysql_fetch_assoc($sql))
{
  $row['paperCategory'] = stripslashes($row['paperCategory']);
  echo "<option value='{$row['paperCategoryId']}'>{$row['paperCategory']}</option>";
}
$paperCategory, $paperCategoryId)=mysql_fetch_array($sql). "</option&gt;\n&quote;;

Probably want something like

Code: Select all

while ($row = mysql_fetch_assoc($sql))
{
  $row['paperCategory'] = stripslashes($row['paperCategory']);
  echo "<option value=\"\">" .list($paperCategory, $paperCategoryId)=mysql_fetch_array($sql). "</option>\n";
Probably want something like

Code: Select all

while ($row = mysql_fetch_assoc($sql))
{
  $row['paperCategory'] = stripslashes($row['paperCategory']);
  echo "<option value='{$row['paperCategoryId 19

Code: Select all

echo "<option value=\"\">" .list($paperCategory, $paperCategoryId)=mysql_fetch_array($sql). "</option>\n";
Probably want something like

Code: Select all

while ($row = mysql_fetch_assoc($sql))
{
  $row['paperCategory'] = stripslashes($row['paperCategory']);
  echo "<option value='{$row['paperCategoryId']}'>{$row['paperCategory']}</option>";
}
;lt;option value=\"\"&gt;&quote; .list($paperCategory, $paperCategoryId)=mysql_fetch_array($sql). &quote;&lt;/option>\n";

Probably want something like

Code: Select all

while ($row = mysql_fetch_assoc($sql))
{
  $row['paperCategory'] = strip9d]
echo "<option value=\"\">" .list($paperCategory, $paperCategoryId)=mysql_fetch_array($sql). "</option>\n";
Probably want something like

Code: Select all

while ($row = mysql_fetch_assoc($sql))
{
  $row['paperCategory'] = stripslashes($row['paperCategory']);
  echo "<option value='{$row['paperCategoryId']}'>{$row['paperCategory']}</option>";
}
19

Code: Select all

echo "<option value=\"\">" .list($paperCategory, $paperCategoryId)=mysql_fetch_array($sql). "</option>\n";
Probably want something like

Code: Select all

while ($row = mysql_fetch_assoc($sql))
{
  $row['paperCategory'] = stripslashes($row['paperCategory']);
  echo "<option value='{$row['paperCategoryId']}'>{$row['paperCategory']}</option>";
}
Probably want something like

Code: Select all

while ($row = mysql_fetch_assoc($sql))
{
  $row['paperCategory'] = stripslashes($row['paperCategory']);
  echo "<
echo "<option value=\"\">" .list($paperCategory, $paperCategoryId)=mysql_fetch_array($sql). "</option>\n";
Probably want something like

Code: Select all

while ($row = mysql_fetch_assoc($sql))
{
  $row['paperCategory'] = stripslashes($row['paperCategory']);
  echo "<option value='{$row['paperCategoryId']}'>{$row['paperCategory']}</option>";
}
Post Reply