Forum Moderators: coopster

Message Too Old, No Replies

Having problems with page

where I select a record, but the first record is displayed every time

         

BadGoat

4:17 pm on Apr 21, 2005 (gmt 0)

10+ Year Member



Hi!

So, I have one page where I select a record which I want to edit... the records are in a drop down menu which are sorted by name. When I select a record on the list to edit and click Submit, I wanted it to go to the next page and display the record I selected.. But with my code, it always displays the first record, and I am not sure why. Does anyone spot what I've done wrong?

Here is the code for where I select the record:


$query = "SELECT * FROM acctmgr";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

echo '<form action="edit_acctmgr.php?acctmgr_name='.$row['acctmgr_name'].'" method="post">
<table align=left border="0" width="80%" cellpadding="0" cellspacing="0" summary="MAIN TABLE">

<TR>
<td ROWSPAN=88888 bgcolor="#ccccff" valign="top" height="80" width="100"><CENTER><img alt="logo_sm (1K)" src="logo_sm.gif" height="80" width="67"><BR><font face="Tahoma" size=2 color="black"><B>I-DEP DIW Management Tool<BR>-----<BR><a href=damndiw1.php>Create new DIW</A><BR>-----<BR><a href=damndiw3.php>View DIW List</A><BR>-----<BR><a href=damndiw6.php>Calendar</A><BR>-----<BR><a href=diw_admin.html>DIW Admin</A></B></FONT></CENTER></TD>
<TD valign="top">

<TABLE>
<tr>
<td width=600 colspan=2 bgcolor="#000080"><font face="Tahoma" size="5" color="white"> I-DEP - DIW Admin </font></td>
</tr>
<TR>
<td colspan=2 bgcolor="#ccccff" width=600><font face="Tahoma" size=2 color="black"><b>Select Account Manager </b></font></td>
</TR>
<TR>
<td width="20%"><b><font face="Tahoma" size=2>Account Manager</font></b></small></td>
<td><select name="dropdownacctmgr" tabindex="4"><option value="notset">- Edit Account Manager -</option>';

//--- CREATE Account Manager SELECT ---
$sql = "SELECT DISTINCT acctmgr_name FROM acctmgr ORDER BY acctmgr_name";
$acctmgr_name = mysql_query($sql) or die($sql . '<br />' . mysql_error());
while ($row = mysql_fetch_array($acctmgr_name)) {
echo '<option value="' . $row['acctmgr_name'] . '">' . $row['acctmgr_name'] . '</option>';
}


And here is the code where I try to display the data for editing:

if(!empty($_REQUEST['action']) && $_REQUEST['action'] == 'update')
{
$acctmgr_name = $_REQUEST['acctmgr_name'];
$_GET['acctmgr_name'] = $acctmgr_name;
$query = "UPDATE `acctmgr` SET acctmgr_name = '".$_REQUEST['newacctmgr_name']."',acctmgr_phone = '".$_REQUEST['newacctmgr_phone']."',acctmgr_cellphone = '".$_REQUEST['newacctmgr_cellphone']."',acctmgr_email = '".$_REQUEST['newacctmgr_email']."' WHERE id='$id'";
$result = mysql_query($query) or die("<b>mySQL Error:</b>".mysql_errono()."<br>".mysql_error());
if(!$result)
{
echo 'Error processing request.';
}
else
{
echo 'Request processed successfully.';
}
}

$acctmgr_name = $_GET['acctmgr_name'];
// The ID is passed through the URL to specify the row,
// Or it is set in the previous script.

$query = "SELECT * FROM acctmgr WHERE acctmgr_name = '$acctmgr_name'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

echo '<form name="update" method="post">
<input type="hidden" value="update" name="action">
<input type="hidden" name="id" value="'.$id.'">

<table align=left border="0" width="80%" cellpadding="0" cellspacing="0" summary="MAIN TABLE">

<TR>
<td ROWSPAN=88888 bgcolor="#ccccff" valign="top" height="80" width="100"><CENTER><img alt="logo_sm (1K)" src="logo_sm.gif" height="80" width="67"><BR><font face="Tahoma" size=2 color="black"><B>I-DEP DIW Management Tool<BR>-----<BR><a href=damndiw1.php>Create new DIW</A><BR>-----<BR><a href=damndiw3.php>View DIW List</A><BR>-----<BR><a href=damndiw6.php>Calendar</A><BR>-----<BR><a href=diw_admin.html>DIW Admin</A></B></FONT></CENTER></TD>
<TD valign="top">

