使用php变量的表名不起作用...

时间:2015-02-14 21:56:52

标签: php mysqli

<?php
session_start();
$user='my username';
$pass='my password';
$db='my database';
$conn=new mysqli('mysql2.000webhost.com',$user,$pass,$db)or die('Unable to Connect');
$date = mysqli_real_escape_string($conn,$_POST["d1"]);
$amount = mysqli_real_escape_string($conn,$_POST["t1"]);
$tname=$_SESSION["tablename"];
$sql1="SELECT * FROM '{$tname}'";
$result=$conn->query($sql1);
$tbalance=0;
if ($result->num_rows > 0){
    while($row=$result->fetch_assoc()){
        echo($row["tamount"]);
        $tbalance=$tbalance+$row["balance"];
    }
}
$tbalance+=$amount;
echo($tname);
$sql2="INSERT INTO '$tname'(tdate,tamount,balance)VALUES('$date','$amount','$tbalance');
if($conn->query($sql2)){
echo("Sucessfully Inserted");
}
else{
echo("Failed");
}
?>

请帮帮我..我是php新手。我已经尝试了所有可能的查询构建语法,但没有改变。我有一个表,与我的会话变量值相同,我想访问或修改表。我必须做出什么改变?

0 个答案:

没有答案