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!

Php Newbie - Form problem 1

Status
Not open for further replies.

WizyWyg

Technical User
Jan 31, 2001
854
JP
Im trying to create an input form and Im getting stuck on a drop menu for a form. This is a very "generic" form so Im hoping someone can help me. Below is my coding on my current page:


<html>
<body>
<?php
if ($submit) {
// process form
$db = mysql_connect(&quot;localhost&quot;, &quot;root&quot;);
mysql_select_db(&quot;accessories&quot;,$db);
$sql = &quot;INSERT INTO accessory (item,snumber,userId) VALUES ('$item','$snumber','$userId')&quot;;
$result = mysql_query($sql);
echo &quot;Thank you! Information entered. Click <a href=index.php>here</a> to view list.\n&quot;;
} else{
// display form
?>
<form method=&quot;post&quot; action=&quot;<?php echo $PHP_SELF?>&quot;>
<table width=40%><tr><td>Item:</td><td><input type=&quot;Text&quot; name=&quot;item&quot; size=&quot;25&quot; maxlength=&quot;50&quot;></td></tr>
<tr><td>SN Number:</td><td><input type=&quot;Text&quot; name=&quot;snumber&quot; size=&quot;25&quot; maxlength=&quot;50&quot;></td></tr>
<tr><td>On Loan to:</td><td>
<select name=&quot;userId&quot;>
<?php
$connection=mysql_connect(&quot;localhost&quot;, &quot;root&quot;);
$query=&quot;select userId, name from user&quot;;
$result2=mysql_db_query($query, &quot;accessories&quot;, $connection);
while($row=mysql_fetch_array($result2))
{
$id=$row['userId'];
$name=$row['name'];
echo(&quot;<option value=\&quot;$id\&quot;>$name</option>&quot;);
}
?>
</select>
</</td></tr></table>

<input type=&quot;Submit&quot; name=&quot;submit&quot; value=&quot;Enter information&quot;>
</form>
<?php
} // end if
?>
</body>
</html>



Currently the Loan to Select fields (options) are showing nothing (its not accessing the users in the user database)


My user database has this in its table:
userId (incremental)
name (name of user)

The accessory database has this in its table:
accessId (incremental)
item (items name)
date (date purchased)
snumber (serial number of item)
userId (this will reference the number in the user database)


So im trying to get the drop down menu pull from the user database by User Id but display the NAME of the user instead.
So when its shown on the index page, it will display the items' information along with the person its on loan to.

for the index page, I currently have:

<html>
<body>
<?php
$db = mysql_connect(&quot;localhost&quot;, &quot;root&quot;);
mysql_select_db(&quot;accessories&quot;,$db);
$result = mysql_query(&quot;SELECT * FROM accessory&quot;,$db);
if ($myrow = mysql_fetch_array($result)) {
echo &quot;<table border=1 width=100%>\n&quot;;
echo &quot;<tr><td>Item</td><td>Date Purchased</td><td>SN Number</td><td>On Loan to</td></tr>\n&quot;;
do {
printf(&quot;<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n&quot;, $myrow[&quot;item&quot;], $myrow[&quot;date&quot;], $myrow[&quot;snumber&quot;], $myrow[&quot;name&quot;]);
} while ($myrow = mysql_fetch_array($result));
echo &quot;</table>\n&quot;;
} else {
echo &quot;Sorry, no records were found!&quot;;
}
?>
</body>
</html>

On Loan to is blank because I can't figure out how to display the information from Accessory database (userId) in the table.



How do I get the index page to show the ITEMS (all info from accessory table) along with the person its on loan to (not by userId number but by name instead), and how do I get the form in insert.php to include the User list from the user table in the database?
 
WizyWig,

Try this. If this does not work, then provide any errors that may occur so we can go to the next step.

<html>
<body>
<?php
$db = mysql_pconnect(&quot;localhost&quot;, &quot;root&quot;);
@mysql_select_db(&quot;accessories&quot;,$db) or die(mysql_error());

if ($submit) {
// process form
$sql = &quot;INSERT INTO accessory (item,snumber,userId) VALUES ('$item','$snumber','$userId')&quot;;
$result = mysql_query($sql) or die(mysql_error().$sql);

echo &quot;Thank you! Information entered. Click <a href=index.php>here</a> to view list.\n&quot;;
}
else{
// display form
?>
<form method=&quot;post&quot; action=&quot;<?php echo $PHP_SELF; ?>&quot;>
<table width=40%>
<tr>
<td>Item:</td>
<td><input type=&quot;Text&quot; name=&quot;item&quot; size=&quot;25&quot; maxlength=&quot;50&quot;></td>
</tr>
<tr>
<td>SN Number:</td>
<td><input type=&quot;Text&quot; name=&quot;snumber&quot; size=&quot;25&quot; maxlength=&quot;50&quot;></td>
</tr>
<tr>
<td>On Loan to:</td>
<td><select name=&quot;userId&quot;>
<?php
$q=&quot;select userId, name from user&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;name&quot;].&quot;</option>&quot;;
}
?>
</select></td>
</tr>
</table>

<input type=&quot;Submit&quot; name=&quot;submit&quot; value=&quot;Enter information&quot;>
</form>

