无法获取mysqli查询以将值插入mysql

时间:2012-11-09 05:00:50

标签: php mysql

我是mysql查询的新手,并且无法将此mysqli语句插入到mysql中。我的脚本设置或我的$ sql有什么问题吗? (我没有收到任何错误,它只是没有插入)

    <?php

$login = "coolkidz";  //this line insets into username
$sname = "heyu";
$city = "beautiful";
$empass = "awesome";
$eusername = "what";
$mcode = "nice";


$sql = "INSERT INTO login(sname, city, empass, eusername, mcode, username) VALUES('$sname', '$city', '$empass', '$eusername', '$mcode', '$login')");

    $hostname_Database = "censored";
    $database_Database = "censored";
    $username_Database = "censored";
    $password_Database = "censored";

    $mysqli = new mysqli($hostname_Database, $username_Database, $password_Database, $database_Database); 
    if (mysqli_connect_errno()) {
       printf("Connect failed: %s\n", mysqli_connect_error());
       exit();
    }

    $result = $mysqli->query($sql);
    if (!$result) {
       printf("%s\n", $mysqli->error);
       exit();
    }
    ?>

2 个答案:

答案 0 :(得分:0)

你有这一行:

$sql = "INSERT INTO login(sname, city, empass, eusername, mcode, username) VALUES('$sname', '$city', '$empass', '$eusername', '$mcode', '$login')");

尝试将其更改为以下内容:

$sql = "INSERT INTO login(`sname`, `city`, `empass`, `eusername`, `mcode`, `username`) VALUES('$sname', '$city', '$empass', '$eusername', '$mcode', '$login')";

请注意列号中的`,

你在行尾有一个)

答案 1 :(得分:0)

Please update "..'$var'..." to  "..'" . $var . "'..." like ex:
=>
$sql = "INSERT INTO login(sname, city, empass, eusername, mcode, username) 
     VALUES('" . $sname . "', '" . $city . "', '" . $empass . "', '"
     . $eusername . "','" . $mcode . "', '" . $login . "')";