Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating Database from drop down menu's using two tables..

Status
Not open for further replies.

WizyWyg

Technical User
Jan 31, 2001
854
JP
Sounds confusing...^_^

Anyway, running into a problem:

A user will select from a list to modify a record in a database and then taken to a page that allows them to edit it.

The page to allow the editing has this coding:

Code:
<?
if (!$accessId) {
	header(&quot;Location: [URL unfurl="true"]http://opushi/test2/modacc.php&quot;);[/URL]
	exit;
} else {
	session_start();
}

if ($valid != &quot;yes&quot;) {
	header(&quot;Location: [URL unfurl="true"]http://opushi/test2/admin.php&quot;);[/URL]
	exit;
} else { 
	session_register('valid');
}

$db_name = &quot;acess&quot;;
$table_name = &quot;accessory&quot;;
$connection = @mysql_connect(&quot;localhost&quot;, &quot;root&quot;) or die(&quot;Couldn't connect.&quot;.mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(&quot;Couldn't select database.&quot;.mysql_error());
$sql = &quot;SELECT item, snumber
	FROM $table_name
	WHERE accessId = \&quot;$accessId\&quot;
	&quot;;
$result = @mysql_query($sql,$connection) or die(&quot;Couldn't execute query.&quot;.mysql_error());
while ($row = mysql_fetch_array($result)) {
	$item = $row['item'];
	$snumber = $row['snumber'];
	$userId = $row['userId'];
}
?>

 
<HTML>
<HEAD>
<TITLE>Modify Accessory</TITLE>
</HEAD>
<BODY>

<h2><font face=&quot;Arial, Helvetica, sans-serif&quot;>Modify Accessory</font></h2>
<FORM METHOD=&quot;post&quot; ACTION=&quot;mod_accessdone.php&quot;>
<INPUT TYPE=&quot;hidden&quot; name=&quot;id&quot; value=&quot;<? echo &quot;$id&quot;; ?>&quot;>

<table width=50% align=center>
	<tr>
		<td><font face=arial size=2>Item:</font></td>
		<td><input type=&quot;text&quot; name=&quot;item&quot; value=&quot;<? echo &quot;$item&quot;; ?>&quot; size=50 maxlength=75></td>
	</tr>
	<tr>
		<td><font face=arial size=2>Serial Number:</font></td>
		<td><input type=&quot;text&quot; name=&quot;snumber&quot; value=&quot;<? echo &quot;$snumber&quot;; ?>&quot; size=50 maxlength=75></td>
	</tr>
	<tr>
		<td><font face=&quot;arial&quot; size=2>On Loan to:</font></td>
		<td><select name=&quot;userId&quot;>
  <?php
	
  $q=&quot;select userId, username from user
  order by userId asc&quot;;
  $r=mysql_query($q) or die(mysql_error().$q);

  while($row=mysql_fetch_array($r)) {
    echo &quot;<option value=\&quot;&quot;.$row[&quot;userId&quot;].&quot;\&quot;>&quot;.$row[&quot;username&quot;].&quot;</option>&quot;;
  }
  ?>
</select>	</td>
	</tr>
</table>
<center><INPUT TYPE=&quot;SUBMIT&quot; NAME=&quot;submit&quot; VALUE=&quot;Update Accessory&quot;></center>
</FORM>


</BODY>
</HTML>

And when the fields have been edited, it takes you to this page:

Code:
<?
if (!$item) {
	header( &quot;Location: [URL unfurl="true"]http://opushi/test2/modacc.php&quot;);[/URL]
	exit;
	} else {
	session_start();
	}
if ($valid != &quot;yes&quot;) {
	header ( &quot;Location: [URL unfurl="true"]http://opushi/test2/admin.php&quot;);[/URL]
	exit;
	} else {
		session_register('valid');
}
	
$db_name = &quot;acess&quot;;
$table_name = &quot;accessory&quot;;
$connection = @mysql_connect(&quot;localhost&quot;,&quot;root&quot;) or die (&quot;Couldn't connect.&quot;.mysql_error());
$db = @mysql_select_db($db_name, $connection) or die (&quot;Couldn't select database.&quot;.mysql_error());
$sql = &quot;UPDATE accessory
	SET
		item = \&quot;$item\&quot;,
		snumber = \&quot;$snumber\&quot;,
		userId = \&quot;$userId\&quot;
		WHERE accessId = \&quot;$accessId\&quot;
		&quot;;
$result = @mysql_query($sql,$connection) or die (&quot;Couldn't execut query.&quot;.mysql_error());
?>


<html>
<head>
<title> Modify Accessory Complete</title>
</head>
<body>
<div align=&quot;center&quot;><font face=&quot;Arial, Helvetica, sans-serif&quot;><b>Modified Accessory</b></font><font size=&quot;2&quot; face=&quot;Arial, Helvetica, sans-serif&quot;><b> 
  </b><br>
  The following accessory has been changed </font><br>
  <table width=50% align=center>
    <tr> 
      <td width=&quot;19%&quot;><font face=&quot;Arial, Helvetica, sans-serif&quot; size=&quot;2&quot;>Item:</font></td>
      <td width=&quot;81%&quot;><font face=&quot;Arial, Helvetica, sans-serif&quot; size=&quot;2&quot;> 
        <? echo &quot;$item&quot;; ?>
        </font></td>
    <tr> 
      <td width=&quot;19%&quot;><font face=&quot;Arial, Helvetica, sans-serif&quot; size=&quot;2&quot;>Serial 
        Number:</font></td>
      <td width=&quot;81%&quot;><font face=&quot;Arial, Helvetica, sans-serif&quot; size=&quot;2&quot;> 
        <? echo &quot;$snumber&quot;; ?>
        </font></td>
    </tr>
    <tr> 
      <td width=&quot;19%&quot;><font face=&quot;Arial, Helvetica, sans-serif&quot; size=&quot;2&quot;>On Loan 
        to:</font></td>
      <td width=&quot;81%&quot;> <font face=&quot;Arial, Helvetica, sans-serif&quot; size=&quot;2&quot;> 
        <? echo &quot;$userId&quot;; ?>
        </font></td>
    </tr>
  </table>
  <font face=&quot;arial&quot; size=2><a href=&quot;admin.php&quot;>Back to the Admin Menu</a></font> 
</div>
</body>
</html>


The problem is the table : accessory (which needs to be updated) within the database is NOT being updated with the new information (ie if you change the items' name, serial number or user)

Help and TIA
 
Hello, my friend.

Here are some things to look into:

1. Your SQL can be modified as such:
$sql = &quot;UPDATE accessory SET
item='$item',
snumber='$snumber',
userId='$userId'
WHERE accessId='$accessId'&quot;;

2. I don't see anything that would set $accessId in the script that updates the record. I do see that in the first script, you are telling PHP to see if $accessId is set, and if not, the user is directed to the modacc.php page. This makes me assume that you are setting this as a session variable.

Have you checked to see what the actual value of $accessId is in your tests? You might want to check this out because it might be possible that the value does not exist for accessId in any of your records.

Other than that, I don't really see anything else (technically) wrong that would make your update/scripts not functional.

Chad. ICQ: 54380631
online.dll
 
Actually I figured it out Chad, i had $id as a value in one of my lines when it was supposed to be $accessId.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top