Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-08-25
4
Medium Priority
?
743 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 17

Accepted Solution

by:
Chris Harte earned 1000 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 44

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 1000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

715 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