0

I am trying to insert an image into a mysql table using React.js. The MySQL table is structured with 3 columns; device: String, name: string, image: longBlob. The image might need to be a different datatype? I have properly retrieved the image as a buffer in my React app and the data looks like this within the React app...

<Buffer ff d8 ff e1 02 b7 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 09 01 03 
00 03 00 00 00 01 00 06 00 00 01 00 00 04 00 00 00 01 00 00 02 a3 01 10 00 02 ... 57470 more bytes>

When I use my sql insert statement like so,

"UPDATE deviceImage SET name='" + name + "', data='" + data + "'  WHERE device='000209'"

the data variable changes form and has a lot of invalid characters such as {, [, and $. How can I properly insert this buffer when it has these characters?

The sql statement looks like this if I print it to the console...

UPDATE deviceImage SET name='006.JPEG', data='����☻�ExifMM*     ☺♥♥☺♠☺♦☺☻�☺►☻  ♥�☺☼�☺1☻♫��i♦☺�☺↕♦☺☺2☻¶�SM-G970UsamsungG970USQU5GUCG2021:05:23 18:51:06§�☻♣☺☺��☻☺��♦
☺☺��"♥☺☻�☺♥☺☺� ☻♀☺Ē♣♣☺☺Ј'♥☺☺@�♣♥☺→�♥
☺☺ؐ♥☻¶☺�♦♣☺☺��♥♥☺�♦☻¶☺��♥☺☻�☻♥☺�
♣☺☻►��♣☺☻↑�     ♥☺♥☺��♣☺☻ td0220dL12XLLD00SM�d......

So, the data variable goes from a buffer array into this thing, which has invalid chars.

Justin O
  • 67
  • 1
  • 11

1 Answers1

0

This took me a bit to find but the answer was to simply avoid string concatenation in my sql query, which you shouldn't do anyways. Here is the correct query which resolved my issue.

let sql = "UPDATE deviceImage SET name=?, image=? WHERE device='000209'"
dbUser.query(sql, [name, data], (err, result) => {
    res.send(result)
    res.end()
})
Justin O
  • 67
  • 1
  • 11