当按下保存按钮时,如何从数据库中选择数据然后将其保存到同一数据库中的另一个表?

时间:2014-10-07 14:12:54

标签: php html mysql

我做了一个程序,其中用户可以生成生病学生的结果,让我们说从8月1日到1月1日。我也可以根据他们的性别和疾病对其进行排序。现在,我接下来要做的是让用户选择用户是否可以保存生成的结果。如果用户选择保存结果,则它将存储在同一数据库中的另一个表中,以用于生成统计信息。例如,有多少男/女生生病。或者有多少学生经历过同样的疾病。这是我的代码,试图将数据保存到另一个表但无济于事。

<form action="savetrial.php" method="POST"> 
  <input type="date" name="date1" value="<?php error_reporting(E_ALL ^ E_NOTICE);
  echo $_POST['date1'];?>"/> 
  <input type="date" name="date2" value="<?php error_reporting(E_ALL ^ E_NOTICE);
  echo $_POST['date2'];?>"/> 
  <select name="gender" value="<?php error_reporting(E_ALL ^ E_NOTICE);
  echo $_POST['gender'];?>">
    <option value="both">Both</option>
    <option value="male">Male</option>
    <option value="female">Female</option>
  </select>
  <select name="disease">
    <option value="all">All</option>
    <option value="colds">Colds</option>
    <option value="measles">Measles</option>
    <option value="chicken pox">Chicken Pox</option>
  </select>
  <input type="submit" name="generate" value="Generate">
  <input type="submit" name="save" value="Save">
</form>

<?php
error_reporting(E_ALL ^ E_NOTICE);

$con = mysqli_connect("localhost", "root", "", "sickstuds") or die("amasarry can't connect...");

if(isset($_POST['date1']) && isset($_POST['date2']) && isset($_POST['gender']) && 
isset($_POST['disease'])){
$date1 = $_POST['date1'];
$date2 = $_POST['date2'];
$gender = $_POST['gender'];
$disease = $_POST['disease'];


if($gender == 'male' || $gender == 'female') // 1 here is the value of the gender it might be 1  
or male depends on your form 
{

 if($disease == 'colds' || $disease == 'measles' || $disease == 'chicken pox'){

     $query = mysqli_query($con, "SELECT * FROM sickstud WHERE gender = '$gender' AND disease = 
'$disease' AND date >= '$date1' AND date <= '$date2'");
  $rows = mysqli_num_rows($query);
  if($rows > 0)
  {
    while($i = mysqli_fetch_assoc($query))
    {

     if($i['disease'] == $disease || $disease == 'all'){

       echo '

          <table border="1" cellpadding="20" cellspacing="5">
            <tr>
              <td>'.$i['id'].'</td>
              <td>'.$i['fn'].'</td>
              <td>'.$i['mn'].'</td>
              <td>'.$i['ln'].'</td>
              <td>'.$i['date'].'</td>
              <td>'.$i['program'].'</td>
              <td>'.$i['Gender'].'</td>
              <td>'.$i['disease'].'</td>
            </tr>
          </table>

        ';

     }

    }
  }
  else{
    echo 'No results found';
  }

 }

 else if($disease == 'all'){

       $query = mysqli_query($con, "SELECT * FROM sickstud WHERE gender = '$gender' OR disease = 
'$disease' AND date >= '$date1' AND date <= '$date2'");
  $rows = mysqli_num_rows($query);
  if($rows > 0)
  {
    while($i = mysqli_fetch_assoc($query))
    {

     if($i['disease'] == $disease || $disease == 'all'){

       echo '

          <table border="1" cellpadding="20" cellspacing="5">
            <tr>
              <td>'.$i['id'].'</td>
              <td>'.$i['fn'].'</td>
              <td>'.$i['mn'].'</td>
              <td>'.$i['ln'].'</td>
              <td>'.$i['date'].'</td>
              <td>'.$i['program'].'</td>
              <td>'.$i['Gender'].'</td>
              <td>'.$i['disease'].'</td>
            </tr>
          </table>

        ';

        $id = $i['id'];
        $fn = $i['fn'];
        $mn = $i['mn'];
        $ln = $i['ln'];
        $prog = $i['program'];
        $gen = $i['gender'];
        $dis = $i['disease'];

        //MY ATTEMPT TO SAVE SELECTED DATA TO ANOTHER TABLE  
        if(isset($_POST['save'])){
          $query2 = mysqli_query($con, "Insert into result VALUES ('$id', '$fn', '$mn', '$ln',        
'$date1' ,'$date2' ,'$prog' ,'$gen' ,'$dis')");
        }

     }

    }
  }
    else{
    echo 'No results found';
  }

 }


}

elseif($gender == 'both')
{
  $query = mysqli_query($con, "SELECT * FROM sickstud WHERE date >= '$date1' AND date <=       
'$date2'");
  $rows = mysqli_num_rows($query);
  if($rows > 0)
  {
    while($i = mysqli_fetch_assoc($query))
    {

      if($i['disease'] == $disease || $disease == 'all'){

       echo '

          <table border="1" cellpadding="20" cellspacing="5">
            <tr>
              <td>'.$i['id'].'</td>
              <td>'.$i['fn'].'</td>
              <td>'.$i['mn'].'</td>
              <td>'.$i['ln'].'</td>
              <td>'.$i['date'].'</td>
              <td>'.$i['program'].'</td>
              <td>'.$i['Gender'].'</td>
              <td>'.$i['disease'].'</td>
            </tr>
          </table>

        ';

     }

    }
  }
  else
  {
    echo 'No results found';
  }
}
}
?>

0 个答案:

没有答案