Hi,
There is a record (id) selected from TABLE1 where 'code' is for
example '1'.
The id is used to delete a record (with that id) from TABLE2(!).
After this the record with that id from TABLE1 is inserted (again)
into TABLE2.
Using update isn't an option because id can't be a primairy key. (two
records can have the same id)
The problem occures while browsing through TABLE1 there are for
example 5 records with the 'code' = '1'. When executing the script the
first result (id=1) in TABLE 1 will be inserted 5 times in TABLE2 at
the end. The second result (id=2) will be inserted 4 times, the third
(id=3) will be inserted 3 times, the fourth (id=4) 2 times, and the
last (id=5) 1 time.
According to this it's easy to conclude it's looping until he cant
find any records with a 'code' = '1' (that's the last record (id=5)
Does some body know a solution for this problem? I need only to
(re)insert the records once 🙂!
Thanks a lot in @ for your help!
Tim.
The code:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<? php
if ($submit) {
$db = @mysql_connect("localhost","db","password") or
Die(mysql_error());
@mysql_select_db("db",$db) or Die("Error: " . mysql_error());
echo "Connection made, counting records <BR>";
// record count
$sqlrows = ("SELECT Count(*) FROM table1 WHERE Code = '1' OR Code2 =
'1'");
$resultrows = @($sqlrows,$db);
$rows = mysql_fetch_row($resultrows);
echo "The are ".$rows[0]."</b> records total<br><br>";
// selecting from table1
$sql = ("SELECT Id FROM table1 WHERE Code = '1' OR Code2 = '1'");
$result = @($sql,$db) or Die(mysql_error());
while(list($Id) = @mysql_fetch_row($result)) {
print "Record with Id = $Id selected from table1 <BR>";
// deleted from table2
$sql2 = ("DELETE FROM table2 WHERE Id=".$Id."");
@($sql2,$db) or Die(mysql_error());
print "Record with Id = $Id deleted from tabel2 <BR>";
// insert again in table2
$sql3 = ("INSERT INTO table2 (field1, field2, field3 ) SELECT field1,
field2, field3 FROM table1 WHERE Code = '1' OR Code2 = '1'");
@($sql3,$db) or Die(mysql_error());
mysql_close($db);
print "Record with Id = $Id inserted again in table2 <BR><BR>";
}
?>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++