How to update the image in the database, namely how to correctly write a request?

INSERT INTO tovars (name) VALUES('$uploadfile') 

name - the name of the field where the address of the image on the server is stored as /o/1.png $ uploadfile - contains the path of the image that is written to the database. Tell me how to write an update request for pictures in the database. The problem is that I have a picture on a certain line. That is, you need a condition where id = id I tried myself

 UPDATE `tovars` SET `name`='$uploadfile' where id=$id 

does not work

  • yes, just the way. - Eliot
  • try this $ sql = "UPDATE tovars SET name = '" + YOUR_NAME_CARDINK + "' WHERE id =" + REC_ID; If you want through variables, then UPDATE tovars SET name = '$ uploadfile' where id = $ id (here the matter is that text variables must be escaped) - Chubatiy
  • Perhaps the trouble is just due to the fact that you put ` - Chubatiy
  • @Chubatiy, the problem is in the condition where Where id = $ id, launched with the parameter like this UPDATE tovars SET name = '$ uploadfile' where id = 79, the picture has changed - Eliot
  • Do you have a variable $ id in the code? - Chubatiy

1 answer 1

If you use the standard library of working with Mysql - mysql_query, then

 $sql = "UPDATE `tovars` SET `name`='".$uploadfile."' where id=".$id; 

or

  $sql = "UPDATE `tovars` SET `name`='{$uploadfile}' where id='{$id}'"; 

Note that string variables in the table are entered in quotes, otherwise they may cause an SQL error. You can read more about this in the official PHP documentation. Strings.

I also advise you to read the articles on the safety of data storage without protection (shielding, etc.)