Jump to content

HELP! deleting multiple rows HELP!


mikepuerto

Recommended Posts

Can anyone tell me why this code will only delete 1 row? there are about 2000 lines in the "$file" that i'm cross referencing to delete the rows... Please help! this is driving me nuts!

[code]
$file = $_GET['file'];

$lines = file($file);

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to the database');
mysql_select_db($dbname);

foreach ($lines as $line_num => $line) {

$query = "DELETE FROM largo1 WHERE id='$line'";
$result = mysql_query($query);
echo "$line deleted $line_num<br>";


}
mysql_close($conn);
[/code]
Link to comment
https://forums.phpfreaks.com/topic/22123-help-deleting-multiple-rows-help/
Share on other sites

Yes, the file exists... Just using a querystring to get it... The file looks like:

[code]
1068
1092
1093
1097
1098
1099
1147
1220
1326
1386
[/code]

and yes, $lines is correct... The script appears to run flawlessly, i get no errors! And to answer your question about my echo's out put, yes it looks perfect! This is what the output looks like:
[code]
1068 deleted 0
1092 deleted 1
1093 deleted 2
1097 deleted 3
1098 deleted 4
1099 deleted 5
1147 deleted 6
1220 deleted 7
1326 deleted 8
1386 deleted 9
[/code]

The first number is obviously the "id" that identifies the row i want to disappear.  At first i thought i actually removed a record... But it's actually not doing anything at all... Could this be some sort of mysql config parameter that only allows for a single DELETE to be used at once or something?
hmm. i assume that since you aren't getting your die message from your connect, that it's connecting.  add a die statement to your db select:

mysql_select_db($dbname) or die(mysql_error());

maybe the db is not being selected.  also make sure your table name is spelled correctly in your query string.  you might also want to try echoing $query and using one of the echoed strings directly into the database like through phpmyadmin or something.

also, your query is using $line which is your actual data; are you sure that's what the id is supposed to be, in your query? i think that's what you're shooting for, but i'm just making sure.  i saw that you originally had $line_num in your query and then you edited your post.
[url=http://www.php.net/file]file()[/url] does not remove the "newline" from the end of each line. If you look at the source of the output it probably looks similar to the following

[code]
1068
deleted 0 <br>
1092
deleted 1<br>
1093
deleted 2<br>
[/code]

You can use [url=http://www.php.net/trim]trim()[/url] or more specifically [url=http://www.php.net/rtrim]rtrim()[/url] to remove the newline
[code]
foreach ($lines as $line_num => $line) {
    $line = rtrim($line);
[/code]

You should also be able to use [url=http://php.net/array_map]array_map[/url] to remove the newlines from each line or perhaps use [url=http://www.php.net/file_get_contents]file_get_contents[/url] and explode instead

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.