Reputation: 75
I'm a beginner and i have a few problems with my codes.
Every things seems to be ok with my codes because it doesn't give me any errors, but after saving, i don't get the data reflected in my database.
Here is the code:
<?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>
Please help!
Upvotes: 1
Views: 45
Reputation: 29
Your query seems wrong. Try this:
//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]');";
?>
Upvotes: -1
Reputation: 1827
First you need to call mysqli_query(). Mysqli_query($conn, $sqli)
Second, what is the table called ? is it election or registration ? and also remove the apostrophe inside first bracket. Also please convert your data into varables. Remove one of the semicolon at the end of your sql statement. It should look something like this:
$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);
}
Third, I would suggest avoid using capital letters as the name of your input and variables.
Fourth, I suggest you change to HTML5 format.
Upvotes: 1
Reputation: 521073
You need to actually execute the INSERT
statement, e.g.
$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;
}
Upvotes: 1