Hirdetés

Keresés

Új hozzászólás Aktív témák

  • sztanozs

    veterán

    válasz lanszelot #6027 üzenetére

    //Create another table - Main table for shared ID -This table share ID with supplier_groups
    $sql = "CREATE TABLE IF NOT EXISTS suppliers (
            id   INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
           supplier_name TEXT NOT NULL,
            email TEXT,
           UNIQUE(id, supplier_name))";

    try {
        $connection->exec($sql);
        echo "Table suppliers created successfully";
    } catch (PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
    //Create another table - Secondary table with shared ID - This table got ID from suppliers table
    $sql = "CREATE TABLE IF NOT EXISTS supplier_groups (
            id INTEGER,
            group_name TEXT NOT NULL,
           FOREIGN KEY (id) REFERENCES suppliers (id))";

    try {
        $connection->exec($sql);
        echo "Table supplier_groups created successfully";
    } catch (PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
    // Create (Insert) Data. SQL query to insert data into the "suppliers" table
    $sql1 = "INSERT OR IGNORE INTO suppliers (group_name) VALUES (?)"
    $sql2 = "SELECT id FROM group_name WHERE group_name = ?)";
    $sql3 = "INSERT INTO suppliers (supplier_name) VALUES (?, ?)";

    try {
       $statement = $connection->prepare($sql1);
       $statement->exec(['jedi']);
       $statement = $connection->prepare($sql2);
       $statement->exec(['jedi']);
       $gid = $statement->fetchColumn();
       $statement = $connection->prepare($sql3);
       $statement->exec(['Obi van Kenobi', $gid]);
        echo "Data inserted successfully";
    } catch (PDOException $e) {
        echo "Error: " . $e->getMessage();
    }

Új hozzászólás Aktív témák