dtdfl62844 2011-08-11 00:52
浏览 41
已采纳

通过PHP大量插入MySQL数据库会跳过名称

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("
", $_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("
", $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

  • 写回答

1条回答 默认 最新

  • doubeng9567 2011-08-11 02:41
    关注

    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");
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 找人不需要人工智能回答的gamit解算后做形变分析
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错