PHP中的链接回声

PHP中的链接回声

问题描述:

You may have seen from my previous questions, I'm brand new to PHP (and coding) and I'm stuck with this.

I've created this code (try not to cringe):

<?php
$result = mysql_query("SELECT `user_id`, `username`, `email` FROM `users` WHERE `approved` = '0' ORDER BY `username`;");

    echo "<table>";
        while($row = mysql_fetch_array($result))
        {
        echo "<tr><td align=center>" . htmlspecialchars($row['username']) . "</td><td> | </td><td align=center>" . htmlspecialchars($row['email']) . " </td><td> | </td><td><a href=\"approve_user.php?phpuser_id=$row['user_id']\"><font color=\"green\">Approve</font></a> | </td><td><a href=\"deny_user.php?user_id=$row['user_id']\"><font color=\"red\">Delete</font></a></td>";
        }
    echo "</table>";
?>

It connects to the database in another file, returns all of the results successfully etc. However, the problems come when I try adding the 'Approve' and 'Delete' links. I want the approve link to go to approve_user.php?user_id=$row['user_id'] and I want the delete link to go to deny_user.php?user_id=$row['user_id'].

I'm getting the error:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\liste\approve.php on line 22

Line 22 is the echo in the while loop. I know I've messed up big time on that line somewhere, but for the life of me I can't figure out where. Help is appreciated. Thanks!

A quick fix is to concatenate your variables:

echo "<tr><td align=center>" . htmlspecialchars($row['username']) . "</td><td> | </td><td align=center>" . htmlspecialchars($row['email']) . " </td><td> | </td><td><a href=\"approve_user.php?phpuser_id=" . $row['user_id'] . "\"><font color=\"green\">Approve</font></a> | </td><td><a href=\"deny_user.php?user_id=" . $row['user_id'] . "\"><font color=\"red\">Delete</font></a></td>";

However, the code could benefit from making it more readable or using a template system, or at least consider separating the logic and presentation.


Side note: The mysql_* library is deprecated, it is recommended to upgrade to PDO or MySQLi.

You must put {} over array variables in PHP strings:

$text = "Variable = {$_GET["user_input"]}
Simple variable = $var";

Means your code should look like (I added newlines to make code better readable):

echo "<tr><td align=center>" . htmlspecialchars($row['username']) . "</td><td> | </td>
";
echo "<td align=center>" . htmlspecialchars($row['email']) . " </td><td> | </td><td><a href=\"approve_user.php?phpuser_id={$row['user_id']}\"><font color=\"green\">Approve</font></a> | </td>
";
echo "<td><a href=\"deny_user.php?user_id={$row['user_id']}\"><font color=\"red\">Delete</font></a></td></tr>

";

You also had missing </tr>. I fixed that above.

When you're interpolating array elements into a string, there are two ways to do it:

$string = "blah blah $array[key] blah blah";

or:

$string = "blah blah {$array['key']} blah blah";

If you don't surround the array variable with {}, you must leave out the quotes around the key. The key is automatically assumed to be a literal string.

Note: This assumption is only done in string interpolation. You can't write:

$foo = $array[key];

In this case, key is interpreted as the name of a constant, not a literal string.

The second format is more general, because you can put other expressions in the key. It can also be used with object references.

Other ways to do it are with concatenation:

$string = "blah blah " . $array['key'] . " blah blah";

or sprintf():

$string = sprintf("blah blah %s blah blah", $array['key']);

try it like this :

 <?php
  $result = mysql_query("SELECT `user_id`, `username`, `email` FROM `users` WHERE `approved` =  '0' ORDER BY `username`;");

  echo "<table>";
  while($row = mysql_fetch_array($result))
  {
        $username=$row['username'];
        $email = $row['email'];
        $userID = $row['user_id'];
        echo "<tr><td align=center>" . htmlspecialchars($username) . "</td><td> | </td><td align=center>" . htmlspecialchars($email) . " </td><td> | </td><td><a href=\"approve_user.php?phpuser_id=$userID\"><font color=\"green\">Approve</font></a> | </td><td><a href=\"deny_user.php?user_id=$userID\"><font color=\"red\">Delete</font></a></td>";
  }
  echo "</table>";
  ?>