Insert into 2 tables from 1 form. Mysql Transaction?

后端 未结 3 1815
南方客
南方客 2021-01-19 17:07

The user will create an article and submit an image with it.

+The article will go to the articles table.

+The image will go to images table (so that it can

3条回答
  •  孤街浪徒
    2021-01-19 17:51

    $mysqli->query("START TRANSACTION");
    $stmt = $mysqli->prepare('INSERT INTO articles(article_title, article_text, article_date) VALUES (?, ?, NOW())');
    $stmt->bind_param('ss', $_POST['article_name'], $_POST['description']);
    $stmt->execute();
    $stmt = $mysqli->prepare('INSERT INTO images (article_id, image_caption) VALUES(LAST_INSERT_ID(),?)');
    $stmt->bind_param('s', $_POST['image_caption']);
    $stmt->execute();
    $stmt->close();
    $mysqli->query("COMMIT");
    

提交回复
热议问题