I uploaded file with other fields such as pname, pdescription and etc.
but there is an error.
“http: panic serving 127.0.0.1:52987: Error 1054: Unknown column ‘pname’ in ‘field list’”
This is my frontend code.
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6">
<div class="form-group row">
<div class="col-md-3">
<label> Prodcucto name </label>
</div>
<div class="col-md-9">
<input type="text" name="pname" class="form-control" />
</div>
</div>
<div class="form-group row">
<div class="col-md-3">
<label> Prodcucto description</label>
</div>
<div class="col-md-9">
<input type="text" name="pdescription" class="form-control" />
</div>
</div>
<div class="form-group row">
<div class="col-md-3">
<label>Valorunidade</label>
</div>
<div class="col-md-9">
<input type="text" name="cellnumber" class="form-control" />
</div>
</div>
<div class="form-group row">
<div class="col-md-3">
<label> Producto img </label>
</div>
<div class="col-md-9">
<input type="file" name="pimg" class="form-control" />
</div>
</div>
<div class="form-group row">
<div class="col-md-3">
<label> status </label>
</div>
<div class="col-md-9">
<select name="status" class="form-control">
<option selected>Selecione um status</option>
<option value="pago">Pago</option>
<option value="parcial">Parcial</option>
<option value="em aberto">Em Aberto</option>
</select>
</div>
</div>
<div class="form-group row">
<div class="col-md-3">
<label> Obs </label>
</div>
<div class="col-md-9">
<textarea type="text" name="obs" class="form-control"></textarea><br />
</div>
</div>
<button type="submit" class="btn btn-info" value="Gerar Producto">Gerar Producto</button>
</div>
</div>
</form>
and My upload function
func ProductStore(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(10 << 20)
// FormFile returns the first file for the given key `myFile`
// it also returns the FileHeader so we can get the Filename,
// the Header and the size of the file
file, handler, err := r.FormFile("pimg")
if err != nil {
fmt.Println("Error Retrieving the File")
fmt.Println(err)
return
}
defer file.Close()
fmt.Printf("Uploaded File: %+v\n", handler.Filename)
fmt.Printf("File Size: %+v\n", handler.Size)
fmt.Printf("MIME Header: %+v\n", handler.Header)
// Create a temporary file within our temp-images directory that follows
// a particular naming pattern
tempFile, err := ioutil.TempFile("product", "*.png")
fmt.Println(tempFile)
if err != nil {
fmt.Println(err)
}
defer tempFile.Close()
// read all of the contents of our uploaded file into a
// byte array
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
fmt.Println(err)
}
// write this byte array to our temporary file
tempFile.Write(fileBytes)
// return that we have successfully uploaded our file!
fmt.Fprintf(w, "Successfully Uploaded File\n")
//Open database connection
db := dbConn()
// Check the request form METHOD
if r.Method == "POST" {
// Get the values from Form
pname := r.FormValue("pname")
pdescription := r.FormValue("pdescription")
valorunidade := r.FormValue("valorunidade")
pdatafabi := r.FormValue("pdatafabi")
pimg := "product.png"
status := r.FormValue("status")
obs := r.FormValue("obs")
// Prepare a SQL INSERT and check for errors
insForm, err := db.Prepare("INSERT INTO clientes(pname, pdescription, valorunidade, pdatafabi, pimg, status, obs) VALUES(?,?,?,?,?,?,?)")
if err != nil {
panic(err.Error())
}
// Execute the prepared SQL, getting the form fields
insForm.Exec(pname, pdescription, valorunidade, pdatafabi, pimg, status, obs)
// Show on console the action
log.Println("INSERT: pname " +pname+ " | pdescription " +pdescription+ " | valorunidade " +valorunidade+ " | pdatafabi " +pdatafabi+ " |pimg " +pimg+ " |status " +status+ " |obs " +obs)
}
// Close database connection
defer db.Close()
// Redirect to HOME
http.Redirect(w, r, "/product", 301)
}
what is problem?