尝试在php中使用INSERT时出错

时间:2016-12-12 20:24:23

标签: php mysql database mysqli mariadb

我正在尝试通过PHP连接到内部数据库。但是,在尝试插入某些内容时,它会出现错误,如下所示:

  

错误:INSERT INTO id329521_server1.us(name,eml,pwd,fct,mon)VALUES(用户名,emailadress @ gmail.com,123,NULL,'100')
你的SQL中有错误句法;检查与您的MariaDB服务器版本对应的手册,以便在第1行“@ gmail.com,123,NULL,'100')'附近使用正确的语法

我的代码如下所示:

        $name = $_POST["name"];
        $email = $_POST["email"];
        $pwd = $_POST["pwd"];

        $servername = "localhost";
        $username = "genericUsername";
        $password = "genericPassword";
        $dbname = "genericDBName";

        // Create connection
        if (!function_exists('mysqli_init') && !extension_loaded('mysqli')) {
            echo 'We don\'t have mysqli!!!';
        } else {
            $conn = new mysqli($servername, $username, $password, $dbname);
            // Check connection
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            } 
            echo "Connected successfully";

            $sql = "INSERT INTO genericTableName (name, eml, pwd, fct, mon) VALUES ($name, $email, $pwd, NULL, '100')";
            if ($conn->query($sql) === TRUE) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }
        }

0 个答案:

没有答案