php - undefined index 从 HTML 表单中获取值时

标签 php html mysql

我在执行以下代码时遇到 undefined index 错误。

我已经从数据库表 tbl_usr_training_master 中获取了运行时的数据,我想将这个表字段添加到另一个名为 tbl_usr_training_master2 的表中。此外,我在运行时使用另一个页面的 $_GET 方法获取第一个表 ID。以下代码无法按我的意愿工作,您的帮助更加合适

以下是 PHP 代码,我已经尝试了两种方法,1. INSERT INTO 和 2. UPDATE Table。


$eid = $_GET['rid'];
$query = "Select * from tbl_usr_training_master where trai_master_id = '$eid';";

$rs = mysqli_query($con, $query);
$row = mysqli_fetch_array($rs);

$trainingID  = $row['trai_master_id']; //Tbl_usr_training_master PRIMARY KEY ID
$TrainerName = $row['trai_name'];
$TrainerExp = $row['trai_expertise'];
$TrainingTitle = $row ['tari_program_title'];
$TrainingDate = $row['trai_date'];
$TrainingDays = $row['t_days'];
$TrainingVenue = $row['Venue'];
$TrainingCity = $row['City'];
$TrainingPartOrg = $row ['trai_partner_organization'];
$TrainingCat = $row['trai_type_cata'];
$TrainingSubCat = $row['trai_type_subcata'];


if(isset($_POST['ButtonPost']))
{
    //$TrainerName="";
    $trainingID  = $_POST['id']; //Tbl_usr_training_master PRIMARY KEY ID
    $Trainer_Name = $_POST['trainer_name'];
    $TrainerExp = $_POST['TrainerExp'];
    $TrainingTitle = $_POST['Trainingtitle'];
    $TrainingDate = $_POST['TrainingDate'];
    $TrainingDays = $_POST['TrainingDays'];
    $TrainingVenue = $_POST['Venue'];
    $TrainingCity = $_POST['City'];
    $TrainingPartOrg = $_POST['PartnerOrg'];
    $TrainingCat = $_POST['TrainingCat'];
    $TrainingSubCat = $_POST['traningsubcat'];

    $query = "INSERT INTO tbl_usr_training_master2 
(trai_master_id, trai_name,trai_expertise,tari_program_title,trai_date,t_days,Venue,City,trai_partner_organization,trai_type_cata, trai_type_subcata)
VALUES
('$trainingID','$TrainerName','$TrainerExp','$TrainingTitle','$TrainingDate','$TrainingDays','$TrainingVenue','$TrainingCity',
'$TrainingPartOrg','$TrainingCat','$TrainingSubCat');";


   /* $query = "UPDATE tbl_usr_training_master2 SET
        trai_master_id = '$trainingID',
        trai_name = '$Trainer_Name',
        trai_expertise = '$TrainerExp',
        tari_program_title = '$TrainingTitle',
        trai_date = '$TrainingDate',
        t_days = '$TrainingDays',
        trai_date = '$TrainingDate',
        Venue = '$TrainingVenue',
        City = '$TrainingCity',
        trai_partner_organization = '$TrainingPartOrg',
        trai_type_cata = '$TrainingCat',
        trai_type_subcata = '$TrainingSubCat'
        WHERE trai_master_id='$trainingID'"; */

    $rs = mysqli_query($con, $query);

}
$result = mysqli_multi_query($con, $query);
if ($result != 1)
{
    //die(mysqli_error($con));
    echo "Error: " . $query . "<br>" . mysqli_error($con);

}

