This question already has an answer here:
So I made a form:
<div class="add">
<div class="add_student">
<h1>Add Student</h1>
<form action="backend/func/add/add_student.php" method="post">
<input type="text" name="student_id" placeholder="Student ID...">
<input type="text" name="first_name" placeholder="First Name...">
<input type="text" name="last_name" placeholder="Last Name...">
<button type="submit" name="Add"><i class="fa fa-plus-circle fa-lg" aria-hidden="true"></i>Add</button>
</form>
</div>
And for the first field I inputted 123, for the second I inputted test and for the last input I inputted test2. I got an error saying: Unknown column 'test' in 'field list'
. Can someone help me?
My PHP
<?php
// PROCESSES STUDENT INFO
// get connect page
require '../../connect.php';
// get input info
$student_id = $_POST['student_id'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
// check if input is not empy
if(!empty($student_id) && !empty($first_name) && !empty($last_name)) {
$result = mysqli_query($link, "INSERT INTO students (student_id, first_name, last_name)
VALUES ($student_id, $first_name, $last_name);");
if($result) {
header("Location: ../../../admin.php?message=Success!");
} else {
echo mysqli_error($link);
// header("Location: ../../../admin.php?message=Sorry we ran into an error");
}
} else if (empty($student_id) || empty($first_name) || empty($last_name)) {
header("Location: ../../../admin.php?message=Please add you're input values");
}
// header("Location: ../../../admin.php?message=Please add you're input values");
?>
</div>
you must quote fields that are strings:
$result = mysqli_query($link, "INSERT INTO students (student_id, first_name, last_name)
VALUES ($student_id, '$first_name', '$last_name');");