Current location: Hot Scripts Forums » Programming Languages » PHP » using ?id=$id from a mysql table in php

using ?id=$id from a mysql table in php

Reply
  #1 (permalink)  
Old 06-29-04, 05:32 PM
kasper's Avatar
kasper kasper is offline
Newbie Coder
 
Join Date: Jun 2004
Location: CA
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
using ?id=$id from a mysql table in php

Hey guys, I have a page that is loaded using php/mysql displays the results into a table, i have added an extra column for a "up date / change" option.

how do I use the ?id=$id for a link, for each individual row (person), something like my website/update.php?id=$id where $id would be the persons id from my DB.. code as follows:

<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM EmpInfo";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Employee Database</center></b><br><br>";

?><body background="back.gif">
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Employee</font></th>
<th><font face="Arial, Helvetica, sans-serif">EmployeeID</font></th>
<th><font face="Arial, Helvetica, sans-serif">CrewID</font></th>
<th><font face="Arial, Helvetica, sans-serif">Update / Delete</font></th>
</tr>

<?
$i=0;
while ($i < $num) {
$Employee=mysql_result($result,$i,"Employee");
$EmployeeID=mysql_result($result,$i,"EmployeeID");
$CrewID=mysql_result($result,$i,"CrewID");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$Employee"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$EmployeeID"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$CrewID"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="update.php?id=$id">Update</a></font></td>
</tr>
<?
++$i;
}
echo "</table>";


?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-29-04, 05:52 PM
nekeno12 nekeno12 is offline
Wannabe Coder
 
Join Date: May 2004
Location: CO
Posts: 214
Thanks: 0
Thanked 0 Times in 0 Posts
"><a href="update.php?id=$_GET[id]">
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-29-04, 06:12 PM
kasper's Avatar
kasper kasper is offline
Newbie Coder
 
Join Date: Jun 2004
Location: CA
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
tried that, but it just comes back with a blank page. ?

do i hav eto call it with an <? echo or similar?

thankx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 06-29-04, 06:30 PM
nekeno12 nekeno12 is offline
Wannabe Coder
 
Join Date: May 2004
Location: CO
Posts: 214
Thanks: 0
Thanked 0 Times in 0 Posts
Yup,

Try a href="update.php?id=<? echo("$_GET[id]"); ?>">Update</a>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 06-29-04, 06:52 PM
kasper's Avatar
kasper kasper is offline
Newbie Coder
 
Join Date: Jun 2004
Location: CA
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
I'm missing someing here still,, it's link is showing "update.php?ID="


it's not getting the ID... am I missing a call? like ... a WHERE ID=$ID or something?
and if so where would I put it?

thankx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 06-29-04, 07:53 PM
infinitylimit's Avatar
infinitylimit infinitylimit is offline
Code Guru
 
Join Date: Jun 2004
Location: Oregon
Posts: 757
Thanks: 0
Thanked 0 Times in 0 Posts
Talking

First of all the code you supplied doesn't have an $id defined so that is why you are having a problem. I'm assuming you want to use the primary key of the table and that primary key is EmployeeID. Below is the code that will work.

PHP Code:
<a href="update.php?id=<?php echo $EmployeeID?>">Update</a>
update.php will need to do a $_GET['id'] to be able to find the information needed.

I also noticed that you are using a wildcard SQL statement. If you plan on using this a lot you might want to do something like SELECT employeeID,CrewID, ... ect instead of *

You might also peek at http://us2.php.net/mysql_result for information regarding it's use on large sets of data.

Hope this helps,
-IL
__________________
PHP AID -- Get PHP Help Live, or view our scripts, forums, resources, and more.
Hawk Enterprises -- Home to PHP games, open-source code, tutorials and free downloads
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 06-30-04, 08:56 AM
kasper's Avatar
kasper kasper is offline
Newbie Coder
 
