I tried reproducing your issue, and it seems that var_export is generating the backslash, because it simply wraps that output string with single quotation as follows:
var_export( $query);
Output:
'SELECT `Used`, `Date` FROM `volumes2` WHERE `Name` = \'test\''
But if you echo the variable instead:
$query = "SELECT `Used`, `Date` FROM `volumes2` WHERE `Name` = '".$name. "'";
echo $query;
This would be the output:
SELECT `Used`, `Date` FROM `volumes2` WHERE `Name` = 'test'
Which is exactly as expected. Another point that I noticed in your code is that you do not need to add the escape quotes (`), so the following query should do the job:
SELECT Used, Date FROM volumes2 WHERE Name = 'test';
I suggest to double check your query in either phpmyadmin or MySql Workbench before you use it in your code, just to make sure that nothing is missing.
I really don't see any problem with the jQuery part, and as a proof of concept implementation, the following snippet (test.php) should not generate any back-slashes when it executes the AJAX request and print the result out:
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
</head>
<body>
<?php
if (isset($_POST['volumeName'])){
$name = $_POST["volumeName"];
$query = "SELECT `Used`, `Date` FROM `volumes2` WHERE `Name` = '".$name. "'";
echo $name."<br>";
die();
}
?>
<select name="Volumes">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<div id="result">
</div>
<a href="javascript:submit_onclick()">Submit</a>
<script>
function submit_onclick()
{
$.ajax({
type: "POST",
url: "index.php",
data: {'volumeName': $('select[name="Volumes"] option:selected').val()},
success: function (results){
//console.log(results);
$("#result").html(results);
}
});
}
</script>
</body>
</html>
So try to:
- Remove the escape quotes (```) and see if your problem is fixed.
Wrap your query with double quotation (") instead of single quotation ('), and this way var_export will not have to spit out the backslash. Do it as follows:
$query = 'SELECT Used, Date FROM volumes2 WHERE Name = "'.$name. '"';
Best of luck
$nameand var_dump of$query's are escaped because the string is encapsulated by'Used,DateFROMtableWHEREName= '$name'"; produces the same thing.