php - getting error "Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064"
Get the solution ↓↓↓i am getting this error = Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 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 '492x328BG5eb39cafa1826.jpg'),('492x328BG5ecbf5f78bd50.jpg'),('492x328BG5edda064e'.....evey time i post
require_once "dbconnect.inc.php";
$errors = array();
$success = array();
if(strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
$subdir = substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890') , 0 , 10 );
$path="uploads";
mkdir($path."/$subdir",0777,true);
$uploadDir = 'uploads/'.$subdir.'/';
$allowTypes = array('jpg','png','jpeg','gif');
if(!empty(array_filter($_FILES['files']['name']))){
foreach($_FILES['files']['name'] as $key=>$val){
$filename = basename($_FILES['files']['name'][$key]);
$targetFile = $uploadDir.$filename;
if(move_uploaded_file($_FILES["files"]["tmp_name"][$key], $targetFile)){
$success[] = "Uploaded $filename";
$insertQrySplit[] = "('$filename')";
}
else {
$errors[] = "Something went wrong- File - $filename";
}
}
//Inserting to database
if(!empty($insertQrySplit)) {
$query = implode(",",$insertQrySplit);
$file_path = $path."/$subdir/";
$sql = ("INSERT INTO upload_images (image,path) VALUES ('".$query."','".$file_path."')");
$stmt= $conn->prepare($sql);
$stmt->execute();
}
}
else {
$errors[] = "No File Selected";
}
}
?>
<!doctype html>
<html lang="en" class="h-100">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Ajax Upload</title>
<!-- Bootstrap core CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.css" rel="stylesheet">
<style>
.formSmall {
width: 500px;
margin: 20px auto 20px auto;
}
.message {
padding:10px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-7">
<h5 class="text-align"> Upload Form</h5>
</div>
<?php if(!empty($success)):?>
<div class="alert alert-success" role="alert">
<ul>
<?php foreach ($success as $val):?>
<li><?php print $val?></li>
<?php endforeach;?>
</ul>
</div>
<?php endif;?>
<!-- Error listing-->
<?php if(!empty($errors)):?>
<div class="alert alert-danger" role="alert">
<ul>
<?php foreach ($errors as $val):?>
<li><?php print $val?></li>
<?php endforeach;?>
</ul>
</div>
<?php endif;?>
<div class="col-lg-7">
<form action="" method="post" enctype="multipart/form-data">
<label>Select Image(s):</label>
<input type="file" name="files[]" multiple >
<br>
<input type="submit" name="submit" value="Upload">
</form>
</div>
</div><!-- .row -->
</div>
</body>
</html>```
Answer
Solution:
you are using'
to start your image value and for his content, sow you need to use\
in the'
that you want to store in your db.
in your php code, just change the line:
$insertQrySplit[] = "('$filename')";
to this:
$insertQrySplit[] = "(\'$filename\')";
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: http failure during parsing for
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.