数据未插入MySQL数据库

时间:2018-10-21 07:50:34

标签: php mysql

我有3个字符串,usernameemailpassword,它们是通过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

问题是,数据未插入数据库中。我也不会收到任何错误。我该怎么办?

1 个答案:

答案 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