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
https://forums.phpfreaks.com/topic/204949-while-loop-is-not-doing-its-job/
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());
}
?>

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.