473,549 Members | 2,745 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 1557
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_incrementi ng 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
5143
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; foreach ($resultArray as $data) { $tableName = mysql_field_table($result, $i);
1
8217
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 size of the database as a float (ie. 0.75 for 0.75mb). Update usage set to 1 indicates the DB should update its size information before giving you...
2
3050
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 Size of DB
3
1507
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
5286
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 to add a row to the database and I do it like this: DataSet dataSet = new DataSet();
0
866
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" (is that the term, it is in JDOM...) <whatever thefieldtomatchURL="12345" dbtable="table1"> <db field="field1"/>
2
1157
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: Operation must use an updateable query. The line of code with the error is: objCmd.ExecuteNonQuery
1
1215
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 database? Samples and URLs would help.
1
1458
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 as question numbers) - The Survey Extra Results (As additional questions can be listed into the Questions table, has 3 columns (a link/id to the...
2
1335
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 without refering to the connection string which is usually used to create the connection with the database. i would appreciate for the help.....
0
7720
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7470
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7809
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6043
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5368
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3500
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1941
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 we have to send another system
0
763
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.