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

  • sztanozs
    veterán

    Köszönöm szépen a választ.

    Nekem nem működik. Lehet én csinalok valamit rosszul ezért belinkelem a kódodt, és a hibát.
    //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)";

    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
    $sql = "INSERT INTO suppliers (supplier_name) VALUES ('Obi van Kenobi')";
    $sql2 = "INSERT INTO supplier_groups (group_name) VALUES ('jedi')";

    try {
    $connection->exec($sql);
    $connection->exec($sql2);
    echo "Data inserted successfully";
    } catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
    }

    Hiba: "Error: SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: suppliers.group_id"

    //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