0

我一直在尝试通过 PHP 代码将我的表单数据导入数据库,但它不起作用,我已经查看了第一千次代码以查找可能的错误,但作为初学者却找不到。表单将实际提交,但没有任何内容进入数据库。

任何快速帮助将不胜感激。这是代码:

$conn = @mysqli_connect('localhost', 'root', 'aboki'); 
if (mysqli_connect_error()) {
die('Connect Error: ' . mysqli_connect_error());
}

$qry = "INSERT INTO users (email, firstName, surname, userName, password, birthday) values ($email, $firstName, $surname, $userName, $password, $userDOB)";
$result = mysqli_query($conn, $qry); 
4

4 回答 4

1

尝试这个

$qry = "INSERT INTO users (email, firstName, surname, userName, password, birthday) 
values ('$email', '$firstName', '$surname', '$userName', '$password', '$userDOB')";
于 2013-10-22T14:01:25.750 回答
1

首先,您没有引用值,这就是它没有插入的原因......

这将解决它(但我强烈建议您不要使用此方法!)

$qry = "INSERT INTO users (email, firstName, surname, userName, password, birthday) values ('$email', '$firstName', '$surname', '$userName', '$password', '$userDOB')";

正确的方法

您最好mysqli在准备好的语句中充分利用提供和绑定这些参数的预定义函数,如下所示:

mysqli_prepare($conn,"INSERT INTO users (email, firstName, surname, userName, password, birthday) values (?, ?, ?, ?, ?, ?)");

mysqli_stmt_bind_param($conn, 'TYPES_HERE',$email, $firstName, $surname, $userName, $password, $birthday)
于 2013-10-22T14:02:27.367 回答
0

我有数据插入的解决方案,你可以试试。

$conn= mysqli_connect("localhost", "root", "my_password", "world");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "INSERT INTO users 
(email, firstName, surname, userName, password, birthday) VALUES
($email, $firstName, $surname, $userName, $password, $userDOB)";
 mysqli_query($conn, $query);
 printf ("New Record has id %d.\n", mysqli_insert_id($link));
 mysqli_close($link);

当您在 Query 中使用 mysqli 时,语法完全不同,

随时问进一步的问题。

谢谢

于 2013-10-27T10:30:21.457 回答
-6

例子:

$stmt = mysqli_prepare($conn, "SELECT District FROM City WHERE Name=?")) {
$stmt->bind_param("s", $city);
$stmt->execute();
于 2013-10-22T14:00:35.500 回答