Hirdetés

Keresés

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

  • sztanozs

    veterán

    válasz bambano #6034 üzenetére

    ahh, persze, kimaradt a group id. nem volt kedvem felhuzni egy php pdo-val, hogy kiprobaljam... amugy meg tenyleg teljesen rossz a sema, es persze a tablak letrehozasanak a sorrendje is szamit.
    vsz most jol es kicsit egyszerubben
    //Create groups table - First table with shared ID - This table provides ID for suppliers table
    $sql = "CREATE TABLE IF NOT EXISTS supplier_groups (
            id INTEGER,
            group_name TEXT NOT NULL,
           UNIQUE(id, group_name))";

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

    try {
        $connection->exec($sql);
        echo "Table suppliers 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 supplier_groups (group_name) VALUES (:name);
            SELECT id FROM supplier_groups WHERE group_name = :name)";
    $sql2 = "INSERT INTO suppliers (supplier_name, group_id) VALUES (:name, :id)";

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

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