[Solved] How can I insert a dash separated string into my database? [closed]


This should work for you:

Just PDO::prepare() your INSERT statement and then loop through your explode()‘d input and insert the values into your db.

<?php

    $input = "12345-45678-543245";
    $arr = explode("-", $input);

    $host = "localhost";
    $dbname = "myDBName";
    $user = "root";
    $password = "";

    try {

        $dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $password);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $stmt = $dbh->prepare("INSERT INTO myTable (`part_id`, `Part numbers`) VALUES(?, ?)");
        foreach($arr as $k => $v)
            $stmt->execute([($k+1), $v]);

    } catch(PDOException $e) {
        echo $e->getMessage();
    }

?>

solved How can I insert a dash separated string into my database? [closed]