Jump to content

Recommended Posts

Im trying to add a column which will show a delete link on the end, the delete link will delete the row.

 

Heres my code so far, I can't get the delete link to work/position right.

 

stealth = my table

 

Heres my delete.php:

 

<?php
mysql_connect("localhost", "removed", "removed") or die(mysql_error());
mysql_select_db("something_removed") or die(mysql_error());
$data = mysql_query("SELECT * FROM stealth")
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Url:</th><td>".$info['folder_name'] . " </td>";
Print "<th>Link:</th> <td>".$info['url'] . " </td>";
Print "<th>Clicks:</th> <td>".$info['visits'] . "</td></tr>";
}
Print "</table>";


if(!isset($cmd)) 
{

   $result = mysql_query("select * from stealth"); 
   

   while($r=mysql_fetch_array($result)) 
   { 

      $url=$r["url"];
      $id=$r["id"];
     
      echo "<a href='delete.php?cmd=delete&id=$id'>$url - Delete</a>";
      echo "<br>";
    }
}
?>

<?
if($cmd=="delete")
{
    $sql = "DELETE FROM news WHERE id=$id";
    $result = mysql_query($sql);
    echo "Row deleted!";
}
?>

 

 

This is how I'd like it to look:

 

13zwiz7.jpg

 

Can anyone please help?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/159962-solved-help-please-sql-row/
Share on other sites

That's how it does look like.

 

I think you meant this (changed the while to for loop for testing purposes):

 

Print "</pre>
<table border cellpadding="3">";
Print "Url:Link:Clicks:";
for($x=0; $x{
   Print "";
   Print "".$x. " ";
   Print "".$x. " ";
   Print "".$x. "";
}
Print "</ta

That's how it does look like.

 

No it dont, it shows the results but the delete column aint displayed at the end (the image I showed was done in paint to show you)

 

 

Your code didnt work, heres the total code with your added code;

 

<?php
mysql_connect("localhost", "REMOVED", "REMOVED") or die(mysql_error());
mysql_select_db("REMOVED") or die(mysql_error());
$data = mysql_query("SELECT * FROM stealth")
or die(mysql_error());
Print "<table border cellpadding=3>";
Print "<th>Url:</th><th>Link:</th><th>Clicks:</th>";
for($url=$r["url"]; $id=$r["id"]; $x++)
{
   Print "<tr>";
   Print "<td>".$info['folder_name'] . " </td>";
   Print "<td>".$info['url'] .  " </td>";
   Print "<td>".$info['visits'] . "</td></tr>";
}
Print "</table>";


if(!isset($cmd)) 
{

   $result = mysql_query("select * from stealth"); 
   

   while($r=mysql_fetch_array($result)) 
   { 

      $url=$r["url"];
      $id=$r["id"];
     
      echo "<a href='delete.php?cmd=delete&id=$id'>$url - Delete</a>";
      echo "<br>";
    }
}
?>

<?
if($cmd=="delete")
{
    $sql = "DELETE FROM news WHERE id=$id";
    $result = mysql_query($sql);
    echo "Row deleted!";
}
?>

 

 

Heres how it looks now:  :o

 

2jg04fp.jpg

 

Yeah my bad, try this:

 

mysql_connect("localhost", "removed", "removed") or die(mysql_error());
mysql_select_db("something_removed") or die(mysql_error());
$data = mysql_query("SELECT * FROM stealth") or die(mysql_error());
Print "</pre>
<table border="'1'" cellpadding="'3'">";
Print "Url:Link:Clicks:";
while($info = mysql_fetch_array( $data ))
{
   Print "";
   Print "".$info['folder_name'] . " ";
   Print "".$info['url'] . " ";
   Print "".$info['visits'] . "";
   if(!isset($cmd))
   {
      echo "{$row['url']} - Delete";
   }
   Print "";
}
Print "</table>";<br

The table and delete is positioned right but the delete link dont function no more. (the delete link is meant to delete the selected row from the mysql database)

 

All of the delete links (dont function) they are linked like:

 

http://www.mysite.com/delete.php?cmd=delete&id=%3E%20-%20Delete%3C/a%3E%3C/td%3E%3C/tr%3E%3Ctr%3E%3Ctd%3EgF6ZXU12NJoj3NHQz%20%3C/td%3E%3Ctd%3Ehttp://www.something.com%20%3C/td%3E%3Ctd%3E2%3C/td%3E%3Ctd%3E%3Ca%20href=

 

Heres how its looking now: (the appearance has progressed but the delete links dont work)

 

2cfv7lf.jpg

Its looking better, but the delete link aint functioning it returns with

 

"Not Found

The requested URL /delete.php was not found on this server.

 

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. "

 

The delete links look like (but dont have ids):

http://www.mysite.com/delete.php?cmd=delete&id=

 

This is sort of the way I'd like to have the rows deleted (but can't seem to figure how to integrate the code):

http://www.spoono.com/php/tutorials/delete/delete.txt

 

The delete links look like (but dont have ids):

 

My mistake again.  Sorry don't have a testing environment right now.  I was using $row rather than $info for retrieving the id.  I also added in the delete code at the top of your script.

 

Try this code:

 

if($_GET['cmd'] == 'delete')
{
    $id = mysql_real_escape_string($_GET['id']);
    $sql = "DELETE FROM news WHERE id = $id";
    $result = mysql_query($sql) or die(mysql_error());
    if($result)
{   
   echo "Row deleted!";
}
else
{
   echo "There was an error deleting.";
}
}
   

