0

Hello i am trying to submit an array of 3 textfileds from my form and then send to my database, table

my form

  <input type="hidden" name="itemqt[]" value="<?php echo $item["quantity"]; ?>">

<input type="hidden" name="itemname[]" id="text-basic" value="<?php echo $item["name"]; ?>">

<input type="hidden" name="itemprice[]" id="text-basic" value="<?php echo $item["price"]; ?>">

basically when i attempt to combine two arrays it works

foreach (array_combine($_POST['id'], $_POST['itemprice'] ) as $name => $email)

 {

  echo  "";
  //$name . " - " . $email .
  $ocode = $_POST['ocode'];
  $sql = "INSERT INTO `orders` (pid, price,status,method,ocode, quantity) VALUES ('$name','$email','paid','Card','$ocode','$_POST[itemqt]')";

 if (mysqli_query($con, $sql)) {
      echo "";
    }
     else {
    echo "Error: " . $sql . "<br>" . mysqli_error($con);
}

}

}

}

but when i try to attempt to combine a 3 array, with another field like dis, it fails. i get a syntax error

foreach (array_combine($_POST['id'], $_POST['itemprice'], $_POST['itemqt'] ) as $name => $email, $qt)

 {

  echo  "";
  //$name . " - " . $email .
  $ocode = $_POST['ocode'];
  $sql = "INSERT INTO `orders` (pid, price,status,method,ocode, quantity) VALUES ('$name','$email','paid','Card','$ocode','$_POST[itemqt]')";

 if (mysqli_query($con, $sql)) {
      echo "";
    }
     else {
    echo "Error: " . $sql . "<br>" . mysqli_error($con);
}

}

}

}

please any idea, has someone successfully combined 3 arrays

2
  • the very nature of array_combine makes combining 3 arrays impossible, are you perhaps thinking of array_merge? PHP manual defines both pretty clearly. Commented Mar 8, 2016 at 18:09
  • Also, even when you merge all arrays the desired result isn't going to give you what you're trying to get in your foreach loop: $name => $email. It's going to give you a number as $name and either quantity, name, or price as $email Commented Mar 8, 2016 at 18:14

1 Answer 1

1

array_combine makes result array from 2 arrays - one for keys and another for values. What do you expect by adding third array? It would be added to keys, to values? Where?

What you really need is just a foreach loop on some base variable, presumably $_POST['id']:

$i = 0;
foreach ($_POST['id'] as $id) {
    // get values from other $_POST variables with the same key as $i
    echo $id, ': ', $_POST['itemprice'][$i], ' => ', $_POST['itemqt'][$i];
    $i++;
}

Or even simpler:

foreach ($_POST['id'] as $key => $id) {
    // get values from other $_POST variables with the same key as $key
    echo $id, ': ', $_POST['itemprice'][$key], ' => ', $_POST['itemqt'][$key];
}

And sure, you can check if $_POST['itemprice'][$key] or $_POST['itemqt'][$key] exist for a certain $key

Sign up to request clarification or add additional context in comments.

8 Comments

ok, and i suppose this "$_POST['itemprice'][$i]" this can be passed into my sql statement
pls how do i pass the values to my database table, when i try to dump dem in my sql like dis "$_POST[itemprice][$key]," i get Array[1] Array[1]
And what is the output of just $_POST['itemprice']?
i get this Array Array
Use print_r or var_dump
|

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.