Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
| modul:m290:learningunits:lu09:theorie:03 [2024/11/05 13:55] – vdemir | modul:m290:learningunits:lu09:theorie:03 [2024/11/05 14:51] (aktuell) – vdemir | ||
|---|---|---|---|
| Zeile 99: | Zeile 99: | ||
| | | ||
| {{: | {{: | ||
| + | | ||
| + | ==== C - CREATE: Insert a new row into the db ==== | ||
| + | Finally, with the INSERT operation, we complete our CRUD requirements. | ||
| + | |||
| + | // insert a row | ||
| + | app.post('/ | ||
| + | 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(' | ||
| + | } | ||
| + | // Construct the insert query | ||
| + | const insertQuery = ' | ||
| + | const values = [username, email]; // Use provided fields for insertion | ||
| + | db.query(insertQuery, | ||
| + | if (err) { | ||
| + | console.error(' | ||
| + | return res.status(500).send(' | ||
| + | } | ||
| + | // Respond with the ID of the newly created user | ||
| + | res.status(201).send(`User added with ID: ${result.insertId}`); | ||
| + | }); | ||
| + | }); | ||
| + | |||
| + | {{: | ||
| ---- | ---- | ||
| [[https:// | [[https:// | ||