mysql:在变量名表中插入变量

lnlaulya  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(338)

我正在尝试使用mysqli\u query在mysql表中插入php变量,其中表名也是一个变量。我尝试了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`");
gwo2fgha

gwo2fgha1#

表结构和insert column coumnt之间有区别,当您希望id column自动递增时,在这种情况下,应该在insert查询中包含列名。
请使用以下代码:

<?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")));
}

?>

相关问题