Reputation:
I'm currently working on a tracking program which tracks hundreds of users at once, but I've came across a somewhat annoying problem. The way I get the users to insert into the database is via an html textarea and then I send them as a parameter. There's always about 7 out of 60 people (it's always the same names, too) that are inserted into the database, but it doesn't insert the proper data. I've tried to just insert those users and it does it all fine, so could there be a problem inserting all of the people at once?
Here's some of the code that I'm using:
index.html:
<head>
<title>Test</title>
</head>
<body>
<form name="input" action="tracker.php" method="get">
<textarea name="names" rows="20" cols="20"></textarea><br>
<input type="submit" value="Submit" />
</form>
</body>
tracker.php:
$database = mysql_connect("mysql.alwaysdata.com", "stacktest", "stackoverflow");
if (!$database) {
die('Could not connect to database: ' . mysql_error());
}
mysql_select_db("stacktest_1", $database);
mysql_query("CREATE TABLE stats (username varchar(12), start text, UNIQUE (username))");
$names = explode("\n", $_GET['names']);
if (isset($_GET['track'])) {
for ($i = 0; $i < count($names); $i++) {
startTracker($names[$i]);
}
}
displayData();
function startTracker($username) {
$stats = getStats($username);
mysql_query("INSERT IGNORE INTO stats (username, start) VALUES ('" . trim($username) . "', '$stats')");
}
function grabStats($username) {
$query = mysql_query("SELECT * FROM stats WHERE username LIKE '$username'");
while ($row = mysql_fetch_array($query)) {
return $row['start'];
}
}
function displayData() {
global $names;
for ($i = 0; $i < count($names); $i++) {
$stats = getStats($names[$i]);
$starting = getStat(grabStats($names[$i]), 2, 0);
$current = getStat($stats, 2, 0);
$gained = $currentExperience - $startingExperience;
echo "Stats for <b>" . $names[$i] . "</b>:<br>";
echo "Starting experience: " . number_format($starting) . "<br>";
echo "Current experience: " . number_format($current) . "<br>";
echo "Gained experience: " . number_format($gained) . "<br><br>";
}
}
function getStats($username) {
$curl = curl_init("http://hiscore.runescape.com/index_lite.ws?player=" . $username);
curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt ($curl, CURLOPT_USERAGENT, sprintf("Mozilla/%d.0", rand(4, 5)));
curl_setopt ($curl, CURLOPT_HEADER, (int) $header);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($curl, CURLOPT_VERBOSE, 1);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$output = curl_exec($curl);
curl_close ($curl);
if (strstr($output, "<html><head><title>")) {
return false;
}
return $output;
}
function getStat($stats, $row, $skill) {
$stats = explode("\n", $stats);
$levels = explode(",", $stats[$skill]);
return $levels[$row];
}
mysql_close($database);
You'll notice inside the database that funkymunky11 has no data, yet there's clearly data from the site that I read from, as you can see.
I've also created a database just so you can see what's happening: http://phpmyadmin.alwaysdata.com/
Username: stacktest
Password: stackoverflow
The follow are the names that I used to test (I put this inside the text area):
aeterna
quuxx
funkymunky11
ts danne
I'm sorry if there's some unneeded code, I went through and cleaned almost everything that I didn't think was needed.
Thanks in advance, Ron
Upvotes: 3
Views: 414
Reputation: 262
I'm thinking you need to see which errors are cropping up on the insert command. Copy the following code to your tracker.php page and then change the call to startTracker so that it calls startTrackerDebug. Then see what you get. Note that in my function I use sprintf() and mysql_real_escape_string() to make sure the data plays nice with mysql when inserted.
function startTrackerDebug($username) {
$stats = getStats($username);
$query = sprintf("INSERT INTO stats (username, start) VALUES('%s', '%s')",
mysql_real_escape_string(trim($username)),
mysql_real_escape_string($stats));
$result = mysql_query($query);
if(!$result) echo(mysql_error() . "Query = $query");
}
Upvotes: 1