0

I want to send scores obtained in an educational PowerPoint game to an online mysql database.

My question is, is this possible to do?

I can see that it looks possible when using Excel VBA, but I would be grateful to know can this be done using PowerPoint VBA and if so how should I approach this?

Thank you for any comments.

EDIT - I thought I would update here as the code can be set out nicely compared to the comment boxes. I have experimented with the below code and have found that I can post to my database, but the problem is that only blank values are being inserted in the database.

Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")

URL = "http:mydomain/AddNew.php?"
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "User-Agent", "Mozilla / 4.0 ( compatible; MSIE 6.0; Windows NT 5.0); "
    objHTTP.send "name=test,&score=50"

I would be grateful for any advice on the correct syntax to use for the name and score to be inserted into my database. Also, how do I send the data using variables?

Thank you very much for any comments.

EDIT - a commenter said that I need to include my php code (use in AddNew.php). So I have copied this in below.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="en-gb" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>

<?php


$name=$_POST['name'];
$score=$_POST['score'];
echo '$name is '.$name;
echo '$score is '.$score;

if( $_REQUEST["name"] || $_REQUEST["score"] ) {
  echo "name ". $_REQUEST['name']. "<br />";
  $name = $_REQUEST['name'];
  echo "Your score ". $_REQUEST['score']. " points.";
  $score = $_REQUEST['score'];
  //exit();
}


if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // collect value of input field
  $name = $_POST['name'];
  if (empty($name)) {
    echo "Name is empty";
  } else {
  echo $name;
}
$score = $_POST['score'];
  if (empty($score)) {
  echo "Score is empty";
  } else {
  echo $score;
  }
}

$servername = "my server name";
$username = "my username";
$password = "my password";
$dbname = "my database name";


try {
$conn = new PDO("mysql:host=$servername;dbname=mydatabasename", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}

try {
$conn = new PDO("mysql:host=$servername;dbname=mydatabasename", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO mytablename (id, name, score)
VALUES ('1', '$name', '$score')";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}


 $conn = null;
?>

</body>
</html>

EDIT - another edit! Below is the result of the objHTTP.ResponseText. Looking at the 3rd line from the bottom you can see that my AddNew.php is not receiving anything for the name and score!

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="en-gb" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>

$name is $score is Name is emptyScore is emptyConnected successfullyNew record created successfully
</body>
</html>
Alec Armstrong
  • 655
  • 1
  • 5
  • 10
  • If you can wrap the db inside an HTTP endpoint: https://stackoverflow.com/questions/17063550/equivalent-curl-in-vba – pintxo Aug 13 '20 at 20:50
  • Yes very possible - plenty of examples to be had online and also here on SO. There's no real difference between doing this in Excel/PPT/Word or any other VBA host. https://stackoverflow.com/search?q=%5Bvba%5D+POST – Tim Williams Aug 13 '20 at 20:55
  • Yes - I have been experimenting with some code and it looks like it is very possible to do a Post request using PowerPoint VBA. At the moment I can connect to my database but only blank values are being entered into the database so I would appreciate some help concerning the syntax of sending data. – Alec Armstrong Aug 13 '20 at 21:33
  • So far I am using the following code: Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP") URL = "mywebsiteaddress/AddNew.php" objHTTP.Open "POST", URL, False objHTTP.setRequestHeader "User-Agent", "Mozilla / 4.0 ( compatible; MSIE 6.0; Windows NT 5.0); " objHTTP.send "id = 1 & name = test & score = 50" – Alec Armstrong Aug 13 '20 at 21:34
  • However, the current problem is that the objhttp.send is not sending in data which my database correctly interprets as I intend. I need to send in an id number (which is an integer and this value does not matter), send in a name which is a string and then a score which is an integer. I need the name and score to be sent as a variable that has been created in other parts of the program. My question is, how do I send in variable names and is there anything wrong with my syntax for the objHTTP.send? – Alec Armstrong Aug 13 '20 at 21:38
  • Difficult to know what the issue is without seeing your PHP code also. Your POST data shouldn't have commas or spaces though `objHTTP.send "name=test&score=50"` – Tim Williams Aug 13 '20 at 23:35
  • Tim Williams - thank you for posting. I shall post the php code at the top. – Alec Armstrong Aug 13 '20 at 23:52
  • I note that I removed the spaces, but still get the same result of a blank database entry. – Alec Armstrong Aug 14 '20 at 00:06
  • What response do you get back from the POST? `Debug.Print objHTTP.ResponseText` – Tim Williams Aug 14 '20 at 01:00
  • Tim Williams - thank you for your latest help. I did a Debug.Print objHTTP.ResponseText and have placed the result in my original question above. From this you can see that AddNew.php is not receiving anything for the values of the name and score. So, surely there is something wrong with what I have sent within the objHTTP.send? – Alec Armstrong Aug 14 '20 at 13:54

1 Answers1

0

Good news - I found a solution - I needed to change my setRequestHeader which I believe specifies that I am using a form request and I also separately worked out how to send variables. The below code should be as follows:

'below are some dummy variables to test and send
Dim name As String
Dim score As Integer
name = "John Doe"
score = 43

Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")

URL = "my URL/AddNew.php?"
objHTTP.Open "POST", URL, False
'objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" 'I do not seem to need this line!
objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"

objHTTP.send ("name=" + name + "&score=" + CStr(score))
Alec Armstrong
  • 655
  • 1
  • 5
  • 10
  • I note that after posting on this thread someone "compromised" my website and deleted the files. I think it is quite likely that it was someone who read this thread and has some very clever skills. Please do not do this. – Alec Armstrong Aug 14 '20 at 16:14