$name = mysqli_real_escape_string($connection, $_POST["name"]);
$surname = mysqli_real_escape_string($connection, $_POST["surname"]);
$username = mysqli_real_escape_string($connection, $_POST["username"]);
$email = mysqli_real_escape_string($connection, $_POST["email"]);
$pw1 = mysqli_real_escape_string($connection, $_POST["pw1"]);
$query = "INSERT INTO 'users' ('id','name', 'surname', 'username', 'email', 'password') VALUES (NULL,'$name', '$surname', '$username', '$email', '$pw1')";
$result = mysqli_query($connection, $query);
if(!$result){
echo ("fail");
}
我测试查询是否使用if(!$ result){echo(“fail”);}并且每次都回显失败并且每次都没有数据插入到数据库中!我检查了语法,我相信它是正确的......这可能是因为数据库“整理”了吗?
答案 0 :(得分:2)
您不应使用table
或field
名称的单引号。你必须使用一个Backtick(如``),它位于Esc键下或1键的左侧或Tab键的上侧。它应该看起来像:
$query = "INSERT INTO `users` (`id`, `name`, `surname`, `username`, `email`,
`password`) VALUES ('null', '$name', '$surname', '$username', '$email', '$pw1')";
或
$query = "INSERT INTO users (id, name, surname, username, email,
password) VALUES ('null', '$name', '$surname', '$username', '$email', '$pw1')";
注意:如果您的id
字段已设置为自动增量,则可以删除id
和值null
。因为id值会自动递增。
希望它会有所帮助。