user2596886
user2596886

Reputation: 45

delete row from database - syntax error

I found this code that does exactly what i need

http://www.daveismyname.com/tutorials/php-tutorials/delete-rows-from-a-mysql-database-with-a-confirmation/

When i copy the code into a new dreamweaver php file, it states that there is a syntax error in this part:

<?php
$result = mysql_query("SELECT * FROM news")or die(mysql_error());
while($row = mysql_fetch_object($result))
{
echo "<ul>n";
echo "<li>$row->newsTitle <a href="javascript:delnews('$row->newsID','$row->newsTitle')">Delete</a></li>n";
echo "</ul>n";
}
?>

more specifically in the echo <li>$row line.

if I try to upload it anyways it says:

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/asbj1076/public_html/testdel/admin.php on line 39

i can save it as a html5 file without that syntax error, but the code still aint working properly.

Any suggestions?

I know there has been a lot of similar questions already, sorry for that. I'm just a noob who's in over my head, so i need really specific help.

Thanks. Asbjørn

Upvotes: 1

Views: 114

Answers (2)

The Alpha
The Alpha

Reputation: 146191

You may try (keep the ul out of loop)

echo "<ul>";
while($row = mysql_fetch_object($result))
{
    echo "<li>" . $row->newsTitle . " <a href='javascript:delnews(\'" . $row->newsID ."\', \'" . $row->newsTitle . "\')>Delete</a></li>";
}
echo "</ul>";

Output will be :

  • Hello Delete

Upvotes: 1

Bad Wolf
Bad Wolf

Reputation: 8349

Because of the double quotes " in your second echo statement PHP is throwing an error because it is trying to evaluate the HTML that is inside of those quotes.

echo "<li>$row->newsTitle <a href="javascript:delnews('$row->newsID','$row->newsTitle')">Delete</a></li>n";
                                   ^                                                  ^

Everything between those marks is trying to be evaulated as php and errors. Escape your quotes like this instead.

echo "<li>$row->newsTitle <a href=\"javascript:delnews('$row->newsID','$row->newsTitle')\">Delete</a></li>n";

Upvotes: 2

Related Questions