Mysql INSERT INTO无法正常工作

时间:2014-10-02 06:59:47

标签: php mysql sql

我制作了一份录取表格并试图将信息保存到数据库中。它正在发挥作用,但是有关于提出一些问题的学术记录。                    

    <!----- Admission ---------------------------------------------------------->
    <tr>
    <td width="180">Admission in Class / Group</td>
    <td width="492"><input name="class" type="text" size="30" id="class"/></td>
    </tr>
            <!----- Name ---------------------------------------------------------->
        <tr>
        <td width="180">Name </td>
        <td width="490"><input type="text" name="name" id="name" size="30" /></td>
        </tr>

        <!----- Gender ---------------------------------------------------------->
        <tr>
        <td>Gender</td>
        <td><input type="text" name="gender" id="gender" size="20" /></td>
        </tr>

        <!----- Date of Birth ---------------------------------------------------------->
        <tr>
        <td>Date of Birth</td>
        <td><input type="text" name="date-of-birth" id="date-of-birth" size="20" /></td>
        </tr>


        <!----- Mark of Identification -------------------------------------------------------->
        <tr>
        <td>Mark of Identification</td>
        <td><input type="text" name="mark-of-identification" id="mark-of-identification" size="30" /></td>
        </tr>

<!----- Academic Record -------------------------------------------------------->

<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
  <th width="9%">EXAM</th>
  <th width="11%">SCHOOL / COLLEGE</th>
  <th width="8%">YEAR</th>
  <th width="10%">TOTAL MARKS</th>
  <th width="11%">MARKS OBTAINED</th>
  <th width="12%">GRADE / DIV</th>
  <th width="15%">% OF MARKS</th>
  <th width="24%">MAJOR SUBJECTS</th>
 </tr>
 <tr>
  <td><input type="text" name="exam" id="ssc" size="7" /></td>
  <td><input type="text" name="ssc-school" id="ssc-school" size="10" /></td>
  <td><input name="ssc-year" type="text" id="ssc-year" value="" size="7" /></td>
  <td><input name="ssc-total-marks" type="text" id="ssc-total-marks" value="" size="5" /></td>
  <td><input name="ssc-marks" type="text" id="ssc-marks" value="" size="5" /></td>
  <td><input name="ssc-grade" type="text" id="ssc-grade" value="" size="5" /></td>
  <td><input name="ssc-percentage" type="text" id="ssc-percentage" value="" size="5" /></td>
  <td><input name="ssc-sub" type="text" id="ssc-sub" value="" size="10" /></td>
  </tr>
  <tr>
  <td><input type="text" name="exam2" id="ssc" size="7" /></td>
  <td><input type="text" name="ssc-ii-school" id="ssc-ii-school" size="10" /></td>
  <td><input name="ssc-year-two" type="text" id="ssc-year-two" value="" size="7" /></td>
  <td><input name="ssc-ii-total-marks" type="text" id="ssc-ii-total-marks" value="" size="5" />        </td>
  <td><input name="ssc-ii-marks" type="text" id="ssc-ii-marks" value="" size="5" /></td>
  <td><input name="ssc-ii-grade" type="text" id="ssc-ii-grade" value="" size="5" /></td>
  <td><input name="ssc-ii-percentage" type="text" id="ssc-ii-percentage" value="" size="5" /> </td>
  <td><input name="ssc-sub2" type="text" id="ssc-sub2" value="" size="10" /></td>
 </tr>
 <tr>
  <td><input type="text" name="exam3" id="ssc" size="7" /></td>
  <td><input type="text" name="o-level-school" id="o-level-school" size="10" /></td>
  <td><input name="o-level" type="text" id="o-level" value="" size="7" /></td>
  <td><input name="o-level-total-marks" type="text" id="o-level-total-marks" value="" size="5" /> </td>
  <td><input name="o-level-marks" type="text" id="o-level-marks" value="" size="5" /></td>
  <td><input name="o-level-grade" type="text" id="o-level-grade" value="" size="5" /></td>
  <td><input name="o-level-percentage" type="text" id="o-level-percentage" value="" size="5" /></td>
  <td><input name="o-level-sub" type="text" id="o-level-sub" value="" size="10" /></td>
 </tr>
 <tr>
   <td><input type="text" name="exam4" id="ssc" size="7" /></td>
  <td><input type="text" name="hssc-school" id="hssc-school" size="10" /></td>
  <td><input name="hssc-year" type="text" id="hssc-year" value="" size="7" /></td>
  <td><input name="hssc-total-marks" type="text" id="hssc-total-marks" value="" size="5" /></td>
  <td><input name="hssc-marks" type="text" id="hssc-marks" value="" size="5" /></td>
  <td><input name="hssc-grade" type="text" id="hssc-grade" value="" size="5" /></td>
  <td><input name="hssc-percentage" type="text" id="hssc-percentage" value="" size="5" /></td>
  <td><input name="hssc-sub" type="text" id="hssc-sub" value="" size="10" /></td>
  </tr>
  </table>

以上部分是录取表格的一部分。我希望如果表单中没有输入任何内容,则不应将任何内容保存在数据库中。如果用户输入一个或两个记录,则只保存一个或两个记录。

我正在尝试这个

do_admission.php:

$class = $_POST['class'];
$name = $_POST['name'];
$gender = $_POST['gender'];
$date_of_birth = $_POST['date-of-birth'];
$mark_of_identification = $_POST['mark-of-identification'];
$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'];

$sql1 = mysql_query("INSERT INTO `school`.`students` (`S_No`, `Name`, `Father_Name`, `Class`, `Gender`, `Mark_of_identification`, `Date_of_birth`) VALUES (NULL,  '$name', '$father_name', '$class', '$gender', '$mark_of_identification');") or die("SELECT Error: ".mysql_error());

以上查询工作正常。 我希望以下查询仅在字段为fill时才起作用,如果它保留为空,则不应保存在数据库中。

$sql2 = 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());

if($sql1 && $sql2) {
    $myURL = 'success.php?sType=insert';
    header('Location: '.$myURL);
    exit;
} else 
    echo "Try again!";

请指导我做哪些更改。 $ sql2查询只有在填充时才能使用,如果它被保留为空,则应该保存其他信息,并且在学术表中应该没有信息。 提前致谢

1 个答案:

答案 0 :(得分:1)

如果您要验证所有字段,则可以在插入前使用if

示例:

if($exam != "" && $ssc_school != "" && $ssc_year != "") {
    $sql2 = 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());
}