The code below uploads images to my sql database called upload_image.
if(isset($_POST['submit'])){
$target_path = "images/";
$target_path = $target_path . basename($_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)){
$conn =new mysqli("localhost", "root", "", "upload_image");
$sql = "Insert into upload_image('path') values('$target_path')";
if($conn->query($sql)==TRUE){
echo"<br><br>";
}else{
echo "Error on upload".$sql.$conn->error;
}
}
}
The error being displayed is
Error on uploadInsert into upload_image('path') values('images/ao.png')
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''path') values('images/ao.png')' at line 1
Here is the HTML part:
<form method="post" enctype="multipart/form-data">
<input type="hidden" value=="1000000" name="MAX_FILE_SIZE"/>
<input type="file" name="file"/>
<input type="submit" name="submit" value="Upload"/>
The HTML and PHP are all in one code.
'path'is a string, not a column. Remove the quotes, those are for strings. Use backticks if using special characters or reserved terms. You also are open to SQL injections parameterize the query.mysqliyou should be using parameterized queries andbind_paramto add user data to your query. DO NOT use string interpolation or concatenation to accomplish this because you have created a severe SQL injection bug. NEVER put$_POST,$_GETor any user data directly into a query, it can be very harmful if someone seeks to exploit your mistake.