Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

1
Javascript XMLHTTPRequest to PHP - Cant insert into table
Post Flair (click to view more posts with a particular flair)
Post Body

So this is my first big step into php and I am somewhat stuck.

I keep getting:

Error: INSERT INTO `MyTable` (`buildInfo`) VALUES ('["name1", "name2", "name3"]') ON DUPLICATE KEY UPDATE<br>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 '' at line 1

but if I run this on mySql server it runs fine:

INSERT INTO `MyTable` (`buildInfo`)
VALUES ('["name1", "name2", "name3"]')

this is my PHP:

<?php
$servername = "oatney.cauldron.dreamhost.com";
$username = "Me";
$password = "MyPassword";
$dbname = "Database_Name";

$conn = new mysqli($servername, $username, $password, $dbname); // Create connection
if ($conn->connect_error) {     // Check connection
die("Connection failed: " . $conn->connect_error);
} 

$build = mysqli_real_escape_string($conn, $_POST['build']);

if (strlen($times) > 200000) {  $times = "";    }

$sql = "INSERT INTO `MyTable` (`buildInfo`) VALUES ('[\"name1\", \"name2\", \"name3\"]') ON DUPLICATE KEY UPDATE";

if ($conn->query($sql) === TRUE) {
echo "Page saved!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

and this is my JS call:

saveBuild('SaveBuild.php', '{"age": 30, "city": "New York", "name": "John"}').then(function(response) {
console.log(response);
});

function saveBuild(url, build){

 return new Promise(function(resolve, reject) {
// Do the usual XHR stuff
var req = new XMLHttpRequest();

req.open('POST', url, true);
req.setRequestHeader('Content-Type', 'application/json;charset=UTF-8'); //-Name and value, one row for each

req.onload = function() {
  // This is called even on 404 etc
  // so check the status
  if (req.status == 200) {
    // Resolve the promise with the response text
    resolve(req.response);
  }
  else {
    // Otherwise reject with the status text
    // which will hopefully be a meaningful error
    reject(Error(req.statusText));
  }
};



// Handle network errors
req.onerror = function() {
  reject(Error("Network Error"));
};

// Make the request
req.send('build=aaa'   JSON.stringify({"age": "30", "city": "New York", "name": "John"}));
  });
}

Any suggestions as to what I am doing wrong?

Author
Account Strength
100%
Account Age
13 years
Verified Email
Yes
Verified Flair
No
Total Karma
26,663
Link Karma
8,214
Comment Karma
18,430
Profile updated: 4 days ago
Posts updated: 8 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
3 years ago