如何将下拉菜单中的数据插入数据库中的新表?

时间:2014-09-01 17:01:25

标签: php mysql database-connection

假设我有一个包含3列的表格: 我从另一个表(Register)获得了第1列(Id Pelajar)和第2列(Nama Pelajar)的数据。 但是如何在下拉菜单(第3列)中将数据插入数据库? 我想将第1列,第2列和第3列中的所有数据插入到数据库中的新表(penilai)中。

这是我在penilai.php编码:

<?php
session_start();
require_once "conn.php";
    $conn = connect();
    $db = connectdb();

mysql_select_db($db,$conn) or die (mysql_error() . "\n");
$query_usr = "select * from register"; //to get data from register table into column 1 and 2
$usr = mysql_query($query_usr,$conn) or die(mysql_error()."\n".$query_usr);
$row_usr=mysql_fetch_assoc($usr);
?>
<!DOCTYPE html>
<table width="400" border="0">
  <center><tr>
    <td><form action="newpenilai.php" method="post">
    <table width=500 border='1'>
    <tr>
    <td>Id Pelajar</td>
    <td>Nama Pelajar</td>
    <td>Nama Penilai</td>

    </tr>

<?php
require_once "conn.php";
    $conn = connect();
    $db = connectdb();

mysql_select_db($db,$conn) or die (mysql_error() . "\n");

$query_usr ="SELECT * FROM register";//to get data from register table into column 1 and 2
$usr = mysql_query($query_usr,$conn) or die(mysql_error()."\n".$query_usr);
$row_usr=mysql_fetch_assoc($usr);

$result=mysql_query($query_usr);

    if($result)
{
    while ($row=mysql_fetch_assoc ($result))
    {
    ?>

    <input name="id_pelajar" type="text" value="<?php echo $row ["id_pelajar"];?>"/>

    <input name="nama_pelajar" type="text" value="<?php echo $row ["nama_pelajar"];?>"/>
    <select name="nama_penilai">
    <option>-Sila pilih-</option>
      <option value="En Badrul Hisham">En Badrul Hisham</option>
      <option value="En Kamarul Zaman">En Kamarul Zaman</option>
      <option value="Pn Hasnita Ahmad">Pn Hasnita Ahmad</option>
      <option value="Pn Hazfa Amani">Pn Hazfa Amani</option>
      <option value="Pn Zalika Lutfi">Pn Zalika Lutfi</option>
    </select>

<?php }}?>

<center>
<input name="save" type="submit" value="Simpan">
<input name="Reset" type="reset" value="Set Semula">

    </form>

这是我的PHP代码,用于将数据保存到数据库中的新表中:

<?php
require_once "conn.php";
    $conn = connect();
    $db = connectdb();

mysql_select_db($db,$conn) or die (mysql_error() . "\n");

$query_usr ="SELECT * FROM penilai";//new table
$usr = mysql_query($query_usr,$conn) or die(mysql_error()."\n".$query_usr);
$row_usr=mysql_fetch_assoc($usr);

$nama_penilai=$_REQUEST["nama_penilai"];
$id_pelajar=$_REQUEST["id_pelajar"];
$nama_pelajar=$_REQUEST["nama_pelajar"];
$result=mysql_query($query_usr);

$query = "INSERT INTO penilai(nama_penilai,id_pelajar,nama_pelajar)VALUES('$nama_penilai','$id_pelajar','$nama_pelajar')";
$result = mysql_query($query);

echo "<script languange = 'Javascript'>
                alert('Data  telah dimasukkan!');
                location.href = 'view-penilai.php';</script>";
?>

任何?

2 个答案:

答案 0 :(得分:1)

相信我的评论两个文件都很小,所以不要害怕

FOR penilai.php

     <?php
session_start();

// I change the line below and replace it with one line. Because 
// I think you are only connecting with the database in those lines.

/*
 * require_once "conn.php";
    $conn = connect();
    $db = connectdb();
 * 
 */

//This the line i replace with, you need to put password and database 
// name in there
$conn=  mysqli_connect('localhost', 'root', 'YourPassword', 'YourDataBaseName') 
        or die(mysqli_error($conn));

//you dont need these lines so i completly removed them. Reason you 
//running the query, fetching it but not using it any where, and you 
//are running the same query again. you dont have to do it twice in same 
//file. So i removed them

//These are the lines i removed

