#Change data in mysql table with an html page, using echo and Gorm and upload photo's on html page

4 messages · Page 1 of 1 (latest)

past bolt
#

I'm a beginner with Go, this is my first year following a study in IT. As an assignment I need to make a photo sharing page. I learned some basics of Go but definitely not enough to get all of it to work 🙂

What I go to work work:

  • An html page that can create new users, login and add a cookie.

How I need help:

  1. I made a start to change the user accounts with an html page, but didn't get it to work to run the project.
  2. I want to create groups (combine table users and groups to table3) using an html page. I don't know how to start with this part, maybe if I get part 1 to work I might understand this better as well.
  3. I did get a page to upload photo's, but I didn't get it to work with echo in my main project.

I don't know what might be the best way to get help. Maybe share my project or something else might be better?

past bolt
#

Change data in mysql table with an html page, using echo and Gorm and upload photo's on html page

past bolt
#

question 1: main.go file

package main

import (
    "database/sql"
    "net/http"

    "github.com/labstack/echo/v4"
)

func usersHandler(c echo.Context) error {
    //connect to mysql database
    db, err := sql.Open("mysql", "root:password@tcp(127.0.0.1:3306)/eindopdracht?charset=utf8mb4&parseTime=True&loc=Local")
    if err != nil {
        return err
    }
    defer db.Close()

    //query to select all users
    rows, err := db.Query("SELECT id, name, email FROM users")
    if err != nil {
        return err
    }
    defer rows.Close()

    var users []User
    for rows.Next() {
        var u User
        if err := rows.Scan(&u.ID, &u.Name, &u.Email); err != nil {
            return err
        }
        users = append(users, u)
    }
    return c.Render(http.StatusOK, "users.html", users)
}
#

question 1: html file

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Users</title>
</head>

<body>
    <table>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
            <th>Actions</th>
        </tr>
        {{range .Users}}
        <tr>
            <td>{{.ID}}</td>
            <td>{{.Name}}</td>
            <td>{{.Email}}</td>
            <td>
                <button class="delete-btn" data-id="{{.ID}}">Delete</button>
                <button class="edit-btn" data-id="{{.ID}}">Edit</button>
            </td>
        </tr>
        {{end}}
    </table>

    <form id="add-form">
        <label for="name">Name:</label>
        <input type="text" id="name" name="name">
        <label for="email">Email:</label>
        <input type="email" id="email" name="email">
        <button type="submit">Add User</button>
    </form>

    <form id="edit-form" style="display: none;">
        <input type="hidden" id="edit-id" name="id">
        <label for="name">Name:</label>
        <input type="text" id="edit-name" name="name">
        <label for="email">Email:</label>
        <input type="email" id="edit-email" name="email">
        <button type="submit">Save Changes</button>
    </form>

</body>

</html>