Solved

PHP/MySQL hyperlink to view specific record (on new page) from table of records from database

Posted on 2010-08-25
4
704 Views
Last Modified: 2013-12-13
Hi

I am displaying all table data on my page and I want to create a link next to each record that once clicked takes you to a new page with that records details displayed, so they can be updated. I am new to PHP and also I apologise if my code is messy, however I was going to pretty it up once I got the functionality I need working.

I have got the table showing the records from the 'users' table and have created a link. However I don't think the link has been coded correctly from the users.php page to pass onto the view_user.php file (URL doesn't actually include id when hover over button) and also the code in the view_user.php page to accept the id and display the record is returning the following error message (I've tried manually putting the id into the URL and I still get the following):

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/training/public_html/view_user.php on line 39

users.php

<?php
[indent][/indent]mysql_connect("localhost", "training", "******") or die(mysql_error()); [indent][/indent]mysql_select_db("training_users") or die(mysql_error()); 
[indent][/indent]$data = mysql_query("SELECT * FROM users") or die(mysql_error()); 
[indent][/indent]Print "<table border cellpadding=3  >"; 
[indent][/indent]while($info = mysql_fetch_array( $data )) 
[indent][/indent]{ 
[indent][/indent][indent][/indent]Print "<tr>"; 
[indent][/indent][indent][/indent]Print "<th>Username:</th> <td>".$info['username'] . "</td> "; 
[indent][/indent][indent][/indent]Print "<th>Password:</th> <td>".$info['password'] . "</td> ";
[indent][/indent][indent][/indent]Print '<th><a href="http://89.145.90.66/~training/view_user.php?id='.$id.'"/>"Update</a></th> </tr>';
[indent][/indent] }
[indent][/indent]Print "</table>";  [code]


[b]view_user.php[/b]
[code]

 <?php
[indent][/indent]$id=$_GET['id'];
[indent][/indent]$username="training";
[indent][/indent]$password="******";
[indent][/indent]$database="training_users";
[indent][/indent]mysql_connect(localhost,$username,$password);

[indent][/indent]$query="SELECT * FROM users WHERE id = '$id'";
[indent][/indent]$result=mysql_query($query);
[indent][/indent]$num=mysql_num_rows($result);

[indent][/indent]$i=0;
[indent][/indent]while ($i < $num) 
[indent][/indent]{
[indent][/indent][indent][/indent]$first=mysql_result($result,$i,"id");
[indent][/indent][indent][/indent]$last=mysql_result($result,$i,"username");
[indent][/indent][indent][/indent]$last=mysql_result($result,$i,"password");
[indent][/indent][indent][/indent]$phone=mysql_result($result,$i,"title");
[indent][/indent][indent][/indent]$mobile=mysql_result($result,$i,"forename");
[indent][/indent][indent][/indent]$fax=mysql_result($result,$i,"surname");
[indent][/indent][indent][/indent]$email=mysql_result($result,$i,"sex");
[indent][/indent][indent][/indent]$web=mysql_result($result,$i,"email");
[indent][/indent][indent][/indent]$web=mysql_result($result,$i,"contact_no");
[indent][/indent][indent][/indent]$web=mysql_result($result,$i,"account_manager_email");
[indent][/indent][indent][/indent]$web=mysql_result($result,$i,"account_created_date");
[indent][/indent][indent][/indent]$web=mysql_result($result,$i,"introduction_complete");
[indent][/indent][indent][/indent]$web=mysql_result($result,$i,"introduction_complete_date");
[indent][/indent][indent][/indent]$web=mysql_result($result,$i,"diagnosis_faculty_complete");
[indent][/indent][indent][/indent]$web=mysql_result($result,$i,"diagnosis_faculty_complete_date");
[indent][/indent][indent][/indent]$web=mysql_result($result,$i,"send_a_case_complete");
[indent][/indent][indent][/indent]$web=mysql_result($result,$i,"send_a_case_complete_date");
[indent][/indent][indent][/indent]$web=mysql_result($result,$i,"impression_tracking_complete");
[indent][/indent][indent][/indent]$web=mysql_result($result,$i,"impression_tracking_complete_date");
[indent][/indent][indent][/indent]$web=mysql_result($result,$i,"ipr_complete");
[indent][/indent][indent][/indent]$web=mysql_result($result,$i,"ipr_complete_date");
[indent][/indent][indent][/indent]$web=mysql_result($result,$i,"summary_complete");
[indent][/indent][indent][/indent]$web=mysql_result($result,$i,"summary_complete_date");
[indent][/indent][indent][/indent]$web=mysql_result($result,$i,"training_complete");
[indent][/indent][indent][/indent]$web=mysql_result($result,$i,"training_complete_date");
[indent][/indent][indent][/indent]$web=mysql_result($result,$i,"active");
[indent][/indent]?>

<span class="Content" >
<form action="updated.php" method="post">
[indent][/indent]<input type="hidden" name="ud_id" value="<? echo $id; ?>">
[indent][/indent]Username: <input type="text" name="ud_username" value="<? echo $username; ?>"><br/>
[indent][/indent]Password: <input type="text" name="ud_password" value="<? echo $password; ?>"><br/>
[indent][/indent]Title: <input type="text" name="ud_title" value="<? echo $title; ?>"><br/>
[indent][/indent]Forename: <input type="text" name="ud_forename" value="<? echo $forename; ?>"><br/>
[indent][/indent]Surname: <input type="text" name="ud_surname" value="<? echo $surname; ?>"><br/>
[indent][/indent]Sex: <input type="text" name="ud_sex" value="<? echo $sex; ?>"><br/>
[indent][/indent]Email: <input type="text" name="ud_email" value="<? echo $email; ?>"><br/>
[indent][/indent]Contact No: <input type="text" name="ud_contact_no" value="<? echo $contact_no; ?>"><br/>
[indent][/indent]Account Manager Email: <input type="text" name="ud_account_manager_email" value="<? echo $account_manager_email; ?>"><br/>
[indent][/indent]Account Created Date: <input type="text" name="ud_account_created_date" value="<? echo $account_created_date; ?>"><br/>
[indent][/indent]Introduction Complete: <input type="text" name="ud_introduction_complete" value="<? echo $introduction_complete; ?>"><br/>
[indent][/indent]Introduction Completed Date: <input type="text" name="ud_introduction_complete_date" value="<? echo $introduction_complete_date; ?>"><br/>
[indent][/indent]Diagnostic Faculty Complete: <input type="text" name="ud_diagnosis_faculty_complete" value="<? echo $diagnosis_faculty_complete; ?>"><br/>
[indent][/indent]Diagnostic Faculty Complete Date: <input type="text" name="ud_diagnosis_faculty_complete_date" value="<? echo $diagnosis_faculty_complete_date; ?>"><br/>
[indent][/indent]Send a Case Complete: <input type="text" name="ud_send_a_case_complete" value="<? echo $send_a_case_complete; ?>"><br/>
[indent][/indent]Send a Case Complete Date: <input type="text" name="ud_send_a_case_complete_date" value="<? echo $send_a_case_complete_date; ?>"><br/>
[indent][/indent]Impression Tracking Complete: <input type="text" name="ud_impression_tracking_complete" value="<? echo $impression_tracking_complete; ?>"><br/>
[indent][/indent]Impression Tracking Complete Date: <input type="text" name="ud_impression_tracking_complete_date" value="<? echo $impression_tracking_complete_date; ?>"><br/>
[indent][/indent]IPR Complete: <input type="text" name="ud_ipr_complete" value="<? echo $ipr_complete; ?>"><br/>
[indent][/indent]IPR Complete Date: <input type="text" name="ud_ipr_complete_date" value="<? echo $ipr_complete_date; ?>"><br/>
[indent][/indent]Summary Complete: <input type="text" name="ud_summary_complete" value="<? echo $summary_complete; ?>"><br/>
[indent][/indent]Summary Complete Date: <input type="text" name="ud_summary_complete_date" value="<? echo $summary_complete_date; ?>"><br/>
[indent][/indent]Training Complete: <input type="text" name="ud_training_complete" value="<? echo $training_complete; ?>"><br/>
[indent][/indent]Training Complete Date: <input type="text" name="ud_training_complete_date" value="<? echo $training_complete_date; ?>"><br/>
[indent][/indent]Account Active: <input type="text" name="ud_active" value="<? echo $active; ?>"><br/>
[indent][/indent]<input type="Submit" value="Update">
</form> </span>

<?php
[indent][/indent]++$i;
}
  mysql_close();
?>

Open in new window

0
Comment
Question by:Rogit85
4 Comments
 
LVL 16

Accepted Solution

by:
Chris Harte earned 250 total points
ID: 33522075
I have given this the most cursory of glances, but what instantly strikes me is that error message means you have not run your query against the database. line 23 stands out

mysql_connect(localhost,$username,$password);

Local host should be in quotes

mysql_connect('localhost', $username, $password);
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 33522131
In your users.php file, you're not echoing the ID properly to the URL. You should use $info['id'] instead of $id.

With regard to your view_user.php page - that's a strange (wrong) way of doing things. Have a look below to see how to basically loop through and retrieve data.







Print '<th><a href="http://89.145.90.66/~training/view_user.php?id='.$info['id'].'"/>"Update</a></th> </tr>';



//Looping through a recordset

$query="SELECT * FROM users WHERE id = '$id'";

$result=mysql_query($query);

if ($result)

{

   while ($row=mysql_fetch_array($result)

   {

      //Do whatever you need.

      $first=$row['id'];

      $last=$row['username'];

   }

}

Open in new window

0
 
LVL 3

Assisted Solution

by:Andreaspisc
Andreaspisc earned 250 total points
ID: 33522205
To address the other part of your question, in line 10 you append $id in the generated URL but you never set that variable.  Did you perhaps mean $info['id'] instead?

Also, lines 32 onwards use an odd choice of variable names.  You appear to be putting the id into a variable called $first, the forename into a variable called $mobile and so on.  It will end in tears!
0
 

Author Closing Comment

by:Rogit85
ID: 33522837
Thanks for your prompt help guys.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to remove numeric and alpha from an alphanumeric field? 5 71
get domain with php 7 19
html input type 3 21
Make custom query_posts look show the excerpt only 9 24
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
The viewer will learn how to count occurrences of each item in an array.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now