Append variable To string in php insert
Append variable To string in php insert
I'm having an issue appending a file name variable to a string in a sql insert like so
$insert = $mysqlConn->query("INSERT into images (image_name, url) VALUES ('".$fileName."', 'images/".$fileName."'");
I can do it with just the $fileName and it works fine but my syntax is wrong. I'm simply trying to make sure that every file name inserted starts with 'images/'
So if I'm inserting 'red.jpg' it would be 'images/red.jpg'
)
VALUES
"INSERT into images (image_name, url) VALUES ('".$fileName."', 'images/".$fileName."')"
You should parameterize your query.
– user3783243
Sep 17 '18 at 4:08
You must use PDO for security reasons to prevent injections.
– Krishnadas PC
Sep 17 '18 at 5:10
2 Answers
2
You can store image value into one variable
$imgPath = 'images/'.$fileName;
Above variable you can pass into the query
try this,you are missing one bracket
$insert = ("INSERT into images (image_name, url) VALUES ('".$fileName."', 'images/".$fileName."')");
Your closing parenthesis is on the wrong side of the quote.
– user3783243
Sep 17 '18 at 4:07
Please don't post answers to questions which are just simple typos. Instead, make a comment (as I have already done) and flag the question as "off-topic" due to a typographical error. Questions like this add no value to the site and will eventually be deleted, thus causing you to lose any reputation that you may gain from answering.
– Nick
Sep 17 '18 at 4:09
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
You're just missing the trailing
)
for theVALUES
, change the string to"INSERT into images (image_name, url) VALUES ('".$fileName."', 'images/".$fileName."')"
– Nick
Sep 17 '18 at 4:05