1

I am trying to insert PHP variables in a mysql table, where the table name is also a variable, using mysqli_query. I've tried multiple solutions from stackoverflow but it still does not work.

I try to do it like this, maybe I am missing something. Thank you in advance!

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

?>

The other two queries work just fine.

The table structure is this:

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`");

11
  • 1
    Are you getting any error in it? Commented Oct 6, 2018 at 7:21
  • @Suresh, no, I don't get any error... Commented Oct 6, 2018 at 7:22
  • Share your few more lines of code to understand the problem. Commented Oct 6, 2018 at 7:23
  • Do you have error reporting set - stackoverflow.com/questions/22662488/… Commented Oct 6, 2018 at 7:24
  • @NigelRen, no, I will set it now and I will try again. Thank you Commented Oct 6, 2018 at 7:26

1 Answer 1

1

There is difference between table structure and insert column coumnt, When you want id column as auto incremented in that case column name should be included in insert query.

Please use the code as below:

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

?>
Sign up to request clarification or add additional context in comments.

1 Comment

@DanielS : Most welcome... Remember to use column name in insert query if you are skipping any column name from insert. Enjoy Coding ! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.