954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

can we use mysql(DELETE...) query in the same page while having anchor tags?

Hi all,

Instead of passing the variables to the other page and coding mysql queries there.. Is it possible that we can use the mysql(DELETE...) query within the anchor tags in the same page?

... to delete a particular row in mysql table?
Like having a link called delete and by clicking on that , a particular row must be deleted in the table.

eg:-
DELETE
(or)
eg:- delete
(or)

eg:-

<?php
 //db connections..

$result = mysql_query("SELECT commentid FROM comments") 
or die(mysql_error());  
 
while($row = mysql_fetch_array( $result )) {
?>
 	<a href="<?php //delete this row ?>">delete</a>
<?php } ?>




Instead of passing the variables to the other page and coding mysql queries there.. Is it possible that we can use the mysql(DELETE...) query in the same page?
I was wondering if there is some possibility like this in PHP .
Please let me know if there are any such methods .

Thankyou all in Advance.

Kavitha Butchi
Junior Poster in Training
69 posts since May 2008
Reputation Points: 10
Solved Threads: 4
 
<?php
 //db connections..

$result = mysql_query("SELECT commentid FROM comments") 
or die(mysql_error());  
 
while($row = mysql_fetch_array( $result )) {
?>
 	<a href="<?php //delete this row ?>">delete</a>
<?php } ?>

Instead of passing the variables to the other page and coding mysql queries there.. Is it possible that we can use the mysql(DELETE...) query in the same page? I was wondering if there is some possibility like this in PHP . Please let me know if there are any such methods .

Thankyou all in Advance.

sure,you can,get the id of that you want to delete then perform the delete query in the same page.for example:
(this is your page,e.g. sample.php)

<?php
 //db connections..

$result = mysql_query("SELECT commentid FROM comments") 
or die(mysql_error());  
 
while($row = mysql_fetch_array( $result )) {
?>
 	<a href="sample.php?delete_id=<?=$row['id']?>">delete</a>
<?php } ?>


put your delete statement above...
(sample.php)

//db connections..

if ($_GET['delete_id']!="")
{
//delete statement goes here....
}

$result = mysql_query("SELECT commentid FROM comments") 
or die(mysql_error());  
 
while($row = mysql_fetch_array( $result )) {
?>
 	<a href="sample.php?delete_id=<?=$row['id']?>">delete</a>
<?php } ?>
ryan_vietnow
Posting Pro
578 posts since Aug 2007
Reputation Points: 28
Solved Threads: 71
 

Thankyou for your time ryan_vietnow
Passing it to the next page is what we are doing here too ....other than this method..

cant we simply run the query in the same page where the anchor tag is present instead of passing it over to next page?

Kavitha Butchi
Junior Poster in Training
69 posts since May 2008
Reputation Points: 10
Solved Threads: 4
 

like

//...database connections...
<a href=mysql_query("DELETE FROM example WHERE age='15'")>delete</a>

something like that which works..where everything is coded in a single page rather than passing it over to the other page.

Kavitha Butchi
Junior Poster in Training
69 posts since May 2008
Reputation Points: 10
Solved Threads: 4
 

like

//...database connections...
<a href=mysql_query("DELETE FROM example WHERE age='15'")>delete</a>

something like that which works..where everything is coded in a single page rather than passing it over to the other page.


AFAIK, Its not possible.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Hello kavitha try this...

<?
/...database connections...
if(!empty($_GET['delid'])){  
	mysql_query("delete from category WHERE id='".$_GET['delid']."'");
	if(!empty($_GET['start']))
    {
	header("location:add_subcategory.php?msg=del&start=".$_GET['start']."");
	}else{
	header("location:samepage.php?msg=del");
	}
}
?>
//This is in your designing table...
 <td height="22" align="center" ><a href="javascript:del('<? echo $frow['id'];?>')">Delete</a> </td>
//and this can be anyware in your page

            <script language="javascript">
var d = document.frmmngmall;						
	function del(x)
	{
				
				if(confirm("You want to delete this record"))
					{
					d.action="samepage.php?delid="+x;
					d.submit();
					}
				
			return;
	}
        </script>

And your a href must be in form tag...

This is working finely...try this...

Thanks.
Shanti

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

Hello kavitha try this...

<?
/...database connections...
if(!empty($_GET['delid'])){  
	mysql_query("delete from category WHERE id='".$_GET['delid']."'");
	
	header("location:samepage.php?msg=del");
	
}
?>
//This is in your designing table...
 <td height="22" align="center" ><a href="javascript:del('<? echo $frow['id'];?>')">Delete</a> </td>
//and this can be anyware in your page

            <script language="javascript">
var d = document.frmmngmall;						
	function del(x)
	{
				
				if(confirm("You want to delete this record"))
					{
					d.action="samepage.php?delid="+x;
					d.submit();
					}
				
			return;
	}
        </script>
 

<? /...database connections... if(!empty($_GET['delid'])){ mysql_query("delete from category WHERE id='".$_GET['delid']."'"); if(!empty($_GET['start'])) { header("location:add_subcategory.php?msg=del&start=".$_GET['start'].""); }else{ header("location:samepage.php?msg=del"); } } ?> //This is in your designing table... <td height="22" align="center" ><a href="javascript<b></b>:del('<? echo $frow['id'];?>')">Delete</a> </td> //and this can be anyware in your page <script language="javascript"> var d = document.frmmngmall; function del(x) { if(confirm("You want to delete this record")) { d.action="samepage.php?delid="+x; d.submit(); } return; } </script>


And your a href must be in form tag...

This is working finely...try this...

Thanks.
Shanti

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

Thankyou shanthi chepuru.
I shall consider giving it a shot ...am little concerned as it involves Javascript.
I appreciate fr your time :)

Kavitha Butchi
Junior Poster in Training
69 posts since May 2008
Reputation Points: 10
Solved Threads: 4
 
AFAIK, Its not possible.

Hello nav33n,

thank you fr the response.

While googling fr this info I found out these broken strings..dont exactly if this is relevant or not.. thought I could ask it here...


You are not an employee of
Warning: mysql_connect() [function.mysql-connect]: Access denied for user ...

So I thought there might be something like this function.mysql-query....:icon_exclaim:

Please Let me know if nothing as such exists.


complete search result:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user ...
www.myrandomblogs.org/register.php - 25k - Cached - Similar pages - Note this

googled with search term as :

Kavitha Butchi
Junior Poster in Training
69 posts since May 2008
Reputation Points: 10
Solved Threads: 4
 

mysql_connect error is because the user gave wrong credentials to connect to the database.
Its not possible to call a php function or a mysql query with "onclick" event. As others have mentioned already, you can use ajax or pass the id of the record in the anchor tag and then do relevant operation.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

OK nav33n, :) thankyou for your time.
Appreciate it.

Kavitha Butchi
Junior Poster in Training
69 posts since May 2008
Reputation Points: 10
Solved Threads: 4
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You