如何将所选数据从下拉菜单插入到数据库中的多个表中

时间:2014-09-01 00:43:07

标签: php mysql database-connection

这是我的register.php(表格):

<table width="350" border="0">
  <center><tr>
    <td><form action="newregister.php" method="post">
    Nama</td>
    <td> : </td>
    <td></td>
    <td width="200"><center><input name="nama_pelajar" type="text"></center></td>
  </tr>
  <tr>
    <td>No Kad Pengenalan</td>
    <td> : </td>
    <td></td>
    <td><center><input name="ic_pelajar" type="text"></center></td></tr>
  <tr>
    <td>ID Pelajar</td>
    <td> : </td>
    <td></td>
    <td><center><input name="id_pelajar" type="text"></td>
  </tr>
  <tr>
    <td><br>Sesi</td>
    <td> : </td>
    <td></td>
    <td><center><select name="sesi">
      <option><center>-Sila pilih-</option>
      <option value="Jun 14">Jun 2014</option>
      <option value="Dis 14">Dis 2014</option>
      <option value="Jun 15">Jun 2015</option>
      <option value="Dis 15">Dis 2015</option>
      <option value="Jun 16">Jun 2016</option>
      <option value="Dis 16">Dis 2016</option>
      <option value="Jun 17">Jun 2017</option>
      <option value="Dis 17">Dis 2017</option>
      </select>
     </td>
  </tr>
  <tr>
    <td><br>Kursus</td>
    <td> : </td>
    <td></td>
    <td><center><select name="kursus">
      <option>-Sila pilih-</option>
      <option value="Senibina">Kursus Lukisan Seni Bina</option>
      <option value="Elektrik">Kursus Pemasangan Elektrik</option>
      <option value="Fesyen">Kursus Fesyen &amp; Pakaian</option>
      <option value="Makanan">Kursus Pemprosesan Makanan</option>
    </select></td>
  </tr>
  <tr>
    <td><br>No Telefon</td>
    <td> : </td>
    <td></td>
    <td><center><input name="no_tel" type="text"></td>
  </tr>
  <tr>
    <td><br>Email</td>
    <td> : </td>
    <td></td>
    <td><center><input name="email" type="text"></td>
  </tr>
  <tr>
  <td><center><input name="" type="submit" value="Simpan">
  <input name="" type="reset" value="Set Semula">
  </center>
  </form>

  </td>
  </tr>

</table>

这是我的php代码(newregister.php):

<?php

require_once "conn.php";
    $conn = connect();
    $db = connectdb();
mysql_select_db($db,$conn) or die (mysql_error() . "\n");

$query_usr = "select * from register_jun14";
$usr = mysql_query($query_usr,$conn) or die(mysql_error()."\n".$query_usr);
$row_usr=mysql_fetch_assoc($usr);

$nama_pelajar=$_REQUEST["nama_pelajar"];
$ic_pelajar=$_REQUEST["ic_pelajar"];
$id_pelajar=$_REQUEST["id_pelajar"];
$sesi=$_REQUEST["sesi"];
$kursus=$_REQUEST["kursus"];
$no_tel=$_REQUEST["no_tel"];
$email=$_REQUEST["email"];

$query = "INSERT INTO register_jun14(nama_pelajar,ic_pelajar,id_pelajar,sesi,kursus,no_tel,email) VALUES ('$nama_pelajar','$ic_pelajar',
'$id_pelajar','$sesi','$kursus','$no_tel','$email')";
$result = mysql_query($query);

echo "<script languange = 'Javascript'>
                alert('Pendaftaran berjaya!');
                location.href = 'register.php';</script>";

?>

我的问题:

如果有“Jun14”,“Dis14”,“Jun15”,“Dis15”等表格,如何将选中的“sesi”(下拉菜单)插入数据库中的新表?

任何?

1 个答案:

答案 0 :(得分:1)

我假设您要根据此框中的下拉选项选择表格(表示您有多个表jun14,dis14,jun15等)

<td><center><select name="sesi">
      <option><center>-Sila pilih-</option>
      <option value="Jun 14">Jun 2014</option>
      <option value="Dis 14">Dis 2014</option>
      <option value="Jun 15">Jun 2015</option>
      <option value="Dis 15">Dis 2015</option>
      <option value="Jun 16">Jun 2016</option>
      <option value="Dis 16">Dis 2016</option>
      <option value="Jun 17">Jun 2017</option>
      <option value="Dis 17">Dis 2017</option>
      </select>
     </td>

