/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Select items from $Query and pass onto next page??

Hi Guys..

I’m new to this forum, and have a question for you.
I have an aplication, similar to a shopping cart, where the user searches for a product, the mysql_query brings back a list of items, as you would expect and they should be able to select an item and move the item onto the next page. All seems ok, in that, when I hover over the selected item, it displays the correct ‘id’ link at bottom, and when I click to go to the next page, it displays the correct id number in the URL at the top, but when I try to echo the details on the page, it displays the LAST item in the array, and not the one I selected??

here is my query to give you an idea of what I mean..

[code=php]<?php
$query = mysql_query(“SELECT *
FROM full_details
WHERE from_town =’$fromPort’ AND to_town =’$toTown'”);

echo “<h2>Results</h2><p>”;
echo “Going from: ” . $_SESSION[‘fromPort’] . ” – Going To: ” . $_SESSION[‘toTown’] . “<br>”;
//Display the results from $search
while($result=mysql_fetch_array($query)){
?>
<table width=”550″ height=”91″ border=”1″ align=”right” background=”images/resultsBG.png”>
<tr>
<td width=”150″ rowspan=”2″>
<img src=”show_image.php?id=<?php echo $result[“id”]; ?>” />
</td>
<td width=”436″ height=”31″><h3><?php $_SESSION[“id”] = $result[“id”]; echo $_SESSION[“id”] . “&nbsp” . $result[“vehicle”]; ?></h3></td>
<td width=”100″><h3><?php echo $result[“cost”] ?></h3></td>
</tr>
<tr>
<td colspan=”2″><?php echo $result[“description”]?>
<table width=”25″ border=”0″ align=”right” height=”25″>
<tr>
<td><a href=”payments3.php?id=<?php echo $_SESSION[“id”]; ?>”><img src=”images/buttonBG.png” width=”25″ height=”25″ /></a></td>
</tr>
</table>
</td>
</tr>
</table>
<?php
}
//This counts the number or results – and if there wasn’t any it gives them a little message explaining that
$anymatches=mysql_num_rows($query);
if ($anymatches == 0) {
echo “Sorry, but we can not find an entry to match your query<br><br>”;
}
?>[/code]

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@DexterMorganNov 06.2010 — So on the next page you are trying to the the information out of the session?

I do not fully understand so if I am wrong please explain.

Anyway if you are saving all the results in a session then you keep overwriting the values:
[code=php]<td width="436" height="31"><h3><?php $_SESSION["id"] = $result["id"]; echo $_SESSION["id"] . "&nbsp" . $result["vehicle"]; ?></h3></td>
[/code]


here you keep setting the $_SESSION['id'], so you set it once, then overwrite and so on, so only the last result will be stored.

I am not sure if this helps
Copy linkTweet thisAlerts:
@mickyd69authorNov 07.2010 — Thanks for that Dexter..

Yes you are initially right in what you are saying, however, I have tried the code previous with just;

<td width="436" height="31"><h3><?php echo $result["id"] . "&nbsp" . $result["vehicle"]; ?></h3></td>

but the result is the same; instead of carrying forward the item I select, it carries the last item in the array, yet the URL [U]DOES[/U] carry the selected item forward?

Mick!
Copy linkTweet thisAlerts:
@DexterMorganNov 07.2010 — alright can you explain to me what you are trying to do, without any code.

btw why are some $result["id"] and others $_SESSION["id"] :S
Copy linkTweet thisAlerts:
@mickyd69authorNov 07.2010 — Ok... basically..

I have a search form that can be used to search for various vehicles, when you click submit, it pulls all vehicles matching your search criteria from the database and displays them on screen. What I want to do, is be able to click on a particlular vehicle from that search and send the data to the next page. ..only thing is, is that it is sending the last item in the list and not the one that I am selecting.

The original code I wrote had it as $result["id"], ..the $_SESSION["id"] was just me fooling around with the code, trying different things. (which didn't work).

Mick!
Copy linkTweet thisAlerts:
@DexterMorganNov 07.2010 — alright ok, I understand.

Well you do not use sessions to do this, you probably could put it is a little more complex and maybe wasteful.

so get rid of the sessions on the page, they are not necessary.

you say you want to send the data to the next page, really you should only send some sort of identifier and get the values.

is payments3.php the page that you want to see the data about the clicked on vehicle on, if that makes sense :S
Copy linkTweet thisAlerts:
@mickyd69authorNov 07.2010 — Yes... it is.

What's the best way then without using sessions?
Copy linkTweet thisAlerts:
@DexterMorganNov 07.2010 — alright so on the payments3.php page you would do another database query.

You have got an ID so you just get that using $_REQUEST.

Then you will do:

[code=php]
$query = sprintf("SELECT * FROM full_details WHERE id='&#37;u' LIMIT 1",$_REQUEST['id']);
[/code]

BTW I have complicated it a bit for security reasons.

Let me know how it goes.
Copy linkTweet thisAlerts:
@mickyd69authorNov 07.2010 — Cheers Dexter...

I'll give it a test in the morning, and let you know!

Mick!
Copy linkTweet thisAlerts:
@mickyd69authorNov 08.2010 — Dexter...

You are my new PHP Guru!!! ...worked perfect! ?

$query = sprintf("SELECT * FROM full_details WHERE id='%u' LIMIT 1",$_REQUEST['id']);

$result = mysql_query($query);

$row = mysql_fetch_array($result);

echo $row["id"] . "<br>";

Thanks a lot, this helped me BIG style!!

Mick!

PS - Anything you need help with, just ask, if I can I will!
Copy linkTweet thisAlerts:
@DexterMorganNov 08.2010 — [code=php]$query = mysql_query("SELECT *
FROM full_details
WHERE from_town ='$fromPort' AND to_town ='$toTown'"); [/code]

BTW here you are open to SQL injection attacks, these are very bad lol.

http://en.wikipedia.org/wiki/SQL_injection

You can help prevent these attacks by 'escaping' the values before you pass them into the query:
[code=php]$fromPort = mysql_real_escape_string($fromPort);[/code]

if you do this for both values then it should help to prevent the problem.
Copy linkTweet thisAlerts:
@mickyd69authorNov 08.2010 — Ah... thanks for that, will do!

Cheers!
Copy linkTweet thisAlerts:
@mickyd69authorNov 13.2010 — Something strange is afoot??

I transfered the site to the live server, and I'm getting an error on the payments page (which is now the confirm.php page, but still works on the testing server), after it pulls the data through?

still using exactally the same code, haven't altered anything? But now I'm getting..

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /confirm.php on line 8

Any ideas why this won't work live?

Mick!
Copy linkTweet thisAlerts:
@mickyd69authorNov 13.2010 — ...it's ok, sorted it! for some very strange reason, it was not selecting the databse?? I had to manually reconnect the connection, even though all the other pages are using the same connection and working fine?

Mick!
Copy linkTweet thisAlerts:
@DexterMorganNov 13.2010 — ...it's ok, sorted it! for some very strange reason, it was not selecting the databse?? I had to manually reconnect the connection, even though all the other pages are using the same connection and working fine?

Mick![/QUOTE]

Ahh alright, I used to use a single include file which connected to the database when I needed to use the database.

BTW you should check out MYSQLI, only one line of code to connect to the database using that.

http://www.php.net/manual/en/mysqli.overview.php

"If you are using MySQL versions 4.1.3 or later it is strongly recommended that you use the mysqli extension instead. " - The PHP manual strongly recommends that you use it lol
Copy linkTweet thisAlerts:
@mickyd69authorNov 13.2010 — that's exactally what I normally do, and is set like that on the other pages,

<?php include('Connections/justair1.php'); ?>

..sets the connection up and is included on each page. But for some reason known only to itself, will not connect to the database on this page?
Copy linkTweet thisAlerts:
@criterion9Nov 13.2010 — Are you relying on the default of executing queries through the last established connection or are you passing the connection resource to your query execution?
×

Success!

Help @mickyd69 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...