插入期间在数据库中获取空值

时间:2011-04-08 06:52:22

标签: php mysql

我有一个带有Veg,Non Veg和Senior Veg混乱选项的下拉列表框。一旦我选择了一种类型,相应的宿舍入学数量,学生姓名和天数(文本字段必须由显示用户)(来自注册表)。我希望显示的字段插入到一个名为'student_month'的表中。当我尝试插入时,我将空值插入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>";
    ?>

1 个答案:

答案 0 :(得分: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的语法突出显示来帮助您(请参阅上面的差异)。 ;)