<?php
} // end if
?>
</body>
</html>

And our index page:

<html>
<body>
<?php
$db = mysql_pconnect(&quot;localhost&quot;, &quot;root&quot;);
@mysql_select_db(&quot;accessories&quot;,$db);

$q = &quot;SELECT * FROM accessory&quot;;
$result = mysql_query($q) or die(mysql_error().$q);
$num_rows = mysql_num_rows($r);

if($num_rows > 0) {
?>

<table border=1 width=100%>
<tr>
<td>Item</td>
<td>Date Purchased</td>
<td>SN Number</td>
<td>On Loan to</td>
</tr>

<?php

while($myrow = mysql_fetch_array($result)) {
echo &quot; <tr>
<td>&quot;.$myrow[&quot;item&quot;].&quot;</td>
<td>&quot;.$myrow[&quot;date&quot;].&quot;</td>
<td>&quot;.$myrow[&quot;snumber&quot;].&quot;</td>
<td>&quot;.$myrow[&quot;name&quot;].&quot;</td>
</tr>\n&quot;;
}

?>
</table>
<?php
}
else {
echo &quot;No records were found!\n&quot;;
}
?>
</body>
</html>


Chad. ICQ: 54380631
 
Thanks the insert worked (got the users to show up)

Just have an error on the index page:

Warning: Supplied argument is not a valid MySQL result resource in /var/ on line 9
No records were found!



And line 9 =

$num_rows = mysql_num_rows($r);


Thanks SOOOO much in advance
 
Sorry,

it should be $num_rows = mysql_num_rows($result);

Chad. ICQ: 54380631
 
Cool, now one last problem:

under the On Loan to, its pulling a number (the userId) instead of showing the name of hte person who has it (on the index page)

how do i associate userId in accessory (to) name in user?
 
Let's try the following:

Where we have

$q = &quot;SELECT * FROM accessory&quot;;
$result = mysql_query($q) or die(mysql_error().$q);
$num_rows = mysql_num_rows($r);

do this

$q = &quot;SELECT user.name,accessory.* FROM user,accessory WHERE user.userId=accessory.userId&quot;;
$result = mysql_query($q) or die(mysql_error().$q);
$num_rows = mysql_num_rows($r);

Chad. ICQ: 54380631
 
Using:

Code:
$q = &quot;SELECT user.name,accessory.* FROM user,accessory WHERE user.userId=accessory.userId&quot;;
$result = mysql_query($q) or die(mysql_error().$q);
$num_rows = mysql_num_rows($result);


I still get numbers under On Loan to: in the table.

Do I need to also change:

Code:
<?php

while($myrow = mysql_fetch_array($result)) { 
     echo &quot;  <tr>
    <td>&quot;.$myrow[&quot;item&quot;].&quot;</td>
    <td>&quot;.$myrow[&quot;snumber&quot;].&quot;</td>
    <td>&quot;.$myrow[&quot;userId&quot;].&quot;</td>
  </tr>\n&quot;; 
}

?>
 
WHOOHoo! Got it working !

Thanks Chad for all your help, I had to correct the code in the query part, and used left join to do it ^_^.

to show you

Code:
$q = &quot;select accessory.accessId ,accessory.item,accessory.snumber,accessory.userId, user.userId, user.name from accessory left join user on accessory.userId = user.userId &quot;;
$result = mysql_query($q) or die(mysql_error().$q);
$num_rows = mysql_num_rows($result);

if($num_rows > 0) {
?>
<center>
<table border=1 width=700>
  <tr>
    <td width=500>Item</td>
    <td width=100>Serial Number</td>
    <td width=100>On Loan to</td>
  </tr>

<?php

while($myrow = mysql_fetch_array($result)) { 
     echo &quot;  <tr>
    <td>&quot;.$myrow[&quot;item&quot;].&quot;</td>
    <td>&quot;.$myrow[&quot;snumber&quot;].&quot;</td>
    <td>&quot;.$myrow[&quot;name&quot;].&quot;</td>
  </tr>\n&quot;; 
}

?> 
</table>
</center>

Is the new coding and the names show up now ^_^

thanks again; i'll probably be back later to ask about updating the info through php (re-accessing the database and updating the information through a form) but first gotta play around with some coding.

 
Be weary of left joins. The workings of left joins are that they do a row-by-row search for matches based on your query (ON and USING()), and if something does not match on the left table, a resulting row is still returned (a 'fake' row) and NULLS are put in place to accommodate. Also, there is greater overhead when using left joins.

I placed a sample on a dev box of mine using the straight join I provided in my prior posting (SELECT user.name,accessory.* FROM user,accessory WHERE user.userId=accessory.userId).


and you can do inserts here


I ran tests using
'SELECT user.name,accessory.* FROM user,accessory WHERE user.userId=accessory.userId'

and your left join
'select accessory.accessId ,accessory.item,accessory.snumber,accessory.userId, user.userId, user.name from accessory left join user on accessory.userId = user.userId'
and correct information is returned for both, but we get extraneous columns in yours (userId) which is not necessary, and there is an extra (approximate) .5 seconds longer delay.

Check it out and let me know what you think.

Chad. ICQ: 54380631
 
Chad, it worked both ways, thanks for clearing up that left join coding.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top