Join Date: Jun 2004
Location: CA
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
still missing something (i'm an idiot )
when usuing what u suggested, the link is right with the update.php?id=employee id here. but when clicked it looks to me like its still trying to use the ID instead.... primary key is ID with autoinc and now EmployeeID.

employee.php:
*********
<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT Employee,EmployeeID,CrewID FROM EmpInfo";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Employee Database</center></b><br><br>";

?><body background="arb.gif">
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Employee</font></th>
<th><font face="Arial, Helvetica, sans-serif">EmployeeID</font></th>
<th><font face="Arial, Helvetica, sans-serif">CrewID</font></th>
<th><font face="Arial, Helvetica, sans-serif">Update</font></th>
<th><font face="Arial, Helvetica, sans-serif">Delete</font></th>
</tr>

<?
$i=0;
while ($i < $num) {
$Employee=mysql_result($result,$i,"Employee");
$EmployeeID=mysql_result($result,$i,"EmployeeID");
$CrewID=mysql_result($result,$i,"CrewID");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$Employee"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$EmployeeID"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$CrewID"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="update.php?ID=<?php echo $EmployeeID; ?>">Update</a></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="delete.php?ID=<?php echo $EmployeeID; ?>">Delete</a></font></td>
</tr>
<?
++$i;
}
echo "</table>";


?>


**************
update.php
**************
<?
include("dbinfo.inc.php");
$ID=$_GET['ID'];
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT Employee,EmployeeID,CrewID FROM EmpInfo WHERE ID='$ID'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

$i=0;
while ($i < $num) {
$Employee=mysql_result($result,$i,"Employee");
$EmployeeID=mysql_result($result,$i,"EmployeeID");
$CrewID=mysql_result($result,$i,"CrewID");


?>

<form action="updated.php" method="post">
<input type="hidden" name="ud_ID" value="<? echo "$ID"; ?>">
Employee: <input type="text" name="ud_Employee" value="<? echo "$Employee"?>"><br>
Employee ID: <input type="text" name="ud_EmployeeID" value="<? echo "$EmployeeID"?>"><br>
Crew ID: <input type="text" name="ud_CrewID" value="<? echo "$CrewID"?>"><br>
<input type="Submit" value="Update">
</form>

<?
++$i;
}
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 06-30-04, 10:30 AM
mikaelf mikaelf is offline
Wannabe Coder
 
Join Date: Jun 2004
Location: php[dot]net
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
May be I can give little help here...

Quote:
Originally Posted by kasper
still missing something (i'm an idiot )
when usuing what u suggested, the link is right with the update.php?id=employee id here. but when clicked it looks to me like its still trying to use the ID instead.... primary key is ID with autoinc and now EmployeeID.

employee.php:
*********
<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT Employee,EmployeeID,CrewID FROM EmpInfo";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Employee Database</center></b><br><br>";

?><body background="arb.gif">
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Employee</font></th>
<th><font face="Arial, Helvetica, sans-serif">EmployeeID</font></th>
<th><font face="Arial, Helvetica, sans-serif">CrewID</font></th>
<th><font face="Arial, Helvetica, sans-serif">Update</font></th>
<th><font face="Arial, Helvetica, sans-serif">Delete</font></th>
</tr>

<?
$i=0;
while ($i < $num) {
$Employee=mysql_result($result,$i,"Employee");
$EmployeeID=mysql_result($result,$i,"EmployeeID");
$CrewID=mysql_result($result,$i,"CrewID");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$Employee"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$EmployeeID"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$CrewID"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="update.php?ID=<?php echo $EmployeeID; ?>">Update</a></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="delete.php?ID=<?php echo $EmployeeID; ?>">Delete</a></font></td>
</tr>
<?
++$i;
}
echo "</table>";


?>


**************
update.php
**************
<?
include("dbinfo.inc.php");
$ID=$_GET['ID'];
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT Employee,EmployeeID,CrewID FROM EmpInfo WHERE ID='$ID'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

$i=0;
while ($i < $num) {
$Employee=mysql_result($result,$i,"Employee");
$EmployeeID=mysql_result($result,$i,"EmployeeID");
$CrewID=mysql_result($result,$i,"CrewID");


?>

<form action="updated.php" method="post">
<input type="hidden" name="ud_ID" value="<? echo "$ID"; ?>">
Employee: <input type="text" name="ud_Employee" value="<? echo "$Employee"?>"><br>
Employee ID: <input type="text" name="ud_EmployeeID" value="<? echo "$EmployeeID"?>"><br>
Crew ID: <input type="text" name="ud_CrewID" value="<? echo "$CrewID"?>"><br>
<input type="Submit" value="Update">
</form>

<?
++$i;
}
?>
Pay attention to the bolded text:
1.I believe that mysql_numrows is not a valid PHP-built-in function. it should be mysql_num_rows
2.the loop will never start because the condition is always false ($num = 0 while $i increases with step 1).

Solution:
Change mysql_numrows into mysql_num_rows
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 06-30-04, 11:16 AM
kasper's Avatar
kasper kasper is offline
Newbie Coder
 
Join Date: Jun 2004
Location: CA
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
same result,

it is using the link update.php?id=(employee's ID) but when the page comes up it actually brings up the autoic'd ID, so loading up someone with the EmployeeID of 230, actually brings up the record number 230.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 06-30-04, 12:53 PM
infinitylimit's Avatar
infinitylimit infinitylimit is offline
Code Guru
 
Join Date: Jun 2004
Location: Oregon
Posts: 757
Thanks: 0
Thanked 0 Times in 0 Posts
I'm a little confused by your statment.... can you provide a dump of your sql table structure here and also your update code, and state what it's trying to do?
__________________
PHP AID -- Get PHP Help Live, or view our scripts, forums, resources, and more.
Hawk Enterprises -- Home to PHP games, open-source code, tutorials and free downloads
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
Database table contents to email problem (PHP and MySQL) blokeofftheinternet PHP 2 04-29-04 09:34 AM
cant add more than 1 mysql table with php script chrisb62 PHP 1 04-27-04 04:00 AM
Newbie MySQL fccolon PHP 2 03-16-04 10:54 AM
Error when trying to create MySQL table via PHP HasansWeb PHP 2 01-06-04 05:13 PM


All times are GMT -5. The time now is 03:57 AM.
vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.