Hi,
I've never coded in ASP before and I'm trying to port a couple of simple PHP files to ASP.NET. The first file addScores.php takes form data and a hash and inserts the data into the db, it returns error or success depending on the success of the query. This is then used by a third party piece of software.
My first problem is how to retrieve the result of an insert query to see if the query is successful or not:
PHP Version:
My ASP so far:
I've not been able to test this code yet as I havn't been able to install MSSQL server, but the part which im unsure on is the IF statement for testing where a results is returned from the query, is this correct?
I'm trawling through the internet trying to find the right examples but this has got to be done for tomorrow so i'm hoping someone can lend a hand!
Thanks,
Chromis
I've never coded in ASP before and I'm trying to port a couple of simple PHP files to ASP.NET. The first file addScores.php takes form data and a hash and inserts the data into the db, it returns error or success depending on the success of the query. This is then used by a third party piece of software.
My first problem is how to retrieve the result of an insert query to see if the query is successful or not:
PHP Version:
Code:
// Set key vars
$key = md5("password");
// Validate Data
$name = htmlentities(addslashes($_GET['name']));
$email = htmlentities(addslashes($_GET['email']));
$time = urldecode($_GET['time']);
$milliseconds = $_GET['milliseconds'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql database.');
$dbname = 'test';
mysql_select_db($dbname);
// Build Query
$sql = "insert into scores set name='" . $name . "'"
. ", email='" . $email . "'"
. ", time='" . $time . "'"
. ", milliseconds='" . $milliseconds . "'";
// Execute and check result
if(strcasecmp($hash,$key) == 0)
{
if($result = @mysql_query($sql))
{
print "success: " . $name . " " . $time . ":" . $milliseconds;
}
else
{
print("Error: ". mysql_errno() . ". " . mysql_error() . "in sql: ". $sql);
}
}
else
{
print("Error: Incorrect hash.");
}
Code:
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!--#include file="md5.inc.asp"-->
<%
Dim myDataReader as SqlDataReader
Dim mySqlConnection as SqlConnection
Dim mySqlCommand as SqlCommand
Dim key = MD5("test")
Dim fName = request.querystring("name")
Dim fEmail = request.querystring("email")
Dim fTime = request.querystring("time")
Dim fMilliseconds = request.querystring("milliseconds")
Dim fHash = request.querystring("hash")
' Build Query
sqlStatement = "INSERT INTO scores SET name='" & fName & "'"
& ", email='" & fEmail & "'"
& ", time='" & fTime & "'"
& ", milliseconds='" & fMilliseconds & "'"
mySqlConnection = new SqlConnection("server=localhost;user=root;password=test;database=test")
mySqlCommand = new SqlCommand(sqlStatement, mySqlConnection)
mySqlConnection.Open()
myDataReader = mySqlCommand.ExecuteReader(CommandBehavior.CloseConnection)
If String.Compare(key, fHash) = 0 Then
If myDataReader.Read() then
Response.write("success")
Else
Response.write("error")
End If
Else
Response.write("error: hash not a match")
End If
' Always call Close when done reading.
myDataReader.Close()
' Close the connection when done with it.
mySqlConnection.Close()
%>
Code:
If myDataReader.Read() then
Thanks,
Chromis
Comment