newbtophp Posted May 28, 2009 Share Posted May 28, 2009 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: Can anyone please help? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/159962-solved-help-please-sql-row/ Share on other sites More sharing options...
Maq Posted May 28, 2009 Share Posted May 28, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/159962-solved-help-please-sql-row/#findComment-843747 Share on other sites More sharing options...
newbtophp Posted May 28, 2009 Author Share Posted May 28, 2009 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: Quote Link to comment https://forums.phpfreaks.com/topic/159962-solved-help-please-sql-row/#findComment-843759 Share on other sites More sharing options...
Maq Posted May 28, 2009 Share Posted May 28, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/159962-solved-help-please-sql-row/#findComment-843761 Share on other sites More sharing options...
newbtophp Posted May 28, 2009 Author Share Posted May 28, 2009 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) Quote Link to comment https://forums.phpfreaks.com/topic/159962-solved-help-please-sql-row/#findComment-843768 Share on other sites More sharing options...
Maq Posted May 28, 2009 Share Posted May 28, 2009 Yeah sorry here: echo "{$row['url']} - Delete"; Forgot a ' after ]}. You may also want to add another table header above the deletes: Print "UrlLinkClicksDelete"; Quote Link to comment https://forums.phpfreaks.com/topic/159962-solved-help-please-sql-row/#findComment-843774 Share on other sites More sharing options...
newbtophp Posted May 28, 2009 Author Share Posted May 28, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/159962-solved-help-please-sql-row/#findComment-843780 Share on other sites More sharing options...
Maq Posted May 28, 2009 Share Posted May 28, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/159962-solved-help-please-sql-row/#findComment-843788 Share on other sites More sharing options...
newbtophp Posted May 28, 2009 Author Share Posted May 28, 2009 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) Quote Link to comment https://forums.phpfreaks.com/topic/159962-solved-help-please-sql-row/#findComment-843800 Share on other sites More sharing options...
Maq Posted May 28, 2009 Share Posted May 28, 2009 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()); Quote Link to comment https://forums.phpfreaks.com/topic/159962-solved-help-please-sql-row/#findComment-843806 Share on other sites More sharing options...
iarp Posted May 28, 2009 Share Posted May 28, 2009 <?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? Quote Link to comment https://forums.phpfreaks.com/topic/159962-solved-help-please-sql-row/#findComment-843807 Share on other sites More sharing options...
Maq Posted May 28, 2009 Share Posted May 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/159962-solved-help-please-sql-row/#findComment-843811 Share on other sites More sharing options...
iarp Posted May 28, 2009 Share Posted May 28, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/159962-solved-help-please-sql-row/#findComment-843815 Share on other sites More sharing options...
Maq Posted May 28, 2009 Share Posted May 28, 2009 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 # Quote Link to comment https://forums.phpfreaks.com/topic/159962-solved-help-please-sql-row/#findComment-843818 Share on other sites More sharing options...
newbtophp Posted May 28, 2009 Author Share Posted May 28, 2009 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" Quote Link to comment https://forums.phpfreaks.com/topic/159962-solved-help-please-sql-row/#findComment-843822 Share on other sites More sharing options...
iarp Posted May 28, 2009 Share Posted May 28, 2009 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/159962-solved-help-please-sql-row/#findComment-843823 Share on other sites More sharing options...
newbtophp Posted May 28, 2009 Author Share Posted May 28, 2009 Its all fixed, thanks alot Maq!!! and also thanks for your input iarq Quote Link to comment https://forums.phpfreaks.com/topic/159962-solved-help-please-sql-row/#findComment-843824 Share on other sites More sharing options...
Maq Posted May 28, 2009 Share Posted May 28, 2009 Its all fixed, thanks alot Maq!!! and also thanks for your input iarq Great, did you have the wrong database name or something? NOTE - Please click the tab in the bottom left, "topic solved". Quote Link to comment https://forums.phpfreaks.com/topic/159962-solved-help-please-sql-row/#findComment-843825 Share on other sites More sharing options...
newbtophp Posted May 28, 2009 Author Share Posted May 28, 2009 Its all fixed, thanks alot Maq!!! and also thanks for your input iarq 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! Quote Link to comment https://forums.phpfreaks.com/topic/159962-solved-help-please-sql-row/#findComment-843827 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.