/*
 * mysql_select_db($db,$conn) or die (mysql_error() . "\n");
$query_usr = "select * from register"; //to get data from register table into column 1 and 2
$usr = mysql_query($query_usr,$conn) or die(mysql_error()."\n".$query_usr);
$row_usr=mysql_fetch_assoc($usr);
 * 
 */
?>

<!DOCTYPE html>
<table width="400" border="0">
  <center><tr>
          <!-- Your form tag should not be here but i am 
          leaving it here. Because id does not make any different.
          Its better you place it out side of table this below line form one'
          if you don't does not matter as its not important-->

          <td><form action="newpenilai.php" method="post">
    <table width=500 border='1'>
    <tr>
    <td>Id Pelajar</td>
    <td>Nama Pelajar</td>
    <td>Nama Penilai</td>

    </tr>


<?php

//Again i removed some of the lines, reason in those lines you are 
//connecting with your database, you already connected with your database
//in this file you dont have to do it twice

//These are lines i removed

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


$query_usr="select * from `register`";
$result=  mysqli_query($conn, $query_usr) or die(mysqli_error($conn));

    if($result)
{
    while ($row=mysqli_fetch_assoc ($result))
    {
    ?>
    <tr><!--This <tr> i also added you don't have to if you don't want-->

        <!-- Below  is the important first step, as per your  requirement
        you want to insert all data into database, so we gonna store the value 
        in the array. 
        In order to store them in the array, we just simply
        place [] this brackets in the name attributes. 

        Like if you notice in the input field name="id_pelajar" become 
        name="id_pelajar[]", another input field name="nama_pelajar" 
        become name="nama_pelajar[]" And in the select name, 
        name="nama_penilai" become name="nama_penilai[]", 

        So we simply added [] these brackets in every name now this going 
        to store value in the array.-->

        <!--minor changes i added <td> for each input field, and select
        feild, its only minor changes if you don't want you don't have
        to do it. I am posting one original input lines from you question
        so you understand where i added <td> for each field
        <input name="id_pelajar[]" type="text" value="<?php //echo $row ["id_pelajar"];?>"/>-->

        <td><input name="id_pelajar[]" type="text" value="<?php echo $row ["id_pelajar"];?>"/></td>
        <td><input name="nama_pelajar[]" type="text" value="<?php echo $row ["nama_pelajar"];?>"/></td>

        <td><select name="nama_penilai[]">
    <option>-Sila pilih-</option>
      <option value="En Badrul Hisham">En Badrul Hisham</option>
      <option value="En Kamarul Zaman">En Kamarul Zaman</option>
      <option value="Pn Hasnita Ahmad">Pn Hasnita Ahmad</option>
      <option value="Pn Hazfa Amani">Pn Hazfa Amani</option>
      <option value="Pn Zalika Lutfi">Pn Zalika Lutfi</option>
            </select></td>
    </tr><!--This <tr> i also added you don't have to if you don't want-->
<?php }} ?>
</table>
<center>
<input name="save" type="submit" value="Simpan">
<input name="Reset" type="reset" value="Set Semula">

    </form>

FOR newpenilai.php

           <?php
    //Again i removed these lines as you are connecting with your 
    //database.
    //These are the lines i removed
    /*
     * require_once "conn.php";
        $conn = connect();
        $db = connectdb();
     * mysql_select_db($db,$conn) or die (mysql_error() . "\n");
     */

    //Instead i added these line same again here, you need to 
    //give your database name and password.
    $conn=  mysqli_connect('localhost', 'root', 'YourPassword', 'YourDatabaseName')
 or die(mysqli_error($conn));

    //The lines below i completly removed, same reason again as i describe 
    //in penilai.php file you are running the select query not using it
    //anywhere so whats the point.

    //These are the lines i removed

    /*
     * $query_usr ="SELECT * FROM penilai";//new table
    $usr = mysql_query($query_usr,$conn) or die(mysql_error()."\n".$query_usr);
    $row_usr=mysql_fetch_assoc($usr);
     */

    //Now these lines below are very important, because you want all data, 
    //so we store our value, in the array in penilai.php, by adding [], this 
    // in the name attributes, so now we can run the array. and insert all 
    //value into database, i used 'nama_penilai' this feild but you can
    //use any feild like id_pelajar or nama_pelajar, it does not really 
    //matter
    for($i=0; $i<count($_POST['nama_penilai']);$i++){
    $nama_penilai=$_POST["nama_penilai"][$i];
    $id_pelajar=$_POST["id_pelajar"][$i];
    $nama_pelajar=$_POST["nama_pelajar"][$i];

    $query = "INSERT INTO penilai(nama_penilai,id_pelajar,nama_pelajar)VALUES
('$nama_penilai','$id_pelajar','$nama_pelajar')";
    $result = mysqli_query($conn,$query) or die(mysqli_error($conn));
    }