首先你需要像这样改变上面的部分

//you should remove the space and add underscore in order for your query to work
//for the if statement
<td><center><select name="sesi">
      <option><center>-Sila pilih-</option>
      <option value="jun_14">Jun 2014</option>
      <option value="dec_14">Dis 2014</option>
      <option value="Jun_15">Jun 2015</option>
      <option value="Dis_15">Dis 2015</option>
      <option value="Jun_16">Jun 2016</option>
      <option value="Dis_16">Dis 2016</option>
      <option value="Jun_17">Jun 2017</option>
      <option value="Dis_17">Dis 2017</option>
      </select>
     </td>

我删除了值中的空格并添加了一个下划线,如jun 14变为jun_14,dec 14变为dec _14等等,

现在你的newregister php应该是这样的

 <?php
//i remove your this line assuming these lines you are connecting with your database these lines i removed
//require_once "conn.php";
 //$conn = connect();
 //   $db = connectdb();
//mysql_select_db($db,$conn) or die (mysql_error() . "\n");

        //and i added this line instead in this line i am also connecting 
//with database
    $conn=  mysqli_connect('localhost', 'root', 'yourpassword', 'yourdatabasename') 
or   die(mysqli_error($con));



    $query_usr = "select * from register_jun14";
    //$usr = mysql_query($query_usr,$conn) or die(mysql_error()."\n".$query_usr);

    $usr = mysqli_query($conn,$query_usr) or die(mysqli_error()."\n".$query_usr);
    $row_usr=mysqli_fetch_assoc($usr);

    $nama_pelajar=$_REQUEST["nama_pelajar"];
    $ic_pelajar=$_REQUEST["ic_pelajar"];
    $id_pelajar=$_REQUEST["id_pelajar"];
    $sesi=$_REQUEST["sesi"];
    $kursus=$_REQUEST["kursus"];
    $no_tel=$_REQUEST["no_tel"];
    $email=$_REQUEST["email"];


    if($sesi=="jun_14")
    {
    //assuming you have Jun14 table in your database with same field as your 
    //original  insert query in the question

        $query = "INSERT INTO  Jun14(id, nama_pelajar,ic_pelajar,id_pelajar,sesi,kursus,no_tel,email) VALUES ('','$nama_pelajar','$ic_pelajar',
    '$id_pelajar','$sesi','$kursus','$no_tel','$email')";
    $result = mysqli_query($conn,$query) or die(mysqli_error($conn));
    }

    if($sesi=="dec_14")
    {
    //assuming you have Dis14 table in your database with same field as your 
        //original insert query in the question

        $query = "INSERT INTO  Dis14(id, nama_pelajar,ic_pelajar,id_pelajar,sesi,kursus,no_tel,email) VALUES ('','$nama_pelajar','$ic_pelajar',
    '$id_pelajar','$sesi','$kursus','$no_tel','$email')";
    $result = mysqli_query($conn,$query) or die(mysqli_error($conn));
    }

if($sesi=="jun_15")
    {


//assuming you have Jun15 table in your database with same field as your 
//original insert query in the question
        $query = "INSERT INTO  Jun15(id, nama_pelajar,ic_pelajar,id_pelajar,sesi,kursus,no_tel,email) VALUES ('','$nama_pelajar','$ic_pelajar',
    '$id_pelajar','$sesi','$kursus','$no_tel','$email')";
    $result = mysqli_query($conn,$query) or die(mysqli_error($conn));
    }
    //i am leaving this your query here i dont know why you need it, may be you 
//need it 
    $query = "INSERT INTO register_jun14 ( nama_pelajar,ic_pelajar,id_pelajar,sesi,kursus,no_tel,email) VALUES ('$nama_pelajar','$ic_pelajar',
    '$id_pelajar','$sesi','$kursus','$no_tel','$email')";
    $result = mysqli_query($conn,$query) or die(mysqli_error($conn));

您可以根据下拉菜单中的值进一步编写if语句 您可以添加尽可能多的条件,以实现下拉菜单的目标,为什么您运行选择查询我不知道。