我正在尝试制作每周一次的抽奖系统。这是我希望它做的。每周一个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";
}
?>
我测试了这段代码并且没有错误,但它也没有工作lol真正的问题是将新闻帖添加到新闻数据库。
答案 0 :(得分: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')";
答案 1 :(得分:0)
你只是指定一个包含sql语句的变量,但是没有运行它...
在$res = mysqli_query($sql);
$sql="INSERT INTO ...";