我为学生创建了一份录取表,并尝试将信息保存到数据库中。我无法保存信息。请检查我在哪里做错了。
<?php
include 'authentication.php';
include 'includes/dbConnect.php';
$class = $_POST['class'];
$category = $_POST['category'];
$name = $_POST['name'];
$gender = $_POSt['gender'];
$date_of_birth = $_POST['date-of-birth'];
$mark_of_identification = $_POST['mark-of-identification'];
$religion = $_POST['religion'];
$father_name = $_POST['father-name'];
$occupation = $_POST['occupation'];
$army_no = $_POST['army-no'];
$rank = $_POST['rank'];
$regt = $_POST['regt'];
$unit = $_POST['unit'];
$contact = $_POST['contact'];
$guardian = $_POST['guardian'];
$guardian_occupation = $_POST['guardian-occupation'];
$mother = $_POST['mother'];
$mother_occupation = $_POST['mother-occupation'];
$present_adress = $_POST['present-adress'];
$permanent_adress = $_POST['permanent-adress'];
$school = $_POST['school'];
$registration_number = $_POST['registration-no'];
$exam = $_POST['exam'];
$ssc_school = $_POST['ssc-school'];
$ssc_year = $_POST['ssc-year'];
$ssc_total_marks = $_POST['ssc-total-marks'];
$ssc_marks = $_POST['ssc-marks'];
$ssc_grade = $_POST['ssc-grade'];
$ssc_percentage = $_POST['ssc-percentage'];
$ssc_sub = $_POST['ssc-sub'];
$exam2 = $_POST['exam2'];
$ssc_ii_school = $_POST['ssc-ii-school'];
$ssc_year_two = $_POST['ssc-year-two'];
$ssc_ii_total_marks = $_POST['ssc-ii-total-marks'];
$ssc_ii_marks = $_POST['ssc-ii-marks'];
$ssc_ii_grade = $_POST['ssc-ii-grade'];
$ssc_ii_percentage = $_POST['ssc-ii-percentage'];
$ssc_sub2 = $_POST['ssc-sub2'];
$exam3 = $_POST['exam3'];
$o_level_school = $_POST['o-level-school'];
$o_level = $_POST['o-level'];
$o_level_total_marks = $_POST['o-level-total-marks'];
$o_level_marks = $_POST['o-level-marks'];
$o_level_grade = $_POST['o-level-grade'];
$o_level_percentage = $_POST['o-level-percentage'];
$o_level_sub = $_POST['o-level-sub'];
$exam4 = $_POST['exam4'];
$hssc_school = $_POST['hssc-school'];
$hssc_year = $_POST['hssc-year'];
$hssc_total_marks = $_POST['o-level-marks'];
$hssc_marks = $_POST['hssc-marks'];
$hssc_grade = $_POST['hssc-grade'];
$hssc_percentage = $_POST['hssc-percentage'];
$hssc_sub = $POST['hssc-sub'];
$admission_number = $_POST['admission-number'];
$admission_date = $_POST['admission-date'];
$roll_number = $_POST['roll-number'];
$section = $_POST['section'];
$sql = mysql_query("INSERT INTO `school`.`students` (`S_No`, `Roll_No`, `Name`, `Father_Name`, `Class`, `Section`, `Gender`, `Mark_of_identification`, `Date_of_birth`, `Religion`, `Admission_date`, `name_of_last_school`, `Board_registration_number`, `student_category`) VALUES (NULL, '$roll_number', '$name', '$father_name', '$class', '$section', '$gender', '$mark_of_identification', '$date_of_birth', '$religion', '$admission_date', '$school', '$registration_number', '$category');") or die("SELECT Error: ".mysql_error());
$sql = mysql_query("INSERT INTO `school`.`parents` (`S_No`, `Roll_no`, `Father_name`, `Father_occupation`, `Army_number`, `Rank`, `Corps`, `Unit`, `Contact_number`, `Guardian_name`, `Guardian_occupation`, `Mother_name`, `Mother_occupation`, `Present_address`, `Permanent_address`) VALUES (NULL, '$roll_number', '$father_name', '$occupation', '$army_no', '$rank', '$regt', '$unit', '$contact', '$guardian', '$guardian_occupation', '$mother', '$mother_occupation', '$present_adress', '$permanent_adress');") or die("SELECT Error: ".mysql_error());
$sql = mysql_query("INSERT INTO `school`.`academic` (`S_No.`, `Exam`, `School`, `Year`, `Total_marks`, `Marks_obtained`, `Grade`, `Percentage`, `Major_subjects`, `Roll_no`) VALUES (NULL, '$exam', '$ssc_school '$ssc_year', '$ssc_total_marks', '$ssc_marks', '$ssc_grade', '$ssc_percentage', '$ssc_sub', '$roll_number';")or die("SELECT Error: ".mysql_error());
?>
我收到此错误:
&#34; SELECT Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '77'' at line 1
&#34;
请指导我做错的地方。提前致谢
答案 0 :(得分:1)
您的上一个查询有:
... VALUES (NULL, '$exam', '$ssc_school '$ssc_year', '$ssc_total_marks', ...
这是不正确的。它应该是:
... VALUES (NULL, '$exam', '$ssc_school', '$ssc_year', '$ssc_total_marks', ...
它也错过了右括号(正如其他人指出的那样)。
话虽如此:不再使用mysql_ *函数。他们是deprecated。请改为使用PDO或MySQLi与prepared statements。您目前对SQL injection attacks持开放态度。
答案 1 :(得分:1)
您的代码存在一些问题:
此'$ssc_sub', '$roll_number';")
缺少一个括号,应为'$roll_number');")
加$_POSt
t
中的$_POSt['gender'];
必须为大写$_POST
,它是superglobal。
此外,'$ssc_school '$ssc_year',
缺少引号和逗号,应该显示为'$ssc_school', '$ssc_year',
我也注意到你正在使用
(`S_No.`,
在一个查询中
(`S_No`,
在其他两个查询中。 S_No.
&lt; =中的点很可能不在那里。
如果它实际上是列的名称S_No.
,那么您需要为其他两个查询添加点。
在mysql_error
之上使用error reporting来表示代码中的错误。
error_reporting(E_ALL);
ini_set('display_errors', 1);
<强> 旁注: 强>
您目前的代码向SQL injection开放。使用mysqli
with prepared statements或PDO with prepared statements,他们更安全。
答案 2 :(得分:0)
您缺少值{/ 1>的结尾')'
$sql = mysql_query("INSERT INTO `school`.`academic` (`S_No.`, `Exam`, `School`, `Year`, `Total_marks`, `Marks_obtained`, `Grade`, `Percentage`, `Major_subjects`, `Roll_no`) VALUES (NULL, '$exam', '$ssc_school '$ssc_year', '$ssc_total_marks', '$ssc_marks', '$ssc_grade', '$ssc_percentage',
'$ssc_sub', '$roll_number';")or die("SELECT Error: ".mysql_error());
-------------------------^
尝试将其更改为
$sql = mysql_query("INSERT INTO `school`.`academic` (`S_No.`, `Exam`, `School`, `Year`, `Total_marks`, `Marks_obtained`, `Grade`, `Percentage`, `Major_subjects`, `Roll_no`)
VALUES (NULL, '$exam', '$ssc_school '$ssc_year', '$ssc_total_marks',
'$ssc_marks', '$ssc_grade', '$ssc_percentage', '$ssc_sub', '$roll_number');")
or die("SELECT Error: ".mysql_error());
也是巴特指出的
在$ ssc_school
之后的'
中,您错过了VALUES (NULL, '$exam', '$ssc_school
。
尝试添加
VALUES (NULL, '$exam', '$ssc_school'
答案 3 :(得分:-2)
更改您的查询并尝试编写如下
"INSERT INTO tbl (id, name) VALUES (" . $id . " , '" . $name . "')"