Hi,
I am facing an issue when inserting values from a form into my mysql -[php]table item_image[/php]- with the -[php]lastInsertId()[/php]-
Here is my db
[php]item table
id(auto increment,Primary key)
item_name (varchar)
item_description (tinytext)
item_price(varchar)
item_date(date)[/php]
[php]item_image
itemId(PRIMARY KEY),
small_image(varchar),
big_image(varchar)[/php]
HERE IS MY FORM
[php]
Name of Article :
Short Description:
Item small Image :
Item small Image:
AND DOWN HERE WE HAVE THE PHP CODE
[php]if (isset($_GET[‘addform’])) {
$item_name = trim($_POST[‘item_name’]);
$item_description = trim($_POST[‘item_description’]);
$item_price = trim($_POST[‘item_price’]);
try {
$sql = ‘INSERT INTO items SET
item_name = :item_name,
item_description = :item_description,
item_price = :item_price,
item_date= CURDATE()’;
$stmt = $pdo->prepare($sql);
$stmt->bindValue(’:item_name’, $item_name);
$stmt->bindValue(’:item_description’, $item_description);
$stmt->bindValue(’:item_price’, $item_price);
$stmt->execute();
}
catch (PDOException $e) {
echo “Something went wrong”.$e->getMessage();
}
$itemId = $pdo->lastInsertId();
if (isset($_POST[‘addimages’])) {
$add_images = $_POST['addimages'];
try {
$sql ='INSERT INTO item_image SET
itemId = :itemId,
small_image = :small_image,
big_image = :big_image';
$stmt = $pdo->prepare($sql);
foreach ($add_images as $add_image) {
$stmt->bindValue(':itemId', $itemId);
$stmt->bindValue(':small_image', $add_image[0]);
$stmt->bindValue(':big_image', $add_image[1]);
$stmt->execute();
}
}
catch (PDOException $e) {
echo "Sth got wrong with the query".$e->getMessage();
}
}[/php]
The issue i am having is : All the values got insert in items table, but for the item_image table…i am having this error :
[php]SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘30’ for key ‘PRIMARY’[/php]
I am having searching, but cannot find the issue…my foreach loop seems to be correct…
I am trying to is to do is to: [php]insert the lastInsertId()[/php] value and [php]addimages array values[/php] into my[php] item_image[/php] table
Can sombody help out…Thanks.