mysql_connect("localhost", "removed", "removed") or die(mysql_error());
mysql_select_db("something_removed") or die(mysql_error());
$data = mysql_query("SELECT * FROM stealth") or die(mysql_error());
Print "</pre>
<table border="'1'" cellpadding="'3'">";
Print "Url:Link:Clicks:";
while($info = mysql_fetch_array( $data ))
{
   Print "";
   Print "".$info['folder_name'] . " ";
   Print "".$info['url'] . " ";
   Print "".$info['visits'] . "";
   if(!isset($cmd))
   {
      echo "Delete";
   }
   Print "";
}
Print "</table>";<br

 

 

Everything is displayed and configurated fine, except when I click a delete link it displays with errors saying:

 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home/crypt/public_html/delete.php on line 5

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/crypt/public_html/delete.php on line 5

Warning: mysql_query() [function.mysql-query]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home/crypt/public_html/delete.php on line 7

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/crypt/public_html/delete.php on line 7
Access denied for user 'nobody'@'localhost' (using password: NO)

<?php

mysql_connect("localhost", "removed", "removed") or die(mysql_error());
mysql_select_db("something_removed") or die(mysql_error());

if($_GET['cmd'] == 'delete')
{
    $id = mysql_real_escape_string($_GET['id']);
    $sql = "DELETE FROM news WHERE id = $id";
    $result = mysql_query($sql) or die(mysql_error());
    if($result)
{   
   echo "Row deleted!";
}
else
{
   echo "There was an error deleting.";
}
}
   

$data = mysql_query("SELECT * FROM stealth") or die(mysql_error());
Print "<table border='1' cellpadding='3'>";
Print "<th>Url:</th><th>Link:</th><th>Clicks:</th>";
while($info = mysql_fetch_array( $data ))
{
   Print "<tr>";
   Print "<td>".$info['folder_name'] . " </td>";
   Print "<td>".$info['url'] . " </td>";
   Print "<td>".$info['visits'] . "</td>";
   if(!isset($cmd))
   {
      echo "<td><a href='delete.php?cmd=delete&id={$info['id']}'>Delete</a></td>";
   }
   Print "</tr>";
}
Print "</table>";
?>

 

Try that, the mysql connection should've come before the deletion process.

 

Whats with the $cmd? I don't see it ever being used (refering to the if(!isset($cmd)) statement) unless thats initialized on another page to determine admin?

Whats with the $cmd? I don't see it ever being used (refering to the if(!isset($cmd)) statement) unless thats initialized on another page to determine admin?

 

Right here:

 

echo "

 

When someone clicks on the delete link it's passed with the record id to be deleted.

It doesn't make sence for it to pass $cmd(through URL's) and $_GET['cmd'] unless PHP makes an automatic assumption that if $cmd isn't assigned it attempts to use a $_GET or a $_POST?

 

Huh?  When you pass vars via HTTP is assumes GET.

 

Try this:

 

if($_GET['cmd'] == 'delete')
{
   echo "Deleted: " . $_GET['id'];
}

for($i=0; $i{ ?>



?cmd=delete&id='>Record #

Try that, the mysql connection should've come before the deletion process.

 

Move these two lines right before the if statement:

 

mysql_connect("localhost", "removed", "removed") or die(mysql_error());
mysql_select_db("something_removed") or die(mysql_error());

 

 

I added the mysql config above the if, and the delete links show:

 

"Table 'crypt_crypt.news' doesn't exist"

 

 

<?php
if($_GET['cmd'] == 'delete') # If i change the $_GET['cmd'] to $cmd this won't work... at least it doesn't work on my xampp installation.
{
   echo "Deleted: " . $_GET['id'];
}

for($i=0; $i<10; $i++)
{ ?>

<br/>
<a href='<?php echo $_SERVER['PHP_SELF'];?>?cmd=delete&id=<?php echo $i; ?>'>Record #<?php echo $i; ?></a>

<?php } ?>

 

^^ Comments.

 

What i'm refering to is their checking if $cmd is not set and then echoing out the delete link.

 

$cmd is never initialized anywhere, nowhere does it say $cmd = something-here meaning the link would never been shown as long as something is contained in $cmd.

 

With your example i was getting the Deleted: # message but as soon as i made the change thats in my comments it stops working. In the end, the (!isset($cmd) is a waste unless it's being use to determine some type of administration level to stop annonymous people from using it.

Its all fixed, thanks alot Maq!!! and also thanks for your input iarq

 

;D

 

Great, did you have the wrong database name or something?

 

NOTE - Please click the tab in the bottom left, "topic solved".

 

Yup, iarq's code had a different table name, it said "news" and I replaced it with my table name and it worked.

 

<?php

mysql_connect("localhost", "removed", "removed") or die(mysql_error());
mysql_select_db("something_removed") or die(mysql_error());

if($_GET['cmd'] == 'delete')
{
    $id = mysql_real_escape_string($_GET['id']);
    $sql = "DELETE FROM [b]news[/b] WHERE id = $id";
    $result = mysql_query($sql) or die(mysql_error());
    if($result)
   {   
      echo "Row deleted!";
   }
   else
   {
      echo "There was an error deleting.";
   }
}
   

$data = mysql_query("SELECT * FROM stealth") or die(mysql_error());
Print "<table border='1' cellpadding='3'>";
Print "<th>Url:</th><th>Link:</th><th>Clicks:</th>";
while($info = mysql_fetch_array( $data ))
{
   Print "<tr>";
   Print "<td>".$info['folder_name'] . " </td>";
   Print "<td>".$info['url'] . " </td>";
   Print "<td>".$info['visits'] . "</td>";
   if(!isset($cmd))
   {
      echo "<td><a href='delete.php?cmd=delete&id={$info['id']}'>Delete</a></td>";
   }
   Print "</tr>";
}
Print "</table>";
?>

 

 

SOLVED! yay! Thanks Maq!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.