只需单击按钮

时间:2017-05-07 10:08:11

标签: php html mysql

我正在处理名为test.php的文件。

我要做的是,通过点击提交按钮将值插入表格

我有两个表,tbl_1tbl_2

tbl_1

id | name | age


4  | john |  20

9  | tim  |  25

8  | lea  |  22

虽然tbl_2与tbl_1具有相同的结构,但它是空的或没有任何值...我的想法是我想要一个按钮,在点击该按钮后,它会插入值根据ID

tbl_1加入tbl_2

问题是,如何通过点击提交按钮为tbl_2填写tbl_1的值?

我制作了一张表单,试图将tbl_1的价值带到tbl_2,但无济于事......

 <form action="test.php?id" method="post">
    <input type="submit" class="btn btn-info btn-xs" name="submit" value="Copy"/>
</form>

 <?php
if(isset($_POST['submit']))
{
     $io = $_POST['io'];
     $no_kes = $_POST['no_kes'];
     $SQL = "INSERT INTO tbl_2(name, age) VALUES ( '".$name."', '".$age."')";
     $result = mysql_query($SQL);
}
?>

以下是我在网上的表格。

enter image description here

以下是我构建的表的代码。

           <table class="table table-striped table-bordered">
    <tr>
      <th>Id</th>
      <th>Name</th>
      <th>Age</th>

      <th></th>
    </tr>
  <?php
  // Read
  $per_page = 7;
  if (isset($_GET["page"]))
    $page = $_GET["page"];
  else
    $page = 1;
  $start_from = ($page-1) * $per_page;


  $sql = "select * from tbl_1";
  $result = $mydb->query($sql);
  while ($readrow = $result->fetch_array()) {
  ?>   
  <tr>
    <td><?php echo $readrow['id']; ?></td>
    <td><?php echo $readrow['name']; ?></td>
    <td><?php echo $readrow['age']; ?></td>
     <td>

1 个答案:

答案 0 :(得分:1)

由于您已经提到过tbl_2与tbl_1具有相同的结构,因此您可以简单地构建数据库查询:

insert into tbl_2(id, name, age) select tbl_1.id, tbl_1.name, tbl_1.age from tbl_1 left join tbl_2 on tbl_1.id = tbl_2.id;

这是做什么的:

  

它扫描tbl_1并插入缺少的任何值   tbl_2表。它有可能插入重复值。对于   那,你应该有足够强大的数据库结构,避免   表中的重复值(使用唯一键等)。