First, you don't need two database handles - you can almost certainly just use two statement handles on the same database handle. Second, don't put quotes around '$new_id' -- quotes are for delimiting strings in SQL, not for numbers. Third, don't do the prepare inside the loop, the whole point is prepare only needs to be done once. Fourth, you don't need ||die $sth2->errstr, you have RaiseError on. Fifth, what are you expecting to happen with $sql_data and $pointer, you never use them? You better describe what you want to happen. But thanks for the code :-).