2014-10-02 169 views
0

我做了一個入學表格並嘗試將信息保存到數據庫中。它正在工作,但是在給出一些問題上是關於學術成績的一個部分。 Mysql INSERT INTO無法正常工作

<!----- 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()); 

上面的查詢工作正常。 我希望下面的查詢只有在字段被填充時才能工作,它是留空的,應該保存在數據庫中。

$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查詢應該只在它被填充時才工作,如果它被留空,其他信息應該被保存,並且在學術表中應該沒有信息。 在此先感謝

+2

首先,逃不過你的POST參數,防止MySQL的注入。爲什麼你在'$ mark_of_identification'之後使用分號] – Edd 2014-10-02 07:02:38

+0

_number');「)或死(」SELECT Error:「.mysql_error());刪除逗號前 – 2014-10-02 07:02:41

+0

'$ mark_of_identification');在打開'<?php'標記後立即報告文件頂部的錯誤報告(error_porting(E_ALL); ini_set('display_errors',1);' – 2014-10-02 07:04:59

回答

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()); 
} 
+0

如果$ exam =='' – 2014-10-02 07:17:32

+0

要寫什麼你只想在$檢查有任何值的情況下執行sql2嗎? – Shafeeque 2014-10-02 07:20:14

+0

是的,只有它有價值 – 2014-10-02 07:24:42