简体   繁体   English

使用php将刮取的变量插入到mysql表中

[英]Insert scraped variable into mysql table with php

I have been looking to make a very simple database, which has 4 columns.我一直在寻找一个非常简单的数据库,它有 4 列。 First is a local int that auto incriments, 2nd is a single digit ID int, 3rd is another INT and fourth is the scraped variable (string).第一个是自动增加的本地 int,第二个是单个数字 ID int,第三个是另一个 INT,第四个是刮取的变量(字符串)。 Problem I am having however is with the actual inserting of the 3 variables.然而,我遇到的问题是实际插入 3 个变量。

I have the $charIDs variable increase by 1 at the end of a for loop, which is used in the url I am scraping.我在 for 循环结束时将$charIDs变量增加 1,该变量用于我正在抓取的 url 中。 the $server variable is just a static int, and the $trimmedName variable is the actual text I scrape from each web page and it works great. $server变量只是一个静态整数,而$trimmedName变量是我从每个网页上抓取的实际文本,而且效果很好。

for($i = 1; $i <= 5; $i++){
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "http://website.com/asddas/asda/search?=$charIDs");
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);

$content = curl_exec($ch);

$dom = new DOMDocument();
$dom->loadHTML($content);

$xpath = new DOMXPath($dom);

$tags = $xpath->query('//*[@id="div_contents"]/div[2]/div[2]/div[2]/div[1]/dl/dd/span/text()');
foreach ($tags as $tag) {
    trim($name = $tag->nodeValue);
}
if ($name != null){

$trimmedName = trim($name);

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO players (serverID, charID, charName)
VALUES ('$server', '$charIDs', '$trimmedName')";

$conn->close();
$name = null;
$trimmedName = null;
}
$charIDs++;
}

I've checked variables, table names, etc and I am not sure why it isn't working.我检查了变量、表名等,但不确定为什么它不起作用。 I am able to insert rows manually into the database.我能够手动将行插入到数据库中。

Here are some things I have noticed about your script.以下是我注意到的关于你的脚本的一些事情。

1) You are not executing the query, this is the big problem. 1)您没有执行查询,这是大问题。
2) You should still be using parametrized queries. 2)您应该仍然使用参数化查询。
3) Why bother recreating the mysqli and statement objects on each iteration. 3) 为什么要在每次迭代中重新创建 mysqli 和 statement 对象。

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO players (serverID, charID, charName) VALUES (?,?,?)";
$stmt = $conn->prepare($sql);

if (!$stmt) {
    die('could not create statement: ' . $conn->error);
}

for($i = 1; $i <= 5; $i++){
    // curl and XML stuff here
    $binded = $stmt->bind_param('iis', $server, $charIDs, $trimmedName);
    if ($binded) {
        echo ($stmt->execute() ? 'Inserted' : 'Did not insert');
    }
    $charIDs++;
}

$conn->close();

This seems fairly obvious, but I don't see anywhere where you execute the SQL statement.这看起来很明显,但我没有看到您在何处执行 SQL 语句。 I can't comment on how safe this is as you don't provide what comprises the variables.我无法评论这有多安全,因为您没有提供包含变量的内容。 Hopefully you're incorporating best practices for your data希望您正在为您的数据整合最佳实践

$sql = "INSERT INTO players (serverID, charID, charName)
VALUES ('$server', '$charIDs', '$trimmedName')";
$conn->query($sql);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM