Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,235 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,238 people online right now. Registration is fast and FREE... Join Now!




Deleting rows using PHP/MySQL

 
Reply to this topicStart new topic

Deleting rows using PHP/MySQL, Removing selective rows (Rather than the bottom towards the top)

livelonger87
12 Oct, 2008 - 02:11 PM
Post #1

New D.I.C Head
*

Joined: 27 May, 2008
Posts: 30


My Contributions
I've created a script for removing threads and posts from a PHP app. However, I cannot seem to delete rows of which are selected (I.e. thread name here/post ID), as it requires for me to delete one of which is at the bottom of the row (I.e. It'll only remove post ID 1 rather than allowing myself to remove post ID 5), as I can only delete POST ID 5 if I remove all posts prior to post 5.

Here's an example of how I would remove a table entity...

CODE

$user_post = $_POST['remove_post'];
$sql = mysql_query("SELECT * FROM thread_replies");
if(!$sql) {
print "Error". mysql_error(). "<br>";
} else {
$check_array = mysql_fetch_array($sql);
if(!$check_array) {
print "Error". mysql_error(). "<br>";
} else {
$post = $check_array['ID'];
if($user_post != $post) {
print "Post does not exist";
} else {
$remove_post = mysql_query("DELETE FROM thread_replies WHERE ID='$user_post'");
if(!$remove_post) {
print "Error". mysql_error(). "<br>";
} else {
print "Post $user_post has been removed";
}
}//bla bla parenthasis confusion (Writting in this text box)

Help is much apperciated, as you guy's are awesome. smile.gif

This post has been edited by livelonger87: 12 Oct, 2008 - 02:12 PM
User is offlineProfile CardPM
+Quote Post

spearfish
RE: Deleting Rows Using PHP/MySQL
12 Oct, 2008 - 03:38 PM
Post #2

Monkey in Training
Group Icon

Joined: 10 Mar, 2008
Posts: 746



Thanked: 2 times
Dream Kudos: 225
My Contributions
1) You are vulnerable to SQL injection! Run mysql_real_escape_string on any input before you use it in an SQL query.

2) You can use something like $sql = mysql_query(...) or die(mysql_error()); as a less confusing hierarchy. It will stop code execution if the query fails (this can also be used on mysql_fetch_array, and even for checking if the post exists).

3) Add a where clause to the end of your select statement. Without any restrictions, that result will be huge and consequentially will use a lot of memory.

4) Add a LIMIT 1 to the end of both of the queries. That way after it finds one match it stops looking for more.

5) What happens when you run this script? What posts are deleted? How many?
User is offlineProfile CardPM
+Quote Post

livelonger87
RE: Deleting Rows Using PHP/MySQL
12 Oct, 2008 - 04:20 PM
Post #3

New D.I.C Head
*

Joined: 27 May, 2008
Posts: 30


My Contributions
QUOTE(spearfish @ 12 Oct, 2008 - 04:38 PM) *

1) You are vulnerable to SQL injection! Run mysql_real_escape_string on any input before you use it in an SQL query.

2) You can use something like $sql = mysql_query(...) or die(mysql_error()); as a less confusing hierarchy. It will stop code execution if the query fails (this can also be used on mysql_fetch_array, and even for checking if the post exists).

3) Add a where clause to the end of your select statement. Without any restrictions, that result will be huge and consequentially will use a lot of memory.

4) Add a LIMIT 1 to the end of both of the queries. That way after it finds one match it stops looking for more.

5) What happens when you run this script? What posts are deleted? How many?

Hey! Thanks for the response.
1)I created that as a quick example.
3)Add a where clause without anything attached? (I.e. WHERE username='$username')
4)How do I use LIMIT 1? Do I add it to the end of the DELETE or SELECT query?
User is offlineProfile CardPM
+Quote Post

spearfish
RE: Deleting Rows Using PHP/MySQL
12 Oct, 2008 - 07:51 PM
Post #4

Monkey in Training
Group Icon

Joined: 10 Mar, 2008
Posts: 746



Thanked: 2 times
Dream Kudos: 225
My Contributions
3) Same type of a deal as with the DELETE query. So it'd look kind of like this:
php

$sql = mysql_query("SELECT * FROM thread_replies WHERE ID='{$user_post}'");


4) LIMIT is always (AFAIK) the very last thing in any query. Think of it this way: you have a table containing, say, 250,000 users and you need to load the user info for only one user. Even though you will be limiting the number of results returned to one via a WHERE clause, it's best to have both the limit and where clauses, because that way once SQL reaches one match (say, 50,000 rows down) it doesn't bother checking the other 200,000 rows.

So the query using a LIMIT might look something like this:

php

$query = "SELECT post_count FROM members WHERE id='{$id}' LIMIT 1";


5) This pertains to your original question, 1-4 were just things I noticed about your coding tongue.gif What's the deal with this?

If you have any questions feel free to ask!

This post has been edited by spearfish: 12 Oct, 2008 - 07:53 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 03:30AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month