Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将下拉菜单中的数据插入数据库中的新表中?_Php_Mysql_Database Connection - Fatal编程技术网

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

Php 如何将下拉菜单中的数据插入数据库中的新表中?,php,mysql,database-connection,Php,Mysql,Database Connection,假设我有一个包含3列的表: 我从另一个表(寄存器)中获得了第1列(Id Pelajar)和第2列(Nama Pelajar)中的数据。 但如何将下拉菜单(第3列)中的数据插入数据库? 我想将第1列、第2列和第3列中的所有数据插入数据库中的新表(penilai) 这是我在penilai.php中的代码: <?php session_start(); require_once "conn.php"; $conn = connect(); $db = connectdb();

假设我有一个包含3列的表: 我从另一个表(寄存器)中获得了第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
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>";
?>

佩拉贾酒店
纳马·佩拉贾
纳玛·佩尼莱

相信我,没有注释,两个文件都很小,所以不要害怕

用于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>
           <?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));
    }

佩拉贾酒店
纳马·佩拉贾
纳玛·佩尼莱
用于newpenilai.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>
           <?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));
    }
penilai.php的


佩拉贾酒店
纳马·佩拉贾
纳玛·佩尼莱

您已经将表单的方法定义为method=“post”,因此要从输入字段中获取值,您将使用$\u post['nama\u penilai'],…只保存了一行。我在第1列和第2列中有多个数据。所以我想为第3列插入多个数据。请帮帮我,我抓不到你!是否要在“选择”菜单中获取大厅选项?选择是一个选择列表,您可以选择一个值或多个值(如果您想获得多个值,请在选择列表中添加多个值)…您能告诉我您的电子邮件地址吗?我想通过电子邮件发送照片。希望你能帮我,没关系,梅·萨吉拉。我有办法了。顺便说一句,谢谢你的关心:)我非常感谢并很高兴加入这个从未让我失望的论坛。感谢那些即使我是新手也愿意帮助我的人。我所有的问题都解决了!!非常感谢!你的答案无效。但我已经找到了解决办法。感谢您的关注:)