0

I need help uploading pictures to my database, in Sequel Pro. This is the HTML code in order to run the PHP script once the user has pressed the "Submit" button:

<form action="/Alesh_New_Website/saveImg.php" method="POST"   enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" name="submit" value="Upload">
</form>

This is the PHP code:

$servername = "localhost"; 
$username = "root";
$password = "root";
$dbname = "IMGSTORAGE";

try{
$conn = new PDO("mysql:host=$servername; dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$name = addslashes($_FILES['image']['name']);
$image = addslashes($_FILES['name']['tmp_name']);
$image = file_get_contents($image);
$image = base64_encode($image);
$sql = "INSERT INTO 'STOREDIMG' ('name', 'image') VALUES ('$name, $image')";

$conn->exec($sql);
echo "Image uploaded";
}
catch (PDOException $e){
echo "Connection Failed: " . $e->getMessage();  
}

?>

I was watching videos in order to create this PHP statement; I barely know about PHP, but I want to learn it at its fullest. I need a way to make this work, since every time I run it, I get this error:

"Connection Failed: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''STOREDIMG' ('name', 'image') VALUES ('Image_Name.jpg, ')' at line 1"

Is there any way I can fix this or make it work in order to serve the purpose I want? I appreciate your help. Thank you.

Paul T.
  • 4,703
  • 11
  • 25
  • 29
  • It's because of the incorrect identifier qualifiers used. Consult the following https://dev.mysql.com/doc/refman/5.7/en/identifier-qualifiers.html including the duplicate the question was closed with. You may have read a book or watched a video on this, confusing ticks `\`` as regular quotes `'` being two different animals. – Funk Forty Niner Aug 25 '17 at 17:05
  • 1
    `('$name, $image')` - that failed also but the duplicate contains the correct syntax. Since you're using PDO, you can fix that with using a prepared statement and do away with variables concatenates. – Funk Forty Niner Aug 25 '17 at 17:06
  • What do you mean duplicate? –  Aug 25 '17 at 17:08

0 Answers0