答案 1 :(得分:1)

for penilai.php

<?php
session_start();
$conn=  mysqli_connect('localhost', 'root', 'YourPassword', 'YourDatabaseName') or die(mysqli_error($conn));
?>
<form action="original_newpenilai.php" method="post">
    <table width=500 border='1'>
    <tr>
    <td>Id Pelajar</td>
    <td>Nama Pelajar</td>
    <td>Nama Penilai</td>
    </tr>
    <tr>
<?php
$query_usr="select * from `register`";
$result=  mysqli_query($conn, $query_usr) or die(mysqli_error($conn));
if($result)
{
    while ($row=mysqli_fetch_assoc ($result))
    {
    ?>

    <td><input name="id_pelajar[]" type="text" value="<?php echo $row ["id_pelajar"];?>"/></td>
    <td><input name="nama_pelajar[]" type="text" value="<?php echo $row ["nama_pelajar"];?>"/></td>
    <?php $drop_value = $row ["nama_penilai[]"]; 
    echo '<td><select name="nama_penilai[]">';
    echo '<option>-Sila pilih-</option>';

    if($drop_value = 'En Badrul Hisham'){
      echo '<option value="En Badrul Hisham" selected="selected">En Badrul Hisham</option>
            <option value="En Kamarul Zaman">En Kamarul Zaman</option>
            <option value="Pn Hasnita Ahmad">Pn Hasnita Ahmad</option>
            <option value="Pn Hazfa Amani">Pn Hazfa Amani</option>
            <option value="Pn Zalika Lutfi">Pn Zalika Lutfi</option>';
     }else if($drop_value = 'En Kamarul Zaman'){
      echo '<option value="En Badrul Hisham">En Badrul Hisham</option>
            <option value="En Kamarul Zaman" selected="selected">En Kamarul Zaman</option>
            <option value="Pn Hasnita Ahmad">Pn Hasnita Ahmad</option>
            <option value="Pn Hazfa Amani">Pn Hazfa Amani</option>
            <option value="Pn Zalika Lutfi">Pn Zalika Lutfi</option>'; 
        }
     else if($drop_value = 'Pn Hasnita Ahmad'){
      echo '<option value="En Badrul Hisham">En Badrul Hisham</option>
            <option value="En Kamarul Zaman">En Kamarul Zaman</option>
            <option value="Pn Hasnita Ahmad" selected="selected">Pn Hasnita Ahmad</option>
            <option value="Pn Hazfa Amani">Pn Hazfa Amani</option>
            <option value="Pn Zalika Lutfi">Pn Zalika Lutfi</option>';
        }
     else if($drop_value = 'Pn Hazfa Amani'){
      echo '<option value="En Badrul Hisham">En Badrul Hisham</option>
            <option value="En Kamarul Zaman">En Kamarul Zaman</option>
            <option value="Pn Hasnita Ahmad">Pn Hasnita Ahmad</option>
            <option value="Pn Hazfa Amani" selected="selected">Pn Hazfa Amani</option>
            <option value="Pn Zalika Lutfi">Pn Zalika Lutfi</option>';
        }
     else {
      echo '<option value="En Badrul Hisham">En Badrul Hisham</option>
            <option value="En Kamarul Zaman">En Kamarul Zaman</option>
            <option value="Pn Hasnita Ahmad">Pn Hasnita Ahmad</option>
            <option value="Pn Hazfa Amani">Pn Hazfa Amani</option>
            <option value="Pn Zalika Lutfi" selected="selected">Pn Zalika Lutfi</option>';
        }
     ?>
    </select>
    </td>

<?php }} ?>
    <tr>
    <tr>
    <td><input name="save" type="submit" value="Simpan"></td>
    <td><input name="Reset" type="reset" value="Set Semula"></td>
    </td>
   </table>
   </form>