Jump to content

[SOLVED] Retrieving rows from database problem


Maracles

Recommended Posts

I am working on a personal website that in part contains a small 'shares' profile, one of the pages of which will list all owned shares in a single table. Each row is a unique share. The information is called from the database on page load.

 

This table provide only summary information on each share and the user must click a 'view' button next to each share to go to that individual stocks profile with more info.

 

My problem is that I don't know how to link the 'View' button with the actual stock so that when it is clicked the user is presented with a page with that shares detailed info.

 

When a user clicks 'View' the page must know firstly which row of the table and which stock the user has clicked view for (which I don't know how to do) and then post a variable which identifies the stock to the following page which will be a template whose values simply change based on the chosen stock.

 

Can anyone help? I can show code if needed however it is a  bit of a mess at the moment!

 

 

 

 

 

Link to comment
Share on other sites

Thank you for your reply. I understand the concept but i'm not too sure how to include that code. I have included some of my actual code if this helps. It is worth noting that each share already has a unique id in its 'cxcode'.

for ($i=0; $i < $num_results; $i++) {
	$row = mysql_fetch_assoc ($result);
	echo '<tr><td>';
	echo '<p><strong>'.($i+1).'.</td><td>';
	echo htmlspecialchars(stripslashes($row['title']));
	echo "</strong></td><td>";
	echo stripslashes($row['cxcode']);
	echo '</td><td>';
	echo stripslashes($row['reldate']);
	echo '</td><td>';
	echo stripslashes($row['strtdate']);
	echo '</td><td>';
	echo stripslashes($row['enddate']);
	echo '</td><td>';
	echo '<form action="stckprofile.php"><input type="submit" value="View" name="stckview"></form>';
	echo '</td><td>';
	echo '<form action=""><input type="submit" value="Delete" name="stckdel"></form>';
	echo '</td>';

 

 

 

Link to comment
Share on other sites

for ($i=0; $i < $num_results; $i++) {
      $row = mysql_fetch_assoc ($result);
      echo '<tr><td>';
      echo '<p><strong>'.($i+1).'.</td><td>';
      echo htmlspecialchars(stripslashes($row['title']));
      echo "</strong></td><td>";
      echo '<a href="viewstock.php?id='.stripslashes($row['cxcode']).'">View</a>';
      echo '</td><td>';
      echo stripslashes($row['reldate']);
      echo '</td><td>';
      echo stripslashes($row['strtdate']);
      echo '</td><td>';
      echo stripslashes($row['enddate']);
      echo '</td><td>';
      echo '<form action="stckprofile.php"><input type="submit" value="View" name="stckview"></form>';
      echo '</td><td>';
      echo '<form action=""><input type="submit" value="Delete" name="stckdel"></form>';
      echo '</td>';

 

Then on viewstock.php you just need to get the variable like this:

$cxcode = $_GET['id'];

Link to comment
Share on other sites

Ok, I see what you mean however because I'm still very much a PHP beginner I have one more problem.

 

I have never worked with the URL like the one you use i.e. with the '?id=' and having tried your code the link will not connect to the viewstock.php page. The link 'appears to be broken'.

 

When you add an '?id' to a link does it change the way you have to link to the page?

 

Thanks again, this is helping me big time!

Link to comment
Share on other sites

Hi,

 

you need to create the viewstock.php page yourself...

 

After any php file you can add ?anything=variable - this is simply to send variables to another page. You can add many variables by using &... example: ?name=john&age=20&userid=28294

 

then you can get these variables on the page you visit. Say you link to viewstock.php?name=john&age=20&userid=28294

 

then you can create viewstock.php and get these variables like this:

<?php
$username = $_GET['name'];
$userage = $_GET['age'];
$userid = $_GET['userid'];
?>

 

Now you can also use these variables to make a new SQL query and get stock info (viewstock.php?id=10242):

<?php
$stockid = $_GET['id'];
$sql = "select * from stocks WHERE id = ".mysql_real_escape_string($stockid)."";
$res = mysql_query($sql);
....

?>

Link to comment
Share on other sites

A-ha! Firstly, I found my problem which was that the url had a typo which was why it wasn't linking, I have now changed this and it links and the viewstock.php page is updating as desired.

 

Secondly, that is extremely useful info regarding the links and variables that I did not know about! I'm currently travelling and do not have access to some of the PHP tutorial books I have been using so have been struggling to find out methods I don't already know.

 

Thirdly, thanks again for the help, that was the last big problem I had with developing the page basics and have been trying for ages to figure it out.

 

I will mark this solved.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.