Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen Revision Vorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
modul:m290:learningunits:lu09:theorie:03 [2024/11/05 13:55] vdemirmodul:m290:learningunits:lu09:theorie:03 [2024/11/05 14:51] (aktuell) vdemir
Zeile 99: Zeile 99:
      
   {{:modul:m290:learningunits:lu09:theorie:crud_u.png?800| Update of one row of data}}   {{:modul:m290:learningunits:lu09:theorie:crud_u.png?800| Update of one row of data}}
 +  
 +==== C - CREATE: Insert a new row into the db ====
 +Finally, with the INSERT operation, we complete our CRUD requirements.
 +
 +  // insert a row
 +  app.post('/user', (req, res) => {
 +      const { username, email } = req.query; // Get username and email from query parameters
 +      // Ensure that both fields are provided
 +      if (!username || !email) {
 +          return res.status(400).send('Username and email are required for insertion.');
 +      }
 +      // Construct the insert query
 +      const insertQuery = 'INSERT INTO users (username, email) VALUES (?, ?)';
 +      const values = [username, email]; // Use provided fields for insertion
 +      db.query(insertQuery, values, (err, result) => {
 +          if (err) {
 +              console.error('Error inserting user:', err);
 +              return res.status(500).send('Error inserting user');
 +          }
 +         // Respond with the ID of the newly created user
 +          res.status(201).send(`User added with ID: ${result.insertId}`);
 +      });
 +  });
 +
 +{{:modul:m290:learningunits:lu09:theorie:crud_c.png?600| How to insert a row into a database table}}
  
 ---- ----
 [[https://creativecommons.org/licenses/by-nc-sa/4.0/|{{https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png}}]] Volkan Demir [[https://creativecommons.org/licenses/by-nc-sa/4.0/|{{https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png}}]] Volkan Demir
  
  • modul/m290/learningunits/lu09/theorie/03.1730811302.txt.gz
  • Zuletzt geändert: 2024/11/05 13:55
  • von vdemir