Jump to content

Looping insert - then update


AikenDrum

Recommended Posts

Hi there,

 

I have two table - Products and LineItems. I insert 3 values into LineItems - the quoteID, productID and Quantity - I then need to have the Cost field and imageName field in the LineItems table populated from the Products table where the productID in the newly created row in LineItems matches the productID form the Product table.

 

Here is my current code:

 

$i = 0;
$countLI = count($postID);

while ($i < $countLI) {

$qryLi = "INSERT INTO qLi(qid, prodID, Qty) VALUES('$qid', '$postID[$i]', '$postQty[$i]')";

$resultLi = @mysql_query($qryLi);
        if( ! $resultLi ) {
            die( "Could not execute query '$qryLi'. (Reason: " . mysql_error() . "). " );
    }
  	$i++;
}

 

id someone could help me here, I would be so thankful - it's 5:20 in the morning here, been up all night and only now realized what I need.

Link to comment
https://forums.phpfreaks.com/topic/192347-looping-insert-then-update/
Share on other sites

Actually - I used the following and it works - if anyone can see what may be wrong with it - please let me know

 

$i = 0;
$countLI = count($postID);

while ($i < $countLI) {

$qryLi = "INSERT INTO qLi(qid, prodID, Qty) VALUES('$qid', '$postID[$i]','$postQty[$i]') ";
$resultLi = @mysql_query($qryLi);
        if( ! $resultLi ) {
            die( "Could not execute query '$qryLi'. (Reason: " . mysql_error() . "). " );
    }
    
    
$qryLi2 = "Update qLi p, onlineprods pp SET p.Cost = pp.Cost WHERE p.prodID = pp.id";
$resultLi2 = @mysql_query($qryLi2);
        if( ! $resultLi ) {
            die( "Could not execute query '$qryLi'. (Reason: " . mysql_error() . "). " );
    }

echo "<br>".$qryLi;

  	$i++;
}

Hi again,

 

I updated it and this does what I want :

 

$i = 0;
$countLI = count($postID);

while ($i < $countLI) {

$qryLi = "INSERT INTO qLi(qid, prodID, Qty) VALUES('$qid', '$postID[$i]','$postQty[$i]') ";
$resultLi = @mysql_query($qryLi);
        if( ! $resultLi ) {
            die( "Could not execute query '$qryLi'. (Reason: " . mysql_error() . "). " );
    }
    
   $liid     = mysql_insert_id();
    
//    echo $liid;
    
$qryLi2 = "Update qLi p, onlineprods pp SET p.Cost = pp.Cost, p.Product = pp.Product, p.ImgName = pp.ImgName WHERE p.prodID = pp.id AND p.id = '$liid'";

//	echo $qryLi2;

$resultLi2 = @mysql_query($qryLi2);
        if( ! $resultLi ) {
            die( "Could not execute query '$qryLi'. (Reason: " . mysql_error() . "). " );
    }

echo "<br>".$qryLi;

  	$i++;
}

 

quite chuffed actually that it works - probably not optimal but it works

 

Cheers

 

AikenD

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.