它保存时没有错误,但它仍然没有反映在我的数据库中


It saves without an error, but it still doesnt reflect in my database

我是一个初学者,我的代码有一些问题。

我的代码似乎一切都很好,因为它不会给我任何错误,但保存后,我的数据库中没有反映数据。

这是代码:

<?php 
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "election";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$sqli = "INSERT INTO `election`.`registration` (`Reg_Num`, `Full_Name`, `Level`, `Gender`) VALUES ('$_POST[Reg_Num]', '$_POST[Full_Name]', '$_POST[Level]', '$_POST[Gender]');";
?>  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Form</title>
</head>
<body>
<title>Form Submitted</title>
<p></p>
<p>Thanks for Registering</p>
<p></p>
<a href="vote.php">Register another student</a><br />
</body>
</html>

请帮忙!

您需要实际执行INSERT语句,例如

$sqli = "INSERT INTO `election`.`registration` (`Reg_Num`, `Full_Name`, `Level`, `Gender`) VALUES ('$_POST[Reg_Num]', '$_POST[Full_Name]', '$_POST[Level]', '$_POST[Gender]');";
if ($conn->query($sqli) === TRUE) {
    echo "insert succeeded";
} else {
    echo "Error: " . $conn->error;
}

首先需要调用mysqli_query()。Mysqli_query($conn,$sqli)

第二,这个桌子叫什么?是选举还是登记?并且还去除第一括号内的撇号。此外,请将您的数据转换为变量。删除sql语句末尾的一个分号。它应该看起来像这样:

$reg_num = $_POST['Reg_Num'];
$full_name = $_POST['Full_name'];
$level = $_POST['Level'];
$gender = $_POST['Gender'];
$sqli = "INSERT INTO election(Reg_Num, Full_Name, Level, Gender) VALUES ('$reg_num', '$full_name', '$level', '$gender')";
// Check if data inserted successfully
if(mysqli_query($conn, $sqli)) {
    echo 'Data successfully inserted';
} else {
    echo 'Error: ' . mysqli_error($conn);
}

第三,我建议避免使用大写字母作为输入和变量的名称。

第四,我建议你改成HTML5格式。

您的查询似乎是错误的。试试这个:

//insert into your table registration
// table name and column name should be without ''
$sqli = "INSERT INTO registration (Reg_Num, Full_Name, Level, Gender) VALUES('$_POST[Reg_Num]', '$_POST[Full_Name]', '$_POST[Level]', '$_POST[Gender]');";
?>