使用PHP $ variable进行MySQL INSERT INTO
问题描述:
我可以在$strSQL = "SELECT * FROM $person";
中使用 $ variable
但是我不能在$sql = "INSERT INTO $person . . .
I can use $variable there $strSQL = "SELECT * FROM $person";
But I can't use $variable there $sql = "INSERT INTO $person . . .
`$person`
也不要工作...
有什么区别?以及如何使用$ variable代替表名(INSERT INTO table_name )
What the difference? And how I can use $variable instead of name of the table (INSERT INTO table_name)
$sql = 'INSERT INTO $person (date, min, sec, count, temp, universal)
VALUES("'.$date.'", "'.$min.'", "'.$sec.'", "'.$count.'", "'.$temp.'", "'.$universal.'")';
if(!mysql_query($sql))
{echo '<center><p><b>ERROR!</b></p></center>';}
else
{echo '<center><p><b>Its okay</b></p></center>';}
}
解决:
mysql_query("INSERT INTO $person (date, min, sec, count, temp, universal) VALUES('$date', '$min', '$sec', '$count', '$temp', '$universal')") or die(mysql_error());
答
您可以这样使用
mysql_query("INSERT INTO `$person` VALUES(...) ")
or die(mysql_error());
您关闭了if和else错误
You have closed the if and else wrong
请尝试一下
$sql = 'INSERT INTO `name`(date, min, sec, count, temp, universal)
VALUES("'.$date.'", "'.$min.'", "'.$sec.'", "'.$count.'", "'.$temp.'", "'.$universal.'")';
$sql = 'INSERT INTO $person (date, min, sec, count, temp, universal)
VALUES("'.$date.'", "'.$min.'", "'.$sec.'", "'.$count.'", "'.$temp.'", "'.$universal.'")';
if(!mysql_query($sql)) {
echo '<center><p><b>ERROR!</b></p></center>';
}else{
echo '<center><p><b>Its okay</b></p></center>';
}
}// so where this comes from ?