0

I get values in array for one parameter which is width.

HTML file

<div class="col-sm-2"><input type="number" name="width[]" class="form-control"></div> 
<div class="col-sm-2"><input type="number" name="width[]" class="form-control"></div> 
<div class="col-sm-2"><input type="number" name="width[]" class="form-control"></div>  

After I submit values I get only one record in db for width, other variables are all the same except for width which is different.

this is php code

 if (isset($_POST['submit'])) {

    $width  = $_POST['width']; 
    $quantity =   $_POST['quantity'];
    $length =  $_POST['length']; 
    $productID =  $_POST['productID'];

    $date = date("Y-m-d H:i");

    foreach ($width as $value) { 

        $stmt=$conn->prepare("INSERT INTO testDB (width, quantity, length, productID)                 
        VALUES (:witdh, :quantity, :length, :productID) "); 

        $stmt->bindParam(':witdh',$value);
        $stmt->bindParam(':quantity',$quantity);
        $stmt->bindParam(':length',$length);
        $stmt->bindParam(':productID',$productID); 

        if($stmt->execute()){
            echo "success";
            exit();
        } 
        else{
            print_r($stmt->errorInfo()); 
        }  
    }   
}
5
  • 3
    is the spelling correct ... witdh or width ??? Commented Oct 17, 2019 at 9:52
  • 4
    are the spaces in your html correct ? "width []" should be "width[]" Commented Oct 17, 2019 at 9:53
  • @Oops it is width, sorry I was wrong when typing Commented Oct 17, 2019 at 9:59
  • Although it may be misleading. The typo doesn't really matter as it's consistent between the SQL statement and the bindParam call. Commented Oct 17, 2019 at 10:01
  • The bind which sets the quantity in the SQL only ever uses the value $quantity, as this doesn't change in the loop, it will always be the same for each record. The only value which is different is the width field. Commented Oct 17, 2019 at 10:03

2 Answers 2

1

When you do

    if($stmt->execute()){
        echo "success";
        exit();
    } 

This will execute the prepared statement and if it succeeds (even in the first time around the loop) it will exit the code!

You should also prepare the statement before the loop, so use...

$stmt=$conn->prepare("INSERT INTO testDB (witdh, quantity, length, productID)                 
    VALUES (:witdh, :quantity, :length, :productID) "); 
$stmt->bindParam(':witdh',$value);
$stmt->bindParam(':quantity',$quantity);
$stmt->bindParam(':length',$length);
$stmt->bindParam(':productID',$productID); 
foreach ($width as $value) { 

    if($stmt->execute()){
        echo "success";
    } 
    else{
        print_r($stmt->errorInfo()); 
    }  
}   

You can set a flag to say if anything fails, how you handle this is up to you.

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

9 Comments

This is not wrong, but not part of his current problem. He got the name wrong.
@BlackNetworkBit, even if the name is corrected, they will still only get 1 record. It's a case of having to fix 2 problems to get it working.
I think the exit() is just there to test.
@Rahul, still not sure as one of the problems is that OP says that the other fields don't change (I think) which as they aren't changed means they may be expecting something else. Have to wait and see.
Then, OP has to figure out by 'emself.
|
0

You have to remove space from name input element.

<div class="col-sm-2"><input type="number" name="width[]" class="form-control"></div> 
<div class="col-sm-2"><input type="number" name="width[]" class="form-control"></div> 
<div class="col-sm-2"><input type="number" name="width[]" class="form-control"></div>  

If you check, I removed space from the name after width.

If will give proper output in print_r($_POST['width']);

EDIT: Please remove exit(); as it is stopping the loop after the first insert.

5 Comments

I made mistake when I was typing. I got an array with values, but each widh should be new record in table. I got only one record
if you are getting $width multiple values, then there is no problem going forward.
The edit portion which is basically saying what I answered 10 minutes ago.
@Rahul exit was problem.I have three places to enter width, if I only enter 1 width it inserts three rows in database ? How I can prevent that
Replace your$width = $_POST['width']; with $width = array_filter(array_map('trim',$_POST['width'])); and check

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.