我有一个网站,我想将新学生添加到拼写网站,但由于某种原因,添加学生的代码不会添加学生。我确定我的查询很好,因为我从phpMyAdmin生成的示例中复制了它。
connection.php
<?php
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASS", "root");
define("DB_NAME", "spelling");
$connection = new mysqli(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
if (mysqli_connect_errno()) {
exit("Database connection failed: " . mysqli_connect_error());};
?>
addstudent.php
<?php session_start();
require_once("connection.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>
Add a student
</title>
</head>
<body>
<form action="addstudent.php" method="post">
Full Name:
<input type="text" name="fullname" autocomplete="off">
<br />
Username:
<input type="text" name="username" autocomplete="off">
<br />
Password:
<input type="password" name="password" autocomplete="off">
<br />
<br />
<input type="submit" value="Add Student" name="submit">
</form>
<?php
if(isset($_POST["submit"]) ){
$_POST = array_map("strip_tags",$_POST);
$_POST = array_map("trim",$_POST);
$fullname = $_POST["fullname"];
$username = $_POST["username"];
$password = $_POST["password"];
$query = "INSERT INTO `spelling`.`students` (`SID`, `fullname`, `CID`, `username`, `password`) VALUES (NULL, '".$fullname."', '1', '".$username."', '".$password."')";
$result = mysqli_query($query, $connection);
}
?>
</body>
</html>
如果代码在语法上是正确的,我的问题可能在哪里?
答案 0 :(得分:2)
$result = mysqli_query($query, $connection);
您需要在此行上切换查询和连接。像这样:
$result = mysqli_query($connection, $query);
答案 1 :(得分:1)
问题出在$result = mysqli_query($query, $connection);
您切换 $ query和$ connection,see the reference
mysqli_query ( mysqli $link , string $query ... )
答案 2 :(得分:0)
我注意到您在一个看似是(主)键(SID)的列中输入NULL。也许这就是问题所在?