0

I have array of textbox

<form method="POST">
  <input type="text" name="partnumber[]" value="1234">
  <input type="text" name="desc[]" value="used">
  <input type="text" name="qty[]" value="24">
  <input type="text" name="partnumber[]" value="2345">
  <input type="text" name="desc[]" value="good">
  <input type="text" name="qty[]" value="31">
  <input type="text" name="partnumber[]" value="3456">
  <input type="text" name="desc[]" value="brand new">
  <input type="text" name="qty[]" value="22">
  <input type="submit">
</form>

after posting, i want to insert to my database like this. with the same ref_id.

last_insert_id = 17;

id | ref_id | partnumber| description | quantity
1  | 17     | 1234      | used        | 24
2  | 17     | 2345      | good        | 31
3  | 17     | 3456      | brand new   | 22

any help will be appriciated. thanks..

$comma  = "";
$values = "";
$last_id = 17;
foreach ($partnumber as $p) {
    foreach ($desc as $d) {
       foreach ($qty as $q) {
        }
    }
     $values .= $comma."($id,$last_id,'$p','$d','$q')";
     $comma = ",";
}


$sql = "INSERT INTO parts_replaced (id,last_id,partnumber,description,quantity) VALUES $values";
2
  • What code do you have already? Commented Sep 9, 2015 at 5:45
  • I already update the question with my code but it seems that it's not the proper way :( Commented Sep 9, 2015 at 5:49

5 Answers 5

1

you can get the out of this form like that..

 <form method="POST">
          <input type="text" name="partnumber[]" value="1234">
          <input type="text" name="desc[]" value="used">
          <input type="text" name="qty[]" value="24">
          <input type="text" name="partnumber[]" value="2345">
          <input type="text" name="desc[]" value="good">
          <input type="text" name="qty[]" value="31">
          <input type="text" name="partnumber[]" value="3456">
          <input type="text" name="desc[]" value="brand new">
          <input type="text" name="qty[]" value="22">
          <input type="submit">
        </form>
        <?php 
        if(isset($_POST['partnumber']))
        {
        $parnumber=$_POST['partnumber'];

        foreach ($parnumber as $key => $value) {
            echo $value.'-'.$_POST['desc'][$key].'-'.$_POST['qty'][$key].'<br />';

//add your query here
        }

        }

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

Comments

1

Please try this

extract($_POST);
$last_id = 17; 
foreach($partnumber as $key => $p ){  // Can use any of the mandatory field here
        $part_no = $p;
    $description = $desc[$key];
    $qty = $qty[$key];
    $sql = "INSERT INTO parts_replaced (`ref_id`,`partnumber`,`description`,`qty`) VALUES ('".$last_id."', '".$part_no."','".$description."','".$qty ."') ";
    mysql_query($sql); // If id is auto incremented no need to specify it in query
}

Comments

0

The count of fields in your INSERT INTO do not match the count of values:

$sql = "INSERT INTO parts_replaced (id,last_id,part_no,description,qty) VALUES $values";

$values .= $comma."($id,'$p','$d','$q')";

Comments

0

Make your html as below :

<?php $i=0;?>

<form method="POST">
  <input type="text" name="partnumber[<?php echo $i;?>]" value="1234">
  <input type="text" name="desc[<?php echo $i;?>]" value="used">
  <input type="text" name="qty[<?php echo $i;?>]" value="24">
  <input type="text" name="partnumber[<?php echo $i+1;?>]" value="2345">
  <input type="text" name="desc[<?php echo $i+1;?>]" value="good">
  <input type="text" name="qty[<?php echo $i+1;?>]" value="31">
  <input type="text" name="partnumber[<?php echo $i+12;?>]" value="3456">
  <input type="text" name="desc[<?php echo $i+2;?>]" value="brand new">
  <input type="text" name="qty[<?php echo $i+2;?>]" value="22">
  <input type="hidden" name="arr_length" value="<?php echo $i+2;?>">
  <input type="submit">
</form>

Handle in php as below :

<?php
    if(isset($_POST['arr_length']) && $_POST['arr_length'] > 0){
        for($i=0;$i<=$_POST['arr_length'];$i++){
            echo $_POST['partnumber'][$i].'---'.$_POST['desc'][$i].'---'.$_POST['qty'][$i].'<br>';
        }
    }

?>

Comments

0
$values = "";
$last_id = 17;
$parnumber = $_POST['partnumber'];
for($i=0, $max = count($parnumber); $i < $max; $i++) {
    $p = $_POST['parnumber'][$i];
    $desc = $_POST['desc'][$i];
    $qty = $_POST['qty'][$i];
     $values .= "('$id','$last_id','$p','$desc','$qty'), ";
}
$str = trim($values);// trim the last white space
$str = rtrim($values, ',');//remove last comma

$sql = "INSERT INTO parts_replaced (id,last_id,part_no,description,qty) VALUES $str";

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.