我如何使用mysql结果中的数据作为表名

时间:2015-08-21 07:34:08

标签: php mysql

我知道这段代码会很糟糕但是有没有办法在另一个查询中使用mysqli_fetch_assoc的结果作为表名?

这就是我所拥有的。请有人告诉我,我做错了什么

<?php 
   //this is where i set up the $_SESSION['userId']
   $result = mysqli_query($acctCon,"SELECT id FROM users WHERE email='$safeEmail'");
   $assoc = mysqli_fetch_assoc($result);
   $_SESSION["userId"] = $assoc["id"];

   // this is what im trying to fix
   $safePost = mysqli_real_escape_string($db,$_POST['post']);
   $user = $_SESSION['userId'];
   mysqli_query($db,"INSERT INTO $user (status) VALUES ('$safePost')");
?>

有人可以帮助我吗?,它让我疯狂。

1 个答案:

答案 0 :(得分:0)

您正在创建会话,同时您正在使用它。它不会起作用。在插入查询之前使用此行:$user = $assoc["id"];

请使用此代码:

<?php 
   //this is where i set up the $_SESSION['userId']
$result = mysqli_query($acctCon, "SELECT id FROM users WHERE    email='$safeEmail'");
$assoc = mysqli_fetch_assoc($result);
$_SESSION["userId"] = $assoc["id"];
$user = $assoc["id"];
// this is what im trying to fix
$safePost = mysqli_real_escape_string($db, $_POST['post']);
mysqli_query($db, "INSERT INTO $user (status) VALUES ('$safePost')");
?>