简体   繁体   English

MySQL $插入PHP $ variable

[英]MySQL INSERT INTO with PHP $variable

I can use $variable there $strSQL = "SELECT * FROM $person"; 我可以在其中使用$ variable $strSQL = "SELECT * FROM $person"; But I can't use $variable there $sql = "INSERT INTO $person . . . 但是我不能在那儿使用$ variable $sql = "INSERT INTO $person . . .

`$person` 

Don't working too... 也不要工作

What the difference? 有什么区别? And how I can use $variable instead of name of the table (INSERT INTO table_name ) 以及如何使用$ variable代替表名(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>';}
        }

Solve: 解决:

mysql_query("INSERT INTO $person (date, min, sec, count, temp, universal) VALUES('$date', '$min', '$sec', '$count', '$temp', '$universal')") or die(mysql_error());

You may use like this 您可以这样使用

mysql_query("INSERT INTO `$person` VALUES(...) ")
or die(mysql_error());

You have closed the if and else wrong 您关闭了if和else错误

just you may try this 只是你可以试试这个

$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 ?

Maybe with something like this : 也许是这样的:

$query = mysqli_query($connect, "INSERT INTO `$person` VALUES(...) ")
or die(mysql_error());

I like to use sprintf for this stuff. 我喜欢将sprintf用于此类内容。

$stmt = $pdo->prepare(sprintf("INSERT INTO %s VALUES(...)", $person));
$stmt->execute();

what is the value of your $person variable? 您的$person变量的值是多少?

if it's value is a good and an existing table name into your database, no problem, you can try: 如果值是一个好的值,并且数据库中已有表名,那么没问题,您可以尝试:

$strSQL = "SELECT * FROM {$person}";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM