我有3个字符串,username
,email
和password
,它们是通过POST方法从三个文本框中获取的。那是我的代码,将这三个变量插入到我的表中:
$query = "SELECT * from accounts";
mysqli_query($db, $query) or die('Can't query database.');
$sql = "INSERT INTO accounts (username, email, password) VALUES ('$username', '$email', '$password')";
我的表结构: click
问题是,数据未插入数据库中。我也不会收到任何错误。我该怎么办?
答案 0 :(得分:-1)
正如@Jeff在评论中指出的那样,您需要正确地避免撇号出现can\'t
SQL查询是使用mysqli_query
函数执行的(对于单独的查询分别称为)
$query = "SELECT * from accounts";
mysqli_query($db, $query) or die('Can\'t query database.');
// ↑ Here $db carries `mysqli_connect` DB details and $query is your `SELECT` sql query
$sql = "INSERT INTO accounts (username, email, password) VALUES ('$username', '$email', '$password')";
// Another `mysqli_query` call is needed here to execute the ↑ `INSERT` query
mysqli_query($db, $sql) or die('Can\'t query database.');
// ↑ Here $db carries `mysqli_connect` DB details and $sql is your `INSERT` sql query