Jump to content

Recommended Posts

Hi,

OK, it's all about a game I'm creating in VB6, and I want to give the player the possibility to submit from within the application his/her score on internet.

 

What I have so far is a MySQL setup database on my server with a table named 'scores'. I also have the attached PHP code. When I run it from the browser everything seems to work fine and one record is added to the table:

 

<? 

$username="******"; 
$password="******"; 
$database="******"; 
$name="John Smith"; 
$score=10234; 

    // Connect to database 
    mysql_connect ("***************" , $username, $password)  or die ("Cannot connect to database" ); 
    mysql_select_db($database) or die ("Cannot open database" ); 
    mysql_query ("INSERT INTO scores (Name,Score) VALUES ('$name','$score')"); 
    mysql_close(); 

?> 

 

However, the problem occurs when I try to run it from VB6 application with the following code:

 

Option Explicit

Private Sub cmdSubmitScore_Click()
    
    Dim strResponse As String
    Dim playerName As String
    Dim Score As Long
    
    playerName = "John Smith"
    Score = 1548
    
    strResponse = Inet1.OpenURL("http://www.mywebsite.com/submit_score.php?name=" & playerName & "&score=" & Score)
    If InStr(1, strResponse, "score submitted!", vbTextCompare) > 0 Then
        MsgBox "Your score has been submitted!", vbInformation
    Else
        MsgBox "Error submitting score!", vbExclamation
    End If
    
End Sub

 

I keep getting the "Error submitting score!"

I'm not very good at PHP syntax nor at using Inet control, so all I can do now is to wait for your kind help...  :confused:

Thanks!

 

Link to comment
https://forums.phpfreaks.com/topic/169324-solved-php-vb6-help-needed/
Share on other sites

I've never used VB but I can tell what you're mistakes are. Your VB script is calling the following url

 

http://www.mywebsite.com/submit_score.php?name=some_name_here&score=some_score_here

 

Now in submit_score.php you are not retrieving the variables set with in the url.To retrieve name and score url variables you'll need to use the $_GET superglobal, like so $_GET['name'] and $_GET['score'].

 

You'll want to use these variables within your query. Like so

if(isset($_GET['name'], $_GET['score']))
{
    $name = mysql_real_escape_string($_GET['name']);
    $score = (int) $_GET['score'];

    $result = mysql_query ("INSERT INTO scores (Name,Score) VALUES ('$name','$score')");  
}

 

Now your script will insert the values your VB script pass to score_submit.php

 

However there is one more isse The VB script is expecting some respone from the script. It is expecting the string score submitted! to be returned.

 

To do this change

$result = mysql_query ("INSERT INTO scores (Name,Score) VALUES ('$name','$score')");  

 

to

$result = mysql_query ("INSERT INTO scores (Name,Score) VALUES ('$name','$score')");  

if($result)
    echo 'score submitted!';

 

throp, wildteen88, thank you for your reply.

 

wildteen88, this is how my PHP code looks after your suggestions:

 

<? 

$username="******"; 
$password="******"; 
$database="******"; 
//$name="John Smith"; 
//$score=10234; 

    // Connect to database 
    mysql_connect ("***************" , $username, $password)  or die ("Cannot connect to database" ); 
    mysql_select_db($database) or die ("Cannot open database" ); 

    if(isset($_GET['name'], $_GET['score']))
{
    $name = mysql_real_escape_string($_GET['name']);
    $score = (int) $_GET['score'];

    $result = mysql_query ("INSERT INTO scores (Name,Score) VALUES ('$name','$score')");  
}

if($result)
    echo 'score submitted!';

mysql_close(); 

?> 

 

Unfortunately, it's not working for me - the same old problem (and when run

from the browser no record is added to the table.

Could you please review the code to see if there's any problem with it?

Thanks.

 

 

When running your script you need to be going to the following url

 

mywebsite.com/submit_score.php?name=some_name_here&score=some_score_here

 

In order for a record to be inserted into the database.

 

Change this in your VB script

MsgBox "Error submitting score!", vbExclamation

 

To

 

MsgBox "Error submitting score! " & vbNewLine & "Response Text = strResponse" & strResponse, vbExclamation

 

What does your VB script output now?

 

wildteen88, thank you very much for your great help.

Everything is working fine now. Before it wasn't working because I removed something from the VB script and forgot to put it back - sorry for the false alarm.

 

As you see I added 3 more variables: city, country and date. I made some changes to the PHP script,

and all is working fine except the date variable (it declared as Date in the database but displays a string of zeros).

 

<? 

$username="******"; 
$password="******"; 
$database="******"; 

    // Connect to database 
    mysql_connect ("***************" , $username, $password)  or die ("Cannot connect to database" ); 
    mysql_select_db($database) or die ("Cannot open database" ); 

    if(isset($_GET['name'], $_GET['score']))
{
$name = mysql_real_escape_string($_GET['name']);
$city = mysql_real_escape_string($_GET['city']);
$country = mysql_real_escape_string($_GET['country']);
$score = (int) $_GET['score'];
$date = mysql_real_escape_string($_GET['date']);

    $result = mysql_query ("INSERT INTO scores (Name,City,Country,Score,Date) VALUES ('$name','$city','$country','$score','$date')");  
}

if($result)
    echo 'score submitted!';

mysql_close(); 

?> 

 

I also tried to changed the line:

 

if(isset($_GET['name'], $_GET['score']))

 

to:

 

if(isset($_GET['name'], ($_GET['city'],($_GET['country'],$_GET['score'],($_GET['date']))

 

but it didn't work so I left it as it is.

 

I will be thankful if you could review the PHP script once again.

I also tried to changed the line:

 

if(isset($_GET['name'], $_GET['score']))

 

to:

 

if(isset($_GET['name'], ($_GET['city'],($_GET['country'],$_GET['score'],($_GET['date']))

 

but it didn't work so I left it as it is.

 

Should be

if(isset($_GET['name'], $_GET['city'], $_GET['country'], $_GET['score'], $_GET['date']))

 

... except the date variable (it declared as Date in the database but displays a string of zeros).

What is the format of your date? When setting the data type to date for your date column MySQL is expecting the following format yyyy-mm-dd. Or if its datetime then the expected format is yyyy-mm-dd hh:mm:ss

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.