473,503 Members | 12,103 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP Getting row from database by Id

97 New Member
I'm trying to display a single record (row) from the database when a user clicks on a link. There's a page where all the records are displayed and there's a link next to every record. When the user clicks on the link it goes to a new page where I'm trying to display the record that the user clicked on.

The link is like this:

Expand|Select|Wrap|Line Numbers
  1. echo '<a href="delete.php">Delete</a>'; 
  2.  
Which is one of about six items displayed, some of which are from the database. When they click on this link the page is directed to delete.php which has this:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include ('includes/DbCon.php');
  3. $query = 'SELECT headline, body, image, newsDate FROM news WHERE id = '. intval($_GET['$id']);
  4. $result = $mysqli->query ($query);
  5.  
  6. while($row = $mysqli->fetch_array) {
  7. {
  8. echo "<table>";
  9. echo "<tr><td>";
  10. echo $row['headline'];
  11. echo "</td></tr>";
  12. echo "</td><td>";
  13. echo $row['body'];
  14. echo "</td><td>";
  15. echo $row['date'];
  16. echo "</td><td>";
  17. echo "<p><img src=".$dir.$row['image']." alt=''></p>";
  18. echo "</td></tr>";
  19. }
  20. echo "</table>";
  21. mysqli_close($mysqli);
  22. ?>
  23.  
I've tried while loops, for loops and if statements and none of them work. I thought it was a for loop, but couldn't make it work.
Jun 11 '15 #1
3 1550
computerfox
276 Contributor
By the looks of it you're trying to do a simple GUI step by step. Is this your first GUI?

Anyway...

What's you're identifier? Do you have a column that is an int and is set to auto_increment?
For your case, the identifier would be header, buys it's a character type and probably isn't even a primary key, right? You would need to add an auto_incrementing column so that the id you pass to the second page won't have spaces and it would be easier to retrieve the data.

After you have your identifier, you would do:
Expand|Select|Wrap|Line Numbers
  1.  
  2. print "<a href='delete.php?id=".$row['id']."'>Delete</a>";
  3.  
And on the second page you should be able to grab it with:
Expand|Select|Wrap|Line Numbers
  1. $id=$_GET['id'];
  2. $sql="select* from news where id='$id'";
  3.  
In other words you're not passing any data to the URL.

Hope that helps!
Jun 11 '15 #2
tdrsam
97 New Member
Thanks again Computerfox. That got it working.

I was also calling my array the wrong way.

Now I'm trying to style the results, but anyway ...

I couldn't have gotten this going without your help. And, no this is not my first GUI, but it's not far from it. It's about my second or third.
Jun 12 '15 #3
computerfox
276 Contributor
Nice! Keep working on it. PHP is a beautiful and easy to use language. Try to avoid copy/paste because it'll be harder to learn and not helpful to understand it.

Can't really help you with styling though.... I can designs systems, but I can't make a single page look fancy. Oh that's another thing NEVER risk functionality for "fancy". It can look and feel like the best site, but if there's broken code, it doesn't mean anything. Good luck!
Jun 12 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
5140
by: ChronoFish | last post by:
In PHP you can retrieve the table.column name of a MySQL query doing something like this: $result = mysql_query($query, $dbConnection); $resultArray = mysql_fetch_row($result); $i = 0;...
1
8209
by: Robin Tucker | last post by:
As I had real problems working my head around sp_spaceused, I've written an SP to do it (I also noted a lot of questions about this when "searching"). Pass in a database name and it will return the...
2
3047
by: Derrick King | last post by:
COuld someone tell me if its possible to get hold of stats about all Databases on a SQL Server. The sort of things I would like are Name of DB Location of DB Size of DB Location of Logfile...
3
1502
by: Sylvain Provencher | last post by:
Hi! There is a way to obtain tables and fields info from an SQL connection. like tables name, fields name, type and width ? I'm writing a VB.NET application. Thanks
3
5283
by: Saso Zagoranski | last post by:
Hi! A question about database tables... I have a SELECT statement: SELECT something FROM someTable WHERE code = @code The parameter code starts with 1 and is autoincremented... Now I want...
0
863
by: Luis Esteban Valencia | last post by:
Hi experts What I'd like it is something like this... I have an xml file and when going to the page, I want the page to look at the paramater passed in with the URL and gets the XML "element"...
2
1155
by: RAB | last post by:
I tested my code on my local computer and it worked fine. When I uploaded it to my webserver at Godaddy.com, I get the following error... Exception Details: System.Data.OleDb.OleDbException:...
1
1211
by: VG | last post by:
Hi, I want to hookup some database change events automatically to my .NET application. The backend is SQLServer 2003. Is this possible? How do I get notified automatically of changes in my...
1
1454
by: dallasfreeman | last post by:
I'm looking at a quick way to get results that are displayed as rows to display as columns. I have three tables:- - The Questions for the survey - The Results of the survey (Columns are listed...
2
1333
by: Adi | last post by:
hi everyone... i have a doubt.. is there any way i can get the database name.. my page is accessing.. i would like to display the database name, the current page is accessing. This has to be done...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7296
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5604
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
405
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.