1

I am trying to insert multiple values into the table row which are coming from the array. I almost got the problem solved with this answer. Best way to INSERT many values in mysqli?

please check the accepted answer. in this answer, he is adding the value in a single column but I want it in multiple columns. how can I do this?

  $array = array($u_id, $pid, $tradexx_price, $pvp, $product_title, $product_image);
  $query = "INSERT INTO selection (c_id,p_id,tradexx_price,pvp,product_name,img) VALUES (?,?,?,?,?,?)";
  $stmt = $con->prepare($query);
  $stmt->bind_param("ssssss", $one);

  $con->query("START TRANSACTION");
  foreach ($array as $one) {
      $stmt->execute();
  }
  $stmt->close();
  $con->query("COMMIT");

but this code giving me an error.

Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables in

Any answers appreciated.

full script code

    <?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include '../includes/conn.php';

if (isset($_POST['insert_selection'])) {
  $pid = implode(',', $_POST['id']);
  $u_id = $_POST['u_id'];

  foreach ((array)$pid as $p) {
    $get_pro = "SELECT * FROM products WHERE id IN ($p)";
    $run_pro = mysqli_query($con, $get_pro);

    while ($row_pro = mysqli_fetch_array($run_pro)) {
      $id = $row_pro['id'];
      $product_title = $row_pro['product_title'];
      $tradexx_price = $row_pro['product_price'];
      $pvp = $row_pro['pvp'];
      $product_image = $row_pro['product_image'];
      $array = array($u_id, $id, $tradexx_price, $pvp, $product_title, $product_image);
      $query = "INSERT INTO selection (c_id,p_id,tradexx_price,pvp,product_name,img) VALUES (?,?,?,?,?,?)";
      $stmt = $con->prepare($query);
      $stmt->bind_param("ssssss", $cur_c_id, $cur_id, $cur_tradexx_price, $cur_pvp, $cur_product_name, $cur_img);

      $con->query("START TRANSACTION");
      foreach ($u_id as $i => $cur_c_id) {
        $cur_id = $id[$i];
        $cur_tradexx_price = $tradexx_price[$i];
        $cur_pvp = $pvp[$i];
        $cur_product_name = $product_title[$i];
        $cur_img = $product_image[$i];

        $stmt->execute();
      }
      $stmt->close();
      $con->query("COMMIT");


      

    }
  }

}

$pid is value from checkbox

3
  • Count your fields vs your values. Commented Nov 17, 2022 at 20:19
  • @PM77-1 they are the same in the count. Commented Nov 17, 2022 at 20:20
  • You need 8 variables in the bind_param() call. Commented Nov 17, 2022 at 20:20

1 Answer 1

2

You don't need a loop. You can use a SELECT query as the source of data for an INSERT.

error_reporting(E_ALL);
ini_set('display_errors', 1);
include '../includes/conn.php';

if (isset($_POST['insert_selection'])) {
  $pid = implode(',', $_POST['id']);
  $u_id = $_POST['u_id'];

  $sql = "INSERT INTO selection (c_id,p_id,tradexx_price,pvp,product_name,img)
          SELECT ?, id, product_price, pvp, product_title, product_image
          FROM products
          WHERE FIND_IN_SET(id, ?)";
  $stmt = $con->prepare($sql);
  $stmt->bind_param("i", $u_id, $pid);
  $stmt->execute(); }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Comments are not for extended discussion; this conversation has been moved to chat.

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.