Multiple INSERTS

Hi there.

I’m having a strange problem.

I have two tables… let’s suppose Invoice_Headers and Invoice_Lines. At some point, user want to copy a invoice. First, I will create a new header and than I must copy the lines from the original Invoice to the new.

I use something like this:

$sql = "SELECT fields FROM Invoice_Lines WHERE invoice_header=".$original;
$result=mysql_query($sql, $con);
$nlines = mysql_num_rows($result);
for ($i=0; $i < $nlinhas; $i++) {
mysql_data_seek($result, $i);
$row = mysql_fetch_assoc($result);
$sql2 = "INSERT INTO Invoice_Lines VALUES(".$row["field"]."ETC...";
$result=mysql_query($sql2, $con);

This code doen’t work with PHP, but if I copy it and run it in phpMyAdmin, works perfectly.

Any ideas?

Thanks in advance.



There are two records to copy… this code inserts only one… can’t figure it out…

Try putting error_reporting(E_ALL) at the top of the page. I was reading up on mysql_data_seek() and it says that if mysql_num_rows() == 0 that it could fail. This should show you if it is or not.

Hi there.

Thanks for the reply…

I changed the code… I’ using:

while($row = mysql_fetch_array($result)) {

The only error I get is

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

… but, as I sad earlier, I run queries in MySQL and it runs good…

I also tried error_reporting(E_ALL)… and it didn’t helped…

This is really weird…

I tried another thing…

I changed records so that the 2 lines to be copied have exactly the same data, exception to PK.

Again, works only for the 1rst record… second one returns error… :-?

while($row = mysql_fetch_array($result)) {
$sql2 ="INSERT..";
$result=mysql_query($sql2, $con);// I WAS USING SAME VAR $result AS ABOVE

Glad to hear you got it figured out!

Sponsor our Newsletter | Privacy Policy | Terms of Service