MYSQL:将变量插入变量名表

时间:2018-10-06 07:18:49

标签: php mysql

我正在尝试使用mysqli_query将PHP变量插入到mysql表中,其中表名也是变量。我已经尝试过stackoverflow的多种解决方案,但仍然无法正常工作。

我尝试这样做,也许我缺少一些东西。预先谢谢你!

<?php
session_start();
@include_once "modules/connections/dbconn.php";

$value = $_POST['value'];
$playerid = $_SESSION["steamid"];
$playername = fetchinfo("name","users","steamid",$playerid);
$playeravatar = fetchinfo("avatar","users","steamid",$playerid);
$playercoins = fetchinfo("coins", "users","steamid",$playerid);

if($playercoins - $value < 0){
    die(json_encode(array('message' => 'ERROR', 'code' => "Not enough coins!")));
}

$game = fetchinfo("value","parameters","name","raffleRound");
$maxitems = fetchinfo("value","parameters","name","raffleMaxritems");
$items = fetchinfo("itemsnum","rafflegames","id",$game);

$itemname = "Coins";
$itemavatar = "images/creditcardicon.png";
$color = "D2D2D2";

$initialvalue = fetchinfo("value","rafflegames","id",$game);
$from = $initialvalue * 100;
$to = $from + $value * 100;

$tablename = 'rafflegame'.$game;

if($items < $maxitems){
    mysqli_query($GLOBALS["connect"], "UPDATE rafflegames SET `value`=`value`+$value, `itemsnum`=`itemsnum`+1 WHERE `id`=$game");
    mysqli_query($GLOBALS["connect"], "UPDATE users SET `coins`=`coins`-$value WHERE `steamid`=$playerid");
    mysqli_query($GLOBALS["connect"], "INSERT INTO `" . $tablename . "` VALUES ('".$playerid."', '".$playername."','".$itemname."','".$color."','".$value."','".$playeravatar."','".$itemavatar."','".$from."','".$to."')");
}
else {
    die(json_encode(array('message' => 'ERROR', 'code' => "Too many items in the current game")));
}

?>

其他两个查询都可以正常工作。

表结构是这样:

mysqli_query($GLOBALS['connect'],"CREATE TABLE `rafflegame$roundNumber` (
  `id` int(11) NOT NULL auto_increment,
  `userid` varchar(70) NOT NULL,
  `username` varchar(70) NOT NULL,
  `item` text,
  `color` text,
  `value` float,
  `avatar` varchar(512) NOT NULL,
  `image` text NOT NULL,
  `from` int NOT NULL,
  `to` int NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;");
mysqli_query($GLOBALS['connect'],"TRUNCATE TABLE `rafflegame$roundNumber`");

1 个答案:

答案 0 :(得分:1)

表结构和插入列列之间有区别,在这种情况下,当您希望id列自动递增时,列名称应包含在插入查询中。

请使用以下代码:

<?php
session_start();
@include_once "modules/connections/dbconn.php";

$value = $_POST['value'];
$playerid = $_SESSION["steamid"];
$playername = fetchinfo("name","users","steamid",$playerid);
$playeravatar = fetchinfo("avatar","users","steamid",$playerid);
$playercoins = fetchinfo("coins", "users","steamid",$playerid);

if($playercoins - $value < 0){
    die(json_encode(array('message' => 'ERROR', 'code' => "Not enough coins!")));
}

$game = fetchinfo("value","parameters","name","raffleRound");
$maxitems = fetchinfo("value","parameters","name","raffleMaxritems");
$items = fetchinfo("itemsnum","rafflegames","id",$game);

$itemname = "Coins";
$itemavatar = "images/creditcardicon.png";
$color = "D2D2D2";

$initialvalue = fetchinfo("value","rafflegames","id",$game);
$from = $initialvalue * 100;
$to = $from + $value * 100;

$tablename = 'rafflegame'.$game;

if($items < $maxitems){
    mysqli_query($GLOBALS["connect"], "UPDATE rafflegames SET `value`=`value`+$value, `itemsnum`=`itemsnum`+1 WHERE `id`=$game");
    mysqli_query($GLOBALS["connect"], "UPDATE users SET `coins`=`coins`-$value WHERE `steamid`=$playerid");
    mysqli_query($GLOBALS["connect"], "INSERT INTO `" . $tablename . "`(`userid`,`username`,`item`,`color`,`value`,`avatar`,`image`,`from`,`to`) VALUES ('".$playerid."', '".$playername."','".$itemname."','".$color."','".$value."','".$playeravatar."','".$itemavatar."','".$from."','".$to."')");
}
else {
    die(json_encode(array('message' => 'ERROR', 'code' => "Too many items in the current game")));
}

?>