Setting parameters at link

  • Inito
  • Graduate
  • Graduate
  • User avatar
  • Posts: 223

Post 3+ Months Ago

I'm busy with a news poster, and at the administrative part of it, at the part of deleting a message, i've come to the fact that i have to set a parameter on the link to have it work.

the code reflecting it:

Code: [ Select ]
//This part takes care of removing the message
if($action == "delete") {
$news_SQL_del = "DELETE FROM news WHERE news_ID=$news_ID";
$bool = mysql_query($news_SQL_del);
if($bool == 1) echo "<SCRIPT LANGUAGE=JavaScript>window.alert('The message has been removed')</SCRIPT>";
if($bool <> 1) echo "<SCRIPT LANGUAGE=JavaScript>window.alert('An error occured. The message was not removed.')</SCRIPT>";
}
...
<a href="#" action="delete">Remove</a>
  1. //This part takes care of removing the message
  2. if($action == "delete") {
  3. $news_SQL_del = "DELETE FROM news WHERE news_ID=$news_ID";
  4. $bool = mysql_query($news_SQL_del);
  5. if($bool == 1) echo "<SCRIPT LANGUAGE=JavaScript>window.alert('The message has been removed')</SCRIPT>";
  6. if($bool <> 1) echo "<SCRIPT LANGUAGE=JavaScript>window.alert('An error occured. The message was not removed.')</SCRIPT>";
  7. }
  8. ...
  9. <a href="#" action="delete">Remove</a>


To be honest I have no idea how to set a parameter for a link, this is what i tried.

If anyone needs a link, here you go:
http://members.lycos.nl/dave90/l1/news_list.php
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Posts: 4581
  • Loc: Maryland

Post 3+ Months Ago

I guess I don't understand what you mean by a parameter on a link...
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

I think me means appending information to an url like:

Code: [ Select ]
<a href="delete.php?action=delete&id=12">delete this article</a>


Basically, you use name/value pairs after the filename part of the url. The name value pairs are separated from the rest of the url by a ? and each pair is separated from the next by an &:

Code: [ Select ]
<a href="somepage.php?name1=value1&name2=value2">link text</a>
  • Inito
  • Graduate
  • Graduate
  • User avatar
  • Posts: 223

Post 3+ Months Ago

thanks, shouldve thought of it.
But now I keep on getting the error, cause it probably doesnt knows what message to delete.
the link should also send the id of the message (wich is under the primary key news_ID in the database) automatically.

How? is the big question.
Anyone got the big answer? (the answer should be simple, though)
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

Just echo it inside the link:

Code: [ Select ]
<a href="delete.php?action=delete&id=<?php echo $id ?>">delete this article</a>
  • Inito
  • Graduate
  • Graduate
  • User avatar
  • Posts: 223

Post 3+ Months Ago

no, its not really working

Code: [ Select ]
<a href="news_list.php?action=delete&news_ID=<?php echo $news_ID ?>">Remove</a>


is what i made of it.
What causes it to know what message it is in?
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

Well, you have to create the variable $news_ID by extracting the information from your database. I assumed you were already extracting the text of the messages inside a loop to print them on the page, so you would just add news_ID to your select, then extract it from the result of your query to create the variable. Once you have the variable set then you can just echo it.
  • Inito
  • Graduate
  • Graduate
  • User avatar
  • Posts: 223

Post 3+ Months Ago

Well, I got it, it should be <?php echo $news['news_ID'] ?>, as of table news, the news ID field.

When i scroll over the link it shows the correct ID of the message in the bottom bar of IE. But STILL it wont delete it.

Code: [ Select ]
//This part takes care of removing the message
if($action == "delete") {
$news_SQL_del = "DELETE FROM news WHERE news_ID=$news_ID";
$bool = mysql_query($news_SQL_del);
if($bool == 1) echo "<SCRIPT LANGUAGE=JavaScript>window.alert('The message has been removed')</SCRIPT>";
if($bool <> 1) echo "<SCRIPT LANGUAGE=JavaScript>window.alert('An error occured. The message was not removed.')</SCRIPT>";
}
  1. //This part takes care of removing the message
  2. if($action == "delete") {
  3. $news_SQL_del = "DELETE FROM news WHERE news_ID=$news_ID";
  4. $bool = mysql_query($news_SQL_del);
  5. if($bool == 1) echo "<SCRIPT LANGUAGE=JavaScript>window.alert('The message has been removed')</SCRIPT>";
  6. if($bool <> 1) echo "<SCRIPT LANGUAGE=JavaScript>window.alert('An error occured. The message was not removed.')</SCRIPT>";
  7. }


whats wrong with it?
the number is sent as the value of the $news_ID by clickin the link, and still it wont recognise it.

I'm out now till tomorrow, so thanks for your current support, and dont expect any further replies of me today :wink:

http://members.lycos.nl/dave90/l1/news_list.php

is the link to it.
Thanks.
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

I don't really see any error that would cause the delete query to fail. One thing you can do to help troubleshoot it is to change the query so that it will show any error string returned:

Code: [ Select ]
$bool = mysql_query($news_SQL_del) or die(mysql_error());


You could also add the query string to that to make sure it's going through ok:

Code: [ Select ]
$bool = mysql_query($news_SQL_del) or die(mysql_error() . "<br><br><b>$news_SQL_del</b>");


The way you're testing for the boolean variable looks a little odd to me. I would prefer testing like this:

Code: [ Select ]
if($bool == true) {  // or just if($bool)
  echo ".....";
}
else {
  echo ".....";
}
  1. if($bool == true) {  // or just if($bool)
  2.   echo ".....";
  3. }
  4. else {
  5.   echo ".....";
  6. }


However, I don't think that's necessarily wrong (I'm just not used to seeing it done that way), and it wouldn't have anything to do with the failure of the delete query.
  • Inito
  • Graduate
  • Graduate
  • User avatar
  • Posts: 223

Post 3+ Months Ago

When i add the "or die(mysql....)" and then click on remove, it doesnt displays any messages (none of the 2). When i go to that page again (news_list.php) theyre there again. So, theyre not really deleted, and they both disappear.

maybe cause bool has to have a false or true value, and that making the mysql_query AND the or die would be true/false in either way?
(if it wont work, mysql_query is false, and mysql_error true)

Post Information

  • Total Posts in this topic: 10 posts
  • Users browsing this forum: No registered users and 173 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.