Jump to content

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