0

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!

2
  • 1
    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. Commented Aug 20, 2012 at 15:20
  • You never pass Manufacturer through your querystring. Also, you should probably urlencode() your querystring values before writing to the page. Commented Aug 20, 2012 at 15:21

3 Answers 3

4

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>";
Sign up to request clarification or add additional context in comments.

2 Comments

PHP is a language built for the web. It has functions to handle building and accessing query strings. You shouldn't be writing your own.
@meagar, who's writing their own? urlencode() is a PHP function.
1

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);

Comments

0

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>";
}

3 Comments

As a guy new to web design, I must ask, how would I pass Manufacturer? I had thought that this little string would do so: echo "<td><a href=\"list.php?mf_id&&Manufacturer=" Apparently I'm mistaken?
No, you must build a string like list.php?var1=text1&var2=text2
@user1582828 As @meagar just informed me, you can store the key/value pairs in an array and pass them through http_build_query(). Take a look at my answer for the details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.