Reputation: 15
I'm trying to create a table with links that return a 'mf_id' value and its corresponding 'Manufacturer' value. I can do one at a time, but when I try to combine the two, problems begin to crop up. Here's what I have so far:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><a href=\"list.php?mf_id&&Manufacturer=" . $row['mf_id'&&'Manufacturer'] . "\">" . $row['Manufacturer'] . "</a></td>";
echo "</tr>";
}
and the other page:
$numb = $_GET['mf_id'];
$name = $_GET['Manufacturer'];
echo "<h1>$name</h1>";
$result=mysql_query("select * from Products where mf_id=$numb");
Thanks in advance!
Upvotes: 0
Views: 117
Reputation: 7040
Because you never pass Manufacturer
through your querystring, the second page doesn't have access to it via GET
. Also, for validity purposes, your querystring values should be passed through urlencode()
.
This line:
echo "<td><a href=\"list.php?mf_id=" . $row['mf_id'] . "\">" . $row['Manufacturer'] . "</a></td>";
Should be:
echo "<td><a href=\"list.php?mf_id=" . urlencode($row['mf_id']) . "&Manufacturer=" . urlencode($row['Manufacturer']) . "'\">" . $row['Manufacturer'] . "</a></td>";
Please Note: It may not help answer your question, but you should stop using mysql_*
functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this article.
UPDATE:
Per meagar's advice I learned about http_build_query()
. This is definitely the way to go when writing querystrings to URLs:
$data = array('mf_id' => $row['mf_id'], 'Manufacturer' => $row['Manufacturer']);
echo "<td><a href='list.php?" . http_build_query($data) . "'>" . $row['Manufacturer'] . "</a></td>";
Upvotes: 4
Reputation: 239312
This doesn't make any sense at all: $row['mf_id'&&'Manufacturer']
. That is not how you access two elements of an array. You're combining two strings with &&
, yielding boolean true
, and attempting to access $row[true]
. You can't access an array that way.
If you want to use both items, you need to access them individually:
$row['mf_id'] . $row['Manufacturer']
If you want to build a query string containing these two values, you should use http_build_query which will take care of URL-encoding your data:
$query = http_build_query(array('mf_id' => $row['mf_id'], 'manufacturer' => $row['Manufacturer']));
echo '<td><a href="list.php?' . $query . '">' . $row['Manufacturer'] . '</a></td>';
Note that, if you actually just select the fields you need, you don't have to explicitly specify them in the arguments to http_build_query
. If your $row
already contains only mf_id
and manufacturer
, it would be enough to use
$query = http_build_query($row);
Upvotes: 1
Reputation: 4048
You're only passing mf_id into the page, you are not passing Manufacturer.
Edit (as you've changed your code)
Change:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><a href=\"list.php?mf_id&&Manufacturer=" . $row['mf_id'&&'Manufacturer'] . "\">" . $row['Manufacturer'] . "</a></td>";
echo "</tr>";
}
To:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><a href=\"list.php?mf_id=".$row['mf_id']."&Manufacturer=" . $row['Manufacturer'] . "\">" . $row['Manufacturer'] . "</a></td>";
echo "</tr>";
}
Upvotes: 0