我的 HTML 代码,我想获取值并插入到表中



 <form role="form" name="frmreg" id="frmreg" action="" method="post" onclick="validate();" enctype="multipart/form-data">
  <div align="center">
  <table class="table table-bordered" width="108%" height="162" border="1" align="center">
    <tr>
      <th colspan="4" scope="row" bgcolor="#999999"><div align="center">Post Event Budget Form  <br />
        </div></th>
      </tr>


      <tr>
          <th scope="row" style="font-size:10px" align="left">Pre Budget Event Form ID</th>
          <th colspan="3" align="left" bgcolor="#B8D8D8" style="font-size:10px" scope="row">
              <input type="text" required="required" Value="<?php echo $trainingID;?>" name="id" id="id" disabled>
          </th>
      </tr>

      <tr>
      <th width="74" scope="row"  style="font-size:10px" align="left">Training Program / Event Title</th>
      <th colspan="3" scope="row" bgcolor="#B8D8D8" align="left">
        <input type="text" required="required" Value="<?php echo $TrainingTitle;?>" placeholder='"TOPIC"' name="Trainingtitle" id="Trainingtitle" width="auto" disabled/>
        </th>
      </tr>
    <tr>
      <th scope="row" style="font-size:10px" align="left">Trainer Experties</th>
      <th colspan="3" align="left" bgcolor="#B8D8D8" style="font-size:10px" scope="row">
        <select name="TrainerExp" class="form-control" required="required" required = "required" id="TrainerExp" disabled >
        <option selected ><?php echo $TrainerExp;?></option>
        <option value="Yes">Export</option>
            <option value="Yes">MBA Marketing</option>
            <option value="Yes">Phd Computer Sciences</option>
        </select>
        </th>
      </tr>
    <tr>
      <th scope="row" style="font-size:10px" align="left">Trainer Name</th>
      <th colspan="3" align="left" bgcolor="#B8D8D8" style="font-size:10px" scope="row">
        <select name="trainer_name" class="form-control" required = "required" id="trainer_name" disabled >
        <option selected value="<?php echo $TrainerName;?>"> <?php echo $TrainerName;?></option>
        <option value="Yes">Ali</option>
        <option value="No">Aslam</option>
        <option value="No">Iqbal</option>


        </select>
        </th>
      </tr> 
    <tr>
      <th scope="row" style="font-size:10px" align="left">Date of the Event</th>
      <th width="179" bgcolor="#B8D8D8" scope="row">
        <input type="date" required="required" Value="<?php echo $TrainingDate;?>" name="TrainingDate" id="TrainingDate" disabled /></th>
      <th width="327" scope="row" style="font-size:10px" align="center">Event Duration (Days)</th>
      <th width="457" bgcolor="#B8D8D8" scope="row">
        <input type="number" required="required" Value="<?php echo $TrainingDays;?>" name="TrainingDays" id="TrainingDays" disabled/>
        </th>
      </tr>
    <tr>
      <th scope="row" style="font-size:10px" align="left">Training Categoery</th>
      <th colspan="3" align="left" bgcolor="#B8D8D8" style="font-size:10px" scope="row">
        <select name="TrainingCat" class="form-control" Value="<?php echo $TrainingCat;?>" required = "required" id="TrainingCat" disabled >
            <option selected disabled>Post Budget Form</option>
        </select>
        </th>
      </tr>

    <tr>
      <th scope="row" style="font-size:10px" align="left">Training Subcatagoery</th>
      <th colspan="3" align="left" bgcolor="#B8D8D8" style="font-size:10px" scope="row">
        <select name="traningsubcat" class="form-control" required = "required" id="traningsubcat"  >
        <option selected value="<?php echo $TrainingSubCat;?>" disabled><?php echo $TrainingSubCat;?></option>
        <!--<option disabled>Select Options</option>
        <option value="No" disabled>In House Training </option>
        <option value="No" disabled>YEP</option>
        <option value="No" disabled>T-Desk Inhouse</option>
        <option value="No" disabled>T-Desk Outsource</option>
        <option value="No" disabled>T-Desk Online</option> -->
        </select>
        </th>
      </tr>
      <tr>
          <th scope="row" style="font-size:10px" align="left">Venue</th>
          <th width="179" bgcolor="#B8D8D8" scope="row">
              <input type="text" Value="<?php echo $TrainingVenue;?>" required="required" name="Venue" id="Venue" disabled /></th>
          <th width="327" scope="row" style="font-size:10px" align="center">City</th>
          <th width="457" bgcolor="#B8D8D8" scope="row">
              <input type="text" required="required" Value="<?php echo $TrainingCity;?>" name="City" id="City" disabled />
          </th>
      <tr>
      <th scope="row" style="font-size:10px" align="left">Partner Organization (If any) </th>
      <th colspan="3" bgcolor="#B8D8D8" scope="row" align="left">
        <input type="text" name="PartnerOrg" Value="<?php echo $TrainingPartOrg;?>" id="PartnerOrg" disabled/>
        </th>
      </tr>
  </table>
  </div>
  <table class="table table-bordered" width="1065" height="771" border="1" align="center">
  </div>
  <tr>

从哪里获取 ID:



$query = "select * from tbl_usr_training_master";
$rs = mysqli_query($con, $query);
$html ="";
$i=1;

while($row = mysqli_fetch_array($rs))
{
$html .='<tr>
    <td>'.$row['trai_master_id'].'</td>
    <td>'.$row['trai_name'].'</td>
    <td>'.$row['trai_expertise'].'</td>
    <td>'.$row['tari_program_title'].'</td>
    <td>'.$row['trai_date'].'</td>
    <td>'.$row['Venue'].'</td>
    <td>'.$row['City'].'</td>
    <td>'.$row['trai_type_cata'].'</td>
    <td>'.$row['trai_type_subcata'].'</td>
    <td>'.$row['t_days'].'</td>
    <td>'.$row['trai_status'].'</td>' . '
    <td>
                            <a href="#" class="view" title="View" data-toggle="tooltip"><i class="material-icons">&#xE417;</i></a>
                            <a href="post_form.php?rid='.$row['trai_master_id'].'" class="edit" title="Close Pre Budget Form" data-toggle="tooltip"><i class="material-icons">&#xE254;</i></a>
                            <a href="#" class="delete" title="Delete" data-toggle="tooltip"><i class="material-icons">&#xE872;</i></a>
                        </td>
</tr>';
$i=1;
}

最佳答案

根据 MDN doc :

The Boolean disabled attribute, when present, makes the element not mutable, focusable, or even submitted with the form.

$_POST 数组中没有任何被禁用的输入字段,程序将给出 undefined index 。大多数元素都被禁用。

一种常见的解决方案是使用“隐藏”输入元素,如 this post 中所述。 .

关于php - undefined index 从 HTML 表单中获取值时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59574323/

相关文章:

php - 数据库设计: best practice for designing a forum table

php - 在 Flash 应用程序中禁用 PHP 或 JavaScript 中的浏览器缓存

javascript - 可视化 html5 Canvas

javascript - 将 JSP 值传递给 javascript

MySQL 按 $variable 排序

php - 警告 : mysqli_connect(): (HY000/1049): Unknown database in mac terminal only

php - IN() 函数不使用百分号返回结果

php - 获取Wordpress数组中Post的值

jquery - 使用 jQuery 在文本的特定部分之前添加 <br/>

mysql - 连接三个表,不显示所有记录