Gitlab@Informatics
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cloudupdate_prj
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
65160270
cloudupdate_prj
Commits
4e80835d
Commit
4e80835d
authored
2 months ago
by
65160270
Browse files
Options
Downloads
Patches
Plain Diff
update-cart
parent
d8295efd
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
config/database.js
+4
-0
4 additions, 0 deletions
config/database.js
shop-routes/product.js
+17
-13
17 additions, 13 deletions
shop-routes/product.js
with
21 additions
and
13 deletions
config/database.js
+
4
−
0
View file @
4e80835d
...
...
@@ -22,4 +22,8 @@ const pool = mysql.createPool({
}
})();
pool
.
getConnection
()
.
then
(()
=>
console
.
log
(
"
Database connected!
"
))
.
catch
((
err
)
=>
console
.
error
(
"
Database connection error:
"
,
err
));
module
.
exports
=
pool
;
\ No newline at end of file
This diff is collapsed.
Click to expand it.
shop-routes/product.js
+
17
−
13
View file @
4e80835d
...
...
@@ -42,25 +42,29 @@ router.get("/add", (req, res) => {
// เพิ่มสินค้าใหม่พร้อมรูป
router
.
post
(
"
/add
"
,
upload
.
single
(
"
image
"
),
async
(
req
,
res
)
=>
{
console
.
log
(
"
Received Data
:
"
,
req
.
body
);
//
เช็คค่าที่ส่งมา
console
.
log
(
"
Uploaded file
:
"
,
req
.
file
);
//
เช็คว่า multer ได้ไฟล์ไห
ม
console
.
log
(
"
req.file
:
"
,
req
.
file
);
//
ดูว่า multer ได้รับไฟล์ไหม
console
.
log
(
"
req.body
:
"
,
req
.
body
);
//
ดูค่าที่ส่งมาจากฟอร์
ม
try
{
const
{
name
,
price
,
stock
,
description
}
=
req
.
body
;
const
priceNum
=
parseFloat
(
price
);
const
stockNum
=
parseInt
(
stock
,
10
);
if
(
isNaN
(
priceNum
)
||
isNaN
(
stockNum
))
{
return
res
.
status
(
400
).
send
(
"
Price and stock must be valid numbers.
"
);
if
(
!
req
.
file
)
{
console
.
error
(
"
Error: No file uploaded!
"
);
return
res
.
status
(
400
).
send
(
"
File upload failed
"
);
}
// ถ้าไม่มีการอัปโหลดไฟล์ ให้ imagePath เป็น null
cons
t
imagePath
=
req
.
file
?
"
/uploads/
"
+
req
.
file
.
filename
:
null
;
const
imagePath
=
"
/uploads/
"
+
req
.
file
.
filename
;
cons
ole
.
log
(
"
imagePath:
"
,
imagePath
);
// ตรวจสอบพาธไฟล์รูป
try
{
await
pool
.
execute
(
"
INSERT INTO products (name, price, stock, description, image_url) VALUES (?, ?, ?, ?, ?)
"
,
[
name
,
price
Num
,
stock
Num
,
description
,
imagePath
]
[
name
,
price
,
stock
,
description
,
imagePath
]
);
}
catch
(
error
)
{
console
.
error
(
"
Database Error:
"
,
error
);
return
res
.
status
(
500
).
json
({
error
:
"
Database Error
"
,
details
:
error
});
}
res
.
redirect
(
"
/products
"
);
}
catch
(
error
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment