0

我正在尝试制作每周抽奖系统。这就是我想要它做的事情。每周一个 cron 作业将运行“cron.php”,它将从数据库中随机选择一个用户。

一旦这样做,它将使用 JSONAPI 在 Minecraft 服务器上为他们提供一个项目。之后,它会在主网站上发布一条新闻状态,上面写着“gratz”或其他什么。我可以自己做所有这些事情,但我对如何使用一个 PHP 文件完成这一切感到困惑。

这是我到目前为止所拥有的:

<?php
//Selects a random entry from the database and gives them something in game and also says Congradulations on the website as a news post under the name "TCCraft". 

$mysqli = new mysqli("localhost", "uname", "pass", "db with usernames in it"); 
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
} 

if ($result = $mysqli->query("SELECT id, name FROM users ORDER BY RAND() LIMIT 1")) { //selects random user from the database.
    $row = mysqli_fetch_array($result);
    $name = $row['name']; //$name stands for the randomly selected user.
    //now its time to run the code to give the player the reward. We are using JSONAPI http://dev.bukkit.org/bukkit-plugins/jsonapi/
    require('JSONAPI.php');
        $obj = new JSONAPI('IP', 20059, 'unam', 'pass', 535153);
        $result = $obj->call("givePlayerItem", array("{$name}, 264, 24"));
    $time = date("Y-m-d H:i:s");
    //connect to news db
    $con = mysqli_connect("localhost", "uname", "pass", "db for all the news posts");
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
    $sql="INSERT INTO posts (title, user, body, date) VALUES ('Raffle Winner!','TCCraft','<p>Congradulations to {$name} for winning our weekly raffle!</p><p>If you would like to enter the raffle click <a href='http://tccraft.net/enter.php'>here</a></p>','$time')";
    echo "should be added";
}
?>

我测试了这段代码并没有出错,但它也没有工作大声笑真正的问题只是将新闻帖子添加到新闻数据库中。

4

2 回答 2

0

两个问题:

首先,您只需将INSERT查询字符串分配给一个变量。你需要做:

$mysqli->query($sql);

其次,查询中的引用问题。您需要转义href标记周围的引号,否则它们将被解释为分隔VALUES字符串。

    $sql="INSERT INTO posts (title, user, body, date) VALUES ('Raffle Winner!','TCCraft','<p>Congradulations to {$name} for winning our weekly raffle!</p><p>If you would like to enter the raffle click <a href=\\'http://tccraft.net/enter.php\\'>here</a></p>','$time')";
于 2013-06-30T09:22:19.640 回答
0

您只是分配了一个包含sql语句的变量,但没有运行它...添加$res = mysqli_query($sql);$sql="INSERT INTO ...";

于 2013-06-30T09:24:32.960 回答