Insert post with multiple tags in PDO

netlover

DB Structure:

Table: posts
Columns: postid, postsubject, contents

Table: tags
Columns: tagid, tagtxt

Table: posts_tags
Columns: postid, tagid

//already added the posts and get last id and put it in $newId

insert in tags table and in map for posts_tags:

if (isset($_POST['tagtxt'])){
$tags = explode(",", $_POST['tagtxt']);

    for ($x = 0; $x < count($tags); $x++){

        //Due to unique it will only insert if the tag dosent already exist
        $sql_addTags = "INSERT INTO tags (tagtxt) VALUES (?)";
        $stmt = $kanzconn->prepare($sql_addTags);
        $stmt->bindValue(1, $tags[$x], PDO::PARAM_STR); 
        $stmt->execute();       

        //Add the relational Link
        $sql_addRelational = "INSERT INTO posts_tags (postid,tagid) VALUES (?,?)";
        $stmt = $kanzconn->prepare($sql_addRelational);
        $stmt->bindValue(1, $newId, PDO::PARAM_INT);
        $tid = ('SELECT tags.tagid FROM tags WHERE tags.tagtxt = $tags[$x]');
        $stmt->bindValue(2, $tid, PDO::PARAM_INT); 
        $stmt->execute();   

    }
}

NOTE:

$newId = $kanzconn->lastInsertId()

the error:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row:
a foreign key constraint fails (`dbname`.`posts_tags`, CONSTRAINT `posts_tags_ibfk_2`
FOREIGN KEY (`tagid`) REFERENCES `tags` (`tagid`) ON DELETE CASCADE) 

Thsnks

netlover

This is work fine!

Insert any explode $_POST['tagtxt'] to tags table on the condition of non duplication

And insert relational Link for post and tags id in posts_tags table

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

$tags = explode(",", $_POST['tagtxt']);

    for ($x = 0; $x < count($tags); $x++){

    //Due to unique it will only insert if the tag dosent already exist
    $sql_addTags = "INSERT INTO tags (tagtxt) VALUES (?) ON DUPLICATE KEY UPDATE tagtxt = ?";
    $stmt = $kanzconn->prepare($sql_addTags);
    $stmt->bindValue(1, $tags[$x], PDO::PARAM_STR); 
    $stmt->bindValue(2, $tags[$x], PDO::PARAM_STR); 
    $stmt->execute();

    //get tags.tagid inserted or updated in above query
    $TID = $kanzconn->query("SELECT tags.tagid FROM tags WHERE tags.tagtxt = '$tags[$x]'")->fetchColumn();

    //Add the relational Link
    $sql_addRelationalTags = "INSERT INTO posts_tags (postid,tagid) VALUES (?,?)";
    $stmt = $kanzconn->prepare($sql_addRelationalTags);
    $stmt->bindValue(1, $newId, PDO::PARAM_INT);
    $stmt->bindValue(2, $TID, PDO::PARAM_INT); 
    $stmt->execute();

    }
}

Thanks to the users here, especially angel-king-47

PHP/MySQL - How to add multiple tags

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PDO insert not inserting with multiple values

From Dev

PHP PDO Multiple image INSERT

From Dev

Pdo multiple row insert issue

From Dev

Find post with multiple tags

From Dev

Finding post with multiple tags

From Dev

insert multiple values with tags into influxdb

From Dev

PDO insert statement with loop through $_POST array

From Dev

PDO insert statement with loop through $_POST array

From Dev

multiple PDO insert with single statement not working

From Dev

From Textarea to multiple value insert using PDO

From Dev

PDO Insert multiple checkbox values in Mysql

From Dev

Insert multiple rows using PHP PDO

From Dev

PHP, PDO, MySQL - Multiple INSERT vulnerable to injection?

From Dev

get first insert id for multiple insert using pdo in mysql

From Dev

how to Insert multiple arrays with multiple rows into MySQL using PHP PDO

From Dev

What is the best way to insert multiple rows in PHP PDO MYSQL?

From Dev

How to insert multiple records at once with mysqli similar to pdo

From Dev

PDO and binding multiple value sets during insert - recently

From Dev

Insert multiple values via PHP PDO into MySQL database

From Dev

php pdo select multiple rows and insert to other table with LIMIT

From Dev

insert pdo query issue, multiple inserts in wrong table

From Dev

Insert multiple rows into database (PDO) [Items separated by commas]

From Dev

PDO with $_POST

From Dev

how to make an insert function with in a database class to insert a new record with multiple coloumns and multiple values (using PDO )?

From Dev

Insert multiple records to BigQuery using POST api

From Dev

Multiple INSERT INTO MySQL from $_POST array

From Dev

MySQLi - Insert multiple rows - empty POST array

From Dev

Insert multiple records to BigQuery using POST api

From Dev

PHP Form Post and Insert into multiple rows

Related Related

  1. 1

    PDO insert not inserting with multiple values

  2. 2

    PHP PDO Multiple image INSERT

  3. 3

    Pdo multiple row insert issue

  4. 4

    Find post with multiple tags

  5. 5

    Finding post with multiple tags

  6. 6

    insert multiple values with tags into influxdb

  7. 7

    PDO insert statement with loop through $_POST array

  8. 8

    PDO insert statement with loop through $_POST array

  9. 9

    multiple PDO insert with single statement not working

  10. 10

    From Textarea to multiple value insert using PDO

  11. 11

    PDO Insert multiple checkbox values in Mysql

  12. 12

    Insert multiple rows using PHP PDO

  13. 13

    PHP, PDO, MySQL - Multiple INSERT vulnerable to injection?

  14. 14

    get first insert id for multiple insert using pdo in mysql

  15. 15

    how to Insert multiple arrays with multiple rows into MySQL using PHP PDO

  16. 16

    What is the best way to insert multiple rows in PHP PDO MYSQL?

  17. 17

    How to insert multiple records at once with mysqli similar to pdo

  18. 18

    PDO and binding multiple value sets during insert - recently

  19. 19

    Insert multiple values via PHP PDO into MySQL database

  20. 20

    php pdo select multiple rows and insert to other table with LIMIT

  21. 21

    insert pdo query issue, multiple inserts in wrong table

  22. 22

    Insert multiple rows into database (PDO) [Items separated by commas]

  23. 23

    PDO with $_POST

  24. 24

    how to make an insert function with in a database class to insert a new record with multiple coloumns and multiple values (using PDO )?

  25. 25

    Insert multiple records to BigQuery using POST api

  26. 26

    Multiple INSERT INTO MySQL from $_POST array

  27. 27

    MySQLi - Insert multiple rows - empty POST array

  28. 28

    Insert multiple records to BigQuery using POST api

  29. 29

    PHP Form Post and Insert into multiple rows

HotTag

Archive