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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器