<TABLE>
<tr>
<td width=600 colspan=2 bgcolor="#000080"><font face="Tahoma" size="5" color="white"> I-DEP - DIW Admin </font></td>
</tr>
<TR>
<td colspan=2 bgcolor="#ccccff" width=600><font face="Tahoma" size=2 color="black"><b>Edit Account Manager </b></font></td>
</TR>
<TR>
<TD><B>Account Manager Name</B></TD>
<TD><input type="text" name="newacctmgr_name" size="40" value="'.$row['acctmgr_name'].$resstring .'"></TD>
</TR>
<TR>
<TD><B>Phone Number</B></TD>
<TD><input type="text" name="newacctmgr_phone" size="12" value="'.$row['acctmgr_phone'].'"></TD>
</TR>
<TR>
<TD><B>Cell</B></TD>
<TD><input type="text" name="newacctmgr_cellphone" size="12" value="'.$row['acctmgr_cellphone'].'"></TD>
</TR>
<TR>
<TD><B>Email Address</B></TD>
<TD><input type="text" name="newacctmgr_email" size="32" value="'.$row['acctmgr_email'].'"></TD>
</TR>
<TR>
<TD COLSPAN=4><input type="submit" value="Update"> </form></TD>
</TR>

</TABLE>';
?>


Help!

StupidScript

6:49 pm on Apr 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Howdy!

It looks like you are primarily keying off the

acctmgr_name
, which is being tossed around. You should try using the 'id' field as your key, instead.

To streamline things a bit, I'd clean the FORM element on your first page:

<form action="edit_acctmgr.php?acctmgr_name='.$row['acctmgr_name'].'" method="post">

changes to

<form action="edit_acctmgr.php" method="post">
<input type="hidden" name="acctmgr_name" value="'.$row['acctmgr_name'].'">

On the next page, you have:

if(!empty($_REQUEST['action']) && $_REQUEST['action'] == 'update')

