abcd
abcd

Reputation: 109

Inserting value in table with the click of a button

I'm working on a file called test.php.

What I'm trying to do is, to insert a value into a table by clicking a submit button

I have two tables, tbl_1 and tbl_2

tbl_1

id | name | age


4  | john |  20

9  | tim  |  25

8  | lea  |  22

While tbl_2 have the same structure as tbl_1, it is empty or without any value in it... the idea is that I want to have a button, after clicking on that button, it will insert the value of tbl_1 into tbl_2 based on the id

The question is, how do I fill in tbl_2 with the value of tbl_1, through a click of a submit button?

I made a form, trying to bring the value from tbl_1 to tbl_2, but to no avail...

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

Below is how my table looks like on the web.

enter image description here

Below is the code for the table I constructed.

           <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>

Upvotes: 1

Views: 2302

Answers (2)

Yusuf Hassan
Yusuf Hassan

Reputation: 2013

Since you already mentioned tbl_2 has the same structure as tbl_1, you may simply construct your DB query as:

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;

What this does:

It scans tbl_1 and inserts any values that are missing in tbl_2 table. It has the potential to insert duplicate values. For that, you should have enough robust DB structure that avoids duplicate values in a table (using unique keys etc).

Upvotes: 1

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

There are couple of things you need to change in your code, such as:

  • You need to change your form's action attribute in the following way,

    <form action="test.php?id=<?php echo $readrow['id']; ?>" method="post">
        <input type="submit" class="btn btn-info btn-xs" name="submit" value="Copy"/>
    </form>
    
  • Subsequently, you have to catch a particular row's id in test.php page the following way,

    $id = (int)$_GET['id'];
    
  • Finally perform your INSERT operation in the following way,

    if(isset($_POST['submit'])){
        $id = (int)$_GET['id'];
        $SQL = "INSERT INTO tbl_2(name, age) SELECT name, age FROM tbl_1 WHERE id = {$id}";
        $result = mysql_query($SQL);
    }
    

Sidenote: Don't use mysql_* functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use mysqli or pdo instead. And this is why you shouldn't use mysql_* functions.

Upvotes: 2

Related Questions