0

I am trying to load data from MySQL and then display the results. I would like be able to delete the associated record as well.

<?php
include 'conn.php';

$sql = "SELECT * FROM Reviews LIMIT 3";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<h1> " . $row["rTitle"]. " </h1>" . $row["rText"]. "<br> <a href="delete.php?id={$row['id']}">Delete</a>";
}
} else {
echo "0 results";
}

$id = $_GET['rID'];
mysql_query("DELETE FROM Reviews WHERE id = {$id}");

$conn->close();

?>

I have a problem with this line:

echo "<h1> " . $row["rTitle"]. " </h1>" . $row["rText"]. "<br> <a href="delete.php?id={$row['id']}">Delete</a>";

Error: syntax error, unexpected 'delete' (T_STRING), expecting ',' or ';

What am I doing wrong? Thank you.

  • Either change the double quote in `href=""` to single quote, or use single quote as your string wrapper – Carl Binalla May 30 '19 at 08:55
  • You need to escape the double quotes within your string. – Nick May 30 '19 at 09:02
  • The mysql_* interface that you've used for the deleting was removed from version 7 of PHP, you should now be using either the mysqli_* interface (MySQL Improved extension) or PDO. ***ALWAYS*** use prepared statements when plugging a variable into a query, no matter what the source of the data. Also instead of using the SELECT * just select only the fields that you're actually going to use – SpacePhoenix May 30 '19 at 09:11
  • `echo "

    " . $row["rTitle"]. "

    " . $row["rText"]. "
    Delete";` **OR** `echo "

    {$row['rTitle']}

    {$row['rText']}
    Delete";`
    – danish-khan-I May 30 '19 at 09:21

2 Answers2

0
echo '<h1>'.$row["rTitle"].'</h1>'.$row["rText"].'<br><a href="delete.php?id='.$row['id'].'">Delete</a>';
stefo91
  • 618
  • 6
  • 16
  • Thanks. This results in: syntax error, unexpected ''delete.php?id=".$row['' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';' –  May 30 '19 at 08:53
  • I made changes try now and accept my answer if works. – stefo91 May 30 '19 at 08:56
0
echo '<h1>'.$row["rTitle"].'</h1>'.$row["rText"].'<br> <a href="/delete.php?id='.$row['id'].'">Delete</a>';

This will work and always try to use single ' while framing html code bcz ' is faster than ".

Rasa Mohamed
  • 882
  • 1
  • 6
  • 14