Jump to content

While loop is not doing its job


St2099

Recommended Posts

I'm sorry about the lack of information in the topic subject. I'll try to explain here.

 

$var1= mysql_query("SELECT * FROM table1"); 
while ($var2= @mysql_fetch_array($var1)) {
$id = $var2['transid'];

mysql_query("INSERT INTO table 2(name, city)
VALUES ('John', 'London')") or die (error25);
mysql_query("DELETE FROM table1 WHERE id = '$id' ") or die(mysql_error());
}

 

As you can see, the loop is running. If there are multiple inputs in the database(table1) with the same id, I want the loop to just run one of them, and delete the rest. But I can't get it to do this. What am I doing wrong? The result is that I get multiple Johns(example) inserted in table2.

 

 

Link to comment
Share on other sites

A breakdown of your script, as read:

 

run a query, and return everything in the table.

While parsing through the results, assign variable $id to equal column "transid".

Insert into the table named `table_2` at columns name, city the values "john" and "london", respectively.

Delete all occurneces from `table_1` where the `id`=$id, even though it's true name is `transid`.

 

Ok, I think you see the issue here:

<?php
$var1= mysql_query("SELECT `transid` FROM table1");
while ($var2= @mysql_fetch_assoc($var1)) {
$id = $var2['transid'];
mysql_query("INSERT INTO table 2(name, city) VALUES ('John', 'London')") or die (error25);
mysql_query("DELETE FROM table1 WHERE `transid` = '$id' ") or die(mysql_error());
}
?>

Link to comment
Share on other sites

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.