{

$acctmgr_name = $_REQUEST['acctmgr_name'];

$_GET['acctmgr_name'] = $acctmgr_name;

(That's the tossing around part.) The environmental

$_GET[]
array is not something you set, it's a result. Later in the same script, you assert:

$acctmgr_name = $_GET['acctmgr_name'];

Remove both

$_GET['acctmgr_name'] = $acctmgr_name;
and
$acctmgr_name = $_GET['acctmgr_name'];

One main issue is that your previous FORM used the "post" method ... so you should probably be reading the

$_POST['acctmgr_name']
values, instead of
$_GET[]
.

Lastly, I don't see where the 'id' value is being collected. The closest I can find is:

echo '<option value="' . $row['acctmgr_name'] . '">' . $row['acctmgr_name'] . '</option>';

which should probably be changed to:

echo '<option value="' . $row['id'] . '">' . $row['acctmgr_name'] . '</option>';

and likewise, your next queries should key off that data. Note that the 'id' value will be stored in

$_POST['acctmgr_name']
unless you change the name of the drop-down menu to 'id' or something.

This all assumes that there is one row for each account manager and each row has a unique 'id' value.

Hope that's helpful ...

BadGoat

7:28 pm on Apr 21, 2005 (gmt 0)

10+ Year Member



Heya StupidScript!

I have been working on trying to implement your suggestions for a bit now, and I am running into the same brick wall. I tried all the suggestions first, and it didn't work, so I reverted back to the original scripts and tried to just edit the first page first, since changing that should allow the record I selected in the dropdown menu to be displayed on the next page, but when I changed it, it still defaults to the first record in the table.

I did also try to use just the id field, rather than the acctmgr_name field, with the same result

The code, as changed, is:


echo ' <form action="edit_acctmgr.php?id='.$row['id'].'" method="post">
<table align=left border="0" width="80%" cellpadding="0" cellspacing="0" summary="MAIN TABLE">

<TR>
<td ROWSPAN=88888 bgcolor="#ccccff" valign="top" height="80" width="100"><CENTER><img alt="logo_sm (1K)" src="logo_sm.gif" height="80" width="67"><BR><font face="Tahoma" size=2 color="black"><B>I-DEP DIW Management Tool<BR>-----<BR><a href=damndiw1.php>Create new DIW</A><BR>-----<BR><a href=damndiw3.php>View DIW List</A><BR>-----<BR><a href=damndiw6.php>Calendar</A><BR>-----<BR><a href=diw_admin.html>DIW Admin</A></B></FONT></CENTER></TD>
<TD valign="top">

<TABLE>
<tr>
<td width=600 colspan=2 bgcolor="#000080"><font face="Tahoma" size="5" color="white"> I-DEP - DIW Admin </font></td>
</tr>
<TR>
<td colspan=2 bgcolor="#ccccff" width=600><font face="Tahoma" size=2 color="black"><b>Select Account Manager </b></font></td>
</TR>
<TR>
<td width="20%"><b><font face="Tahoma" size=2>Account Manager</font></b></small></td>
<td><select name="dropdownacctmgr" tabindex="4"><option value="notset">- Edit Account Manager -</option>';

//--- CREATE Account Manager SELECT ---
$sql = "SELECT DISTINCT acctmgr_name FROM acctmgr ORDER BY acctmgr_name";
$acctmgr_name = mysql_query($sql) or die($sql . '<br />' . mysql_error());
while ($row = mysql_fetch_array($acctmgr_name)) {
echo '<option value="' . $row['id'] . '">' . $row['acctmgr_name'] . '</option>';
}

echo '</td>
</tr>
<TR>
<TD COLSPAN=2><CENTER><BR><input type="submit" name="Submit" value="Submit" action="required"></CENTER></TD></FORM>
</TR>

StupidScript

7:52 pm on Apr 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi BadGoat ...

//--- CREATE Account Manager SELECT ---

$sql = "SELECT DISTINCT acctmgr_name FROM acctmgr ORDER BY acctmgr_name";

SELECT DISTINCT
will only select the one column of data and removes duplicates from the result set. In this case, all you are getting is
acctmgr_name
... not 'id'. In addition, if there are any duplicate acctmgr_names in the db, performing selects and updates based on that will be confusing, to say the least. Stick to verifiably unique keys, like id.

//--- CREATE Account Manager SELECT ---

$sql = "SELECT id,acctmgr_name FROM acctmgr ORDER BY acctmgr_name";

$acctmgr_name = mysql_query($sql) or die($sql . '<br />' . mysql_error());

while ($row = mysql_fetch_array($acctmgr_name)) {

echo '<option value="' . $row['id'] . '">' . $row['acctmgr_name'] . '</option>';

}

will return both the 'id' and 'acctmgr_name' fields.

Here's a bit of "concept code" (in shorthand ... this won't work if you copy-and-paste ... :) ):

1: For page 1, collect all mgrs and their ids from db

select id,mgr from mgrList order by mgr

2: Use those values in the page 1 form

<form action="page2.php" method="post">

<select name="mgrId">

while $mgr ...

echo "<option value=".$id." />".$mgr."\n";

end while

</select>

</form>

3: Form submits, sending WHICH id to display for editing

4: On page 2, grab the right row and show data in edit form

$mgrId=$_POST['mgrId'];

select * from mgrList where id='$mgrId'

<form action="page3.php" method="post">

<input type="hidden" name="id" value='.$mgrId.'>

Edit Name: <input name="newName" value=".$row["mgrName"].">

Edit Email: <input name="newEmail" value=".$row["mgrEmail"].">

etc..

</form>

5: On page 3, update the db with the data from page 2

$thisId=$_POST['id'];

$thisName=$_POST['newName']; etc..

update mgrList set mgrName='$thisName' etc.. where id='$thisId'

So you grab the ids and names, using the ids for the real action and the names for human-readable uses, and use the ids to key and control the display and updating of the data.

mcibor

8:39 pm on Apr 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think that the main problem is that you don't read what you selected. To read that you need just one line of code:

if(isset($_POST['dropdownacctmgr']) ¦¦!empty($_POST['dropdownacctmgr'])) $acctmgr_name = $_POST['dropdownacctmgr'];
else $acctmgr_name = "something you wish to be if it is not set";

BTW shorten the code to only relevant data (style is not relevant, font and b either), because it is harder to read the code and spot the mistake.

Hope this helps a bit.
Best wishes
Michal Cibor