0

i am inserting data into php script is done .but i have not getting how to update mulitiple rows using php script.

my code format is:

  if(is_array($sheetData)){

        //$sql = "INSERT INTO 13_product_id (product_id,ext_product_id) values ";

        $valuesArr = array();
        $flag=0;
        foreach($sheetData as $row){

            if($flag==0 || $row['E']== 'Not Found on build.com'){

                $flag =1;

                continue;

            }
            //print_r($row);
            $product_id =$row['A'];
            $ext_product_id=$row['D'];


           // $valuesArr[] = "('$product_id','$ext_product_id')";

        }
         $sql = "UPDATE 13_product_id SET ext_product_id='$ext_product_id[$row]' WHERE product_id='$product_id[$row]'";
    //$sql .= implode(',', $valuesArr);
        print_r($sql);
       //mysqli_query($con,$sql) or exit(mysqli_connect_errno()); 
    }

    mysqli_close($con);
    ?>
1
  • 1
    If you're already using MySQLi I strongly suggest looking at how to use parametised queries, rather than embedding your values directly into the SQL, it's faster for multiple executions(like you want to do) and it will protected against SQL injection attacks. Commented Jun 12, 2014 at 15:29

2 Answers 2

2
if(is_array($sheetData)){

    //$sql = "INSERT INTO 13_product_id (ext_product_id,product_id) values (?, ?)";
    $sql = "UPDATE 13_product_id SET ext_product_id= ? WHERE product_id= ?";
    $stmt = mysqli_prepare($con, $sql);
    mysqli_stmt_bind_param($stmt, "ss", $ext_product_id, $product_id);

    $flag=0;
    foreach($sheetData as $row){

        if($flag==0 || $row['E']== 'Not Found on build.com'){
            $flag =1;
            continue;
        }

        $product_id = $row['A'];
        $ext_product_id = $row['D'];
        mysqli_stmt_execute($stmt);

    }
}

mysqli_close($con);
?>
Sign up to request clarification or add additional context in comments.

Comments

0

You need to include your query execution in your loop. Not after it:

foreach($sheetData as $row){
    if($flag==0 || $row['E']== 'Not Found on build.com'){
        $flag =1;
        continue;
    }
    $product_id =$row['A'];
    $ext_product_id=$row['D'];

    $sql = "UPDATE 13_product_id SET ext_product_id='$ext_product_id[$row]' WHERE product_id='$product_id[$row]'";
    mysqli_query($con,$sql) or exit(mysqli_errno()); 
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.