2011-04-08 21 views
0

我有一個下拉列表框,包括素食,非素食和高級素食混合選項。只要我選擇一種類型,相應的Hostel Admissionnumber,學生姓名和天數(Text field必須由用戶輸入)(從註冊表中)。我希望顯示的字段插入名爲'student_month'的表中。當我嘗試插入時,我將null值插入到student_month表中。我在下面粘貼了我的代碼,請稍後查看。任何幫助將不勝感激。非常感謝。在插入過程中獲取數據庫中的空值

<?php 
    $q=$_GET['messtype']; 
    echo "<html> 
    <head> 
    <form action='testbill.php' method='GET'> 
    <p><select name='messtype' id='Select1'> 
     <option value='1'>VEG</option> 
     <option value='2'>NON-VEG</option> 
     <option value='3'>SENIOR VEG MESS</option> 
    <p></select> </b> 
    <p><input type='submit'style='width:100;height:35' name='submit' value='Display details'> 
    </form>"; 

    print "<div id='txtHint' align='center'><b>List of Student Details</b></div>"; 

    if($q=='1') 
    { 

    $a=$_POST['hosteadmissionno']; 
    $b=$_POST['student_name']; 

    $con = mysql_connect('localhost', 'root', ''); 
    if (!$con) 
     { 
     die('Could not connect: ' . mysql_error()); 
     } 

    mysql_select_db("hostel", $con); 
    $a1="SELECT hosteladmissionno,student_name,semester FROM registration WHERE mess_type = 'VEG' AND status_flag=1"; 
    $a2="INSERT into student_month(hosteladmissionno,student_name) values('$a','$b')"; 
    $result = mysql_query($a1,$con); 
    $final=mysql_query($a2,$con); 
    echo "<table border='1' width=80%> 
    <tr> 
    <th width=5%> S.No</th> 
    <th width=10%>H.Admin No</th> 
    <th width=10%>Student Name</th> 
    <th width=5%>No of Days</th> 
    </tr>"; 
    $i=0; 
    while($row = mysql_fetch_array($result)) 
     { 
     $i=$i+1; 
     echo "<form action='testbill.php' method='POST'>"; 
     echo "<tr>"; 
     echo "<td align=center>" .$i."</td>"; 
     echo "<td size=10 align=center>" . $row['hosteladmissionno'] . "</td>"; 
     echo "<td size=35 align=center>" . $row['student_name'] . "</td>"; 
     echo "<td align=center>"."<input type='text' name='days_mess' size=2>".$row['days_mess']. "</td> "; 
     echo "</tr>"; 
     } 
    echo "</table>"; 

    mysql_close($con); 
    } 

    if($q=='2') 
    { 
    $con = mysql_connect('localhost', 'root', ''); 
    if (!$con) 
     { 
     die('Could not connect: ' . mysql_error()); 
     } 

    mysql_select_db("hostel", $con); 
    $a1="SELECT hosteladmissionno,student_name,semester FROM registration WHERE mess_type = 'NON-VEG' AND status_flag=1"; 
    $a2="INSERT into student_month(hosteladmissionno,student_name) values('$a','$b')"; 
    $result = mysql_query($a1,$con); 
    $final=mysql_query($a2,$con); 
    echo "<table border='1' width=80%> 
    <tr> 
    <th width=5%> S.No</th> 
    <th width=10%>H.Admin No</th> 
    <th width=10%>Student Name</th> 
    <th width=5%>No of Days</th> 
    </tr>"; 
    $i=0; 
    while($row = mysql_fetch_array($result)) 
     { 
     $i=$i+1; 
     echo "<form action='testbill.php' method='POST'>"; 
     echo "<tr>"; 
     echo "<td align=center>" .$i."</td>"; 
     echo "<td size=10 align=center>" . $row['hosteladmissionno'] . "</td>"; 
     echo "<td size=35 align=center>" . $row['student_name'] . "</td>"; 
     echo "<td align=center>"."<input type='text' name='days_mess' size=2>".$row['days_mess']. "</td> "; 
     echo "</tr>"; 
     } 
    echo "</table>"; 

    mysql_close($con); 
    } 
    if($q=='3') 
    { 
    $con = mysql_connect('localhost', 'root', ''); 
    if (!$con) 
     { 
     die('Could not connect: ' . mysql_error()); 
     } 

    mysql_select_db("hostel", $con); 
    $a1="SELECT hosteladmissionno,student_name,semester FROM registration WHERE mess_type = 'SENIOR-VEG-MESS' AND status_flag=1"; 
    $a2="INSERT into student_month(hosteladmissionno,student_name) values('$a','$b')"; 
    $result = mysql_query($a1,$con); 
    $final=mysql_query($a2,$con); 
    echo "<table border='1' width=80%> 
    <tr> 
    <th width=5%> S.No</th> 
    <th width=10%>H.Admin No</th> 
    <th width=10%>Student Name</th> 
    <th width=5%>No of Days</th> 
    </tr>"; 
    $i=0; 
    while($row = mysql_fetch_array($result)) 
     { 
     $i=$i+1; 
     echo "<form action='testbill.php' method='POST'>"; 
     echo "<tr>"; 
     echo "<td align=center>" .$i."</td>"; 
     echo "<td size=10 align=center>" . $row['hosteladmissionno'] . "</td>"; 
     echo "<td size=35 align=center>" . $row['student_name'] . "</td>"; 
     echo "<td align=center>"."<input type='text' name='days_mess' size=2>".$row['days_mess']. "</td> "; 
     echo "</tr>"; 
     } 
    echo "</table>"; 

    mysql_close($con); 

    } 



    if($result!='') 
    { 
    echo "<form action='testbill.php'> 
    <input type='submit' style='width:100;height:35'name='submit' value='calculate' /> </form>"; 
    } 

    print "</html>"; 
    ?> 

回答

0

變量 '$ a' 和 '$ B' 不是從查詢字符串轉義:

$a2="INSERT into student_month(hosteladmissionno,student_name) values('$a','$b')"; 

應該是:

$a2="INSERT into student_month(hosteladmissionno,student_name) values('".$a."','".$b."')"; 

使用IDE的語法高亮幫助你(見上面的區別)。 ;)

+0

感謝您的答覆,仍然得到空值。 – Coolbreeze 2011-04-08 07:12:15

+0

在這種情況下:默認調試序列:1.檢查查詢字符串(echo $ a1;)2.如果查詢每個都是正確的,您應該有一個PHPMyAdmin(-alike)數據庫管理系統來粘貼您的查詢。 3.它說查詢是否正確執行?檢查你的插入代碼。 – 2011-04-08 08:11:49