我正在尝试创建一个网站,并且通过我的“创建新学生”#39;页面我试图使用从phpmyadmin生成的一些MySQL代码,当我插入一个新的测试行。但是,当我执行此代码时,它不会将其插入我的数据库。
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."')";
}
?>
</body>
</html>
答案 0 :(得分:3)
您忘了运行查询:
$query = "INSERT INTO `spelling`.`students` (`SID`, `fullname`, `CID`, `username`, `password`) VALUES (NULL, '".$fullname."', '1', '".$username."', '".$password."')";
$result = mysqli_query($connection, $query);
或同时检查错误。
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
同时删除
中的迷路;
exit("Database connection failed: " . mysqli_connect_error());}; // <=
将其更改为
if (mysqli_connect_errno()) {
exit("Database connection failed: " . mysqli_connect_error());
}