user3166656
user3166656

Reputation: 15

PDO SQL won't add new entries

No error is showing, but for some reason I cannot add new entries when I run this script. Any help would be appreciated.

$sth = $dbh->prepare('INSERT INTO inventory(sku, product-id, product-id-type, price, minimum-seller-allowed-price, maximum-seller-allowed-price, item-condition, quantity, add-delete, will-ship-internationally, expedited-shipping, item-note, fulfillment-center-id)
VALUES(:sku, :product_id, :product_id_type, :price, :minimum, :maximum, :condition, :quantity, :add_delete, :international, :expedited, :note, :fulfillment)');
$sth->bindParam(':sku', $sku);
$sth->bindParam(':product_id', $product_id);
$sth->bindParam(':product_id_type', $product_id_type);
$sth->bindParam(':price', $price);
$sth->bindParam(':minimum', $minimum);
$sth->bindParam(':maximum', $maximum);
$sth->bindParam(':condition', $condition);
$sth->bindParam(':quantity', $quantity);
$sth->bindParam(':add_delete', $add_delete);
$sth->bindParam(':international', $international);
$sth->bindParam(':expedited', $expedited);
$sth->bindParam(':note', $note);
$sth->bindParam(':fulfillment', $fulfillment);
$sth->execute();

Upvotes: 1

Views: 34

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

"No error is showing..."

No errors? Because, you're not checking for them. Many of your columns contain hyphens and MySQL thinks you want to do math.

I.e.: product-id which MySQL translates as "product minus id" etc.

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)
would have signaled the syntax errors.

Wrap all your columns that contains hyphens with ticks.

(sku, `product-id`, `product-id-type` ...

etc.

  • Add $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); right after the connection is opened.

Nota:

  • You should try and avoid using hyphens as word seperators and use underscores instead.
    That way you won't have to use ticks.

Upvotes: 2

CodingInTheUK
CodingInTheUK

Reputation: 948

Added here because i can't code it properly in a comment. This is not an answer to the question but i suspect will lead directly to it.

Alter your code like this.

try { 
    // All $sth lines above in this section.

} catch (PDOException $e) {
    // this will print an exception out if there is one.
    echo $e->getMessage();
}

Upvotes: 0

Related Questions