mysqli数据插入不起作用

时间:2015-06-12 18:12:48

标签: php mysqli

我创建了一个简单的MySQL连接来插入数据。连接没问题,但是当我提交表单数据时,INSERT无效。但是,如果我将手动数据放入查询中,它就可以正常工作。

<form action="dbconnection.php" method="POST">
    <label>Username</label>
    <input type="text" name="username" placeholder="Username" required=""/>
<label>Password</label>
<input type="text" name="password" required="" placeholder="Password"/>
    <input type="submit" value="save"/>
</form>

<?php
print_r($_POST);
$server = "localhost";
$dbname="mytestdb";
$username = "root";
$password= "";

//create connection
$conn = mysqli_connect($server,$username,$password,$dbname);


//check connection

if($conn->connect_error){
    die("connection failed". $conn->connect_error);
}

echo "connected successfull";

$username = $_POST['username'];
$password = $_POST['password'];

$sql = "INSERT INTO user('username','password') VALUES ($username,$password)";

mysqli_query($conn,$sql);

1 个答案:

答案 0 :(得分:0)

您在列名称周围使用单引号。将'改为'