1

I haven't been able to get my INSERT statement to work. I know the problem is how I'm trying to bind the parameters. INSERT was working before I added parameter binding.

$inventoryQuery = "INSERT INTO inventory (Whse, Product, Description,
QtyOnHand, QtyUnavail, BinLoc1, BinLoc2, GLCost, OnhandTotalValue,
UnavailTotalValue, GrandTotalValue) 
    VALUES ('?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?');";
// execute INSERT statement
$conn = $GLOBALS['conn'];
$stmt = $conn->prepare($inventoryQuery);
$stmt->bindParam(1, $sqlV[0]);
$stmt->bindParam(2, $sqlV[1]);
$stmt->bindParam(3, $sqlV[2]);
$stmt->bindParam(4, $sqlV[3]);
$stmt->bindParam(5, $sqlV[4]);
$stmt->bindParam(6, $sqlV[5]);
$stmt->bindParam(7, $sqlV[6]);
$stmt->bindParam(8, $sqlV[7]);
$stmt->bindParam(9, $sqlV[8]);
$stmt->bindParam(10, $sqlV[9]);
$stmt->bindParam(11, $sqlV[10]);

$stmt->execute();

Output using var_dump($stmt):

object(PDOStatement)[2] public 'queryString' => string 'INSERT INTO inventory (Whse,Product,Description,QtyOnHand,QtyUnavail,BinLoc1,BinLoc2,GLCost,OnhandTotalValue,UnavailTotalValue,GrandTotalValue) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);' (length=192)

I have seen and tried many ways to handle binding parameters. And now my head is swimming. If you could provide a solution and an explanation, I would be so happy.

Solution from all info provided:

    try{
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $inventoryQuery = "INSERT INTO inventory ($sqlField) 
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    // execute INSERT statement
    $conn = $GLOBALS['conn'];
    $stmt = $conn->prepare($inventoryQuery);
    $stmt->bindValue(1, $sqlV[0]);
    $stmt->bindValue(2, $sqlV[1]);
    $stmt->bindValue(3, $sqlV[2]);
    $stmt->bindValue(4, ((int) ((float) $sqlV[3])));
    $stmt->bindValue(5, ((int) ((float) $sqlV[4])));
    $stmt->bindValue(6, $sqlV[5]);
    $stmt->bindValue(7, $sqlV[6]);
    $stmt->bindValue(8, ((float) $sqlV[7]));
    $stmt->bindValue(9, ((float) $sqlV[8]));
    $stmt->bindValue(10, ((float) $sqlV[9]));
    $stmt->bindValue(11, ((float) $sqlV[10]));

    $stmt->execute();
}
catch(PDOException $ex){
    var_dump($ex);
}

Thanks to everyone that helped!

3
  • You are trying to insert multiple values into one column. Commented Nov 4, 2014 at 14:58
  • I edited the post to show the value of $sqlField variable, which was a string containing all the column names of the table. Commented Nov 4, 2014 at 15:25
  • 2
    @BrentConnor I need to know how all the $sqlV are being populated from. In the meantime, try an array method $stmt->execute(array($sqlV[0], $sqlV[1]), etc.); Commented Nov 4, 2014 at 15:58

2 Answers 2

2

I'm going to go with incorrect column types in this one. Try surrounding your execution in a try-catch block and make sure SQL isn't throwing any errors. If it is, it will likely show you exactly what you need to do.

Here's an example:

try
{
    $stmt->execute();
}
catch (PDOException $ex)
{
    // log this object or just the error messages
    log($ex->getMessage()); // Assuming you have some sort of global logger set up.
}

P.s. You can remove the semi colon in your query string. Being at the end shouldn't hurt anything, but if you were to add anything after it, it would terminate your statement early.

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

Comments

2

This piece of code:

('?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?')

remove the quotes

(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

8 Comments

I removed the quotes, but its still not making it to the database. if I use print_r on $stmt after execute, it outputs: [ PDOStatement Object ( [queryString] => INSERT INTO inventory (Whse,Product,Description,QtyOnHand,QtyUnavail,BinLoc1,BinLoc2,GLCost,OnhandTotalValue,UnavailTotalValue,GrandTotalValue) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?); ) ]
@BrentConnor What errors are you getting? Plus can you var_dump your query?
No errors. var_dump result: object(PDOStatement)[2] public 'queryString' => string 'INSERT INTO inventory (Whse,Product,Description,QtyOnHand,QtyUnavail,BinLoc1,BinLoc2,GLCost,OnhandTotalValue,UnavailTotalValue,GrandTotalValue) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);' (length=192)
@BrentConnor Instead of bindParam try bindValue
changed to bindValue. No affect.
|

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.