PHP将数据插入从另一个表中选择的一个表中:未定义索引

时间:2018-07-03 11:26:42

标签: php mysql

我对PHP非常陌生。我必须将HTML表数据插入Mysql DB。我有一个HTML表,在其中使用Select查询从一个数据库中获取数据值。现在,我要在同一张表中添加一个文本框,并将整个表插入到新的DB表中。

我尝试使用此代码,但是每次给我错误时都显示为未定义索引。我试图回显值以了解数据是否正确传递。它正在过去。但我不明白为什么会出现此错误。

请帮助!

代码:(选择表格)

<form class="form-horizontal" action="insert-docket-details.php" method="post" name="myform" autocomplete="off">
  <?php
 echo " <thead>
      <tr>
          <th>Order ID</th>
          <th>Date</th>
          <th>Name</th>
          <th>Store Name</th>
          <th>Zip</th>
          <th>City</th>
          <th>Address</th>
          <th>Contact</th>
          <th>Tv Varient</th>
          <th>Price</th>
          <th>Delivery</th>
          <th>Action</th>
      </tr>
  </thead>
  <tbody>";
  while($row = mysqli_fetch_array($result))
  {
    $id = $row['id'];
    echo"<tr id='row_$id'>";
    echo "<td>" . $row['id'] . "</td>";
    $date= $row['rdate']; echo "$date"; 
    echo "<td>" . $row['rdate'] . "</td>";
    echo "<td>" . $row['rname'] . "</td>";
    echo "<td>" . $row['rstore'] . "</td>";
    echo "<td>" . $row['rzip'] . "</td>";
    echo "<td>" . $row['rcity'] . "</td>";
    echo "<td>" . $row['raddress'] . "</td>";
    echo "<td>" . $row['rphone'] . "</td>";
    echo "<input type='hidden' id='row_id' value='$id'/>";
    echo "<td> <input  id='test' class='c-font-sm' name='docket'></input> </td>";
    echo"</tr>";
  }
           ?>
  </tbody>
  <button  type='submit' name='submit' class='btn c-theme-btn c-btn-square c-btn-uppercase c-btn-bold'>Submit</button>

</table>

Insert-docket-details.php

<?php
//error_reporting(0);
session_start();
$link = mysqli_connect("localhost", "root", "", "project");
if($link === false){
  die("ERROR: Could not connect. " . mysqli_connect_error());
}

$docket=mysqli_real_escape_string($link,$_REQUEST['docket']);
//$docket=mysqli_real_escape_string($link,$_REQUEST['rdocket']);    

$date=$_POST['date'];
$zip=$_POST['zip'];
$storen=$_POST['store'];
$name=$_POST['name'];
$contact=$_POST['phone'];
$city=$_POST['rcity'];

$sql="INSERT INTO project_docket(rdocket,rdate,rzip,rstore,rname,rphone,rcity) values ('$docket','$date','$zip','$storen','$name','$contact','$city')";

  if($result=mysqli_query($link,$sql))
  {
      echo "Records added successfully. ";

    }

else{
  echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
?>

1 个答案:

答案 0 :(得分:0)

您需要带有值名称的输入标签,以便能够从%_POST数组中检索它们。

while($row = mysqli_fetch_array($result)) {
    $id = $row['id'];
    echo "<tr id='row_$id'>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . '<input type="hidden" name="date" value="'. $row['rdate']. '">' . $row['rdate'] . "</td>";
    // and so on
    echo "<input type='hidden' id='row_id' value='$id' /></td>";
    echo"</tr>";
}

$date = $_POST['date'];
// and so on

您可能需要一种使输入名称唯一的方法。您可以使用增量值或ID来使每个名称唯一。