Rick Skeels Rick Skeels - 19 days ago 5
PHP Question

Move a row in mysqli into another table

I have two tables one named Contacts and one named Repairs.

here is the contents from Repairs

id
name
phone
address
budget
due


in contacts i have the following columns

id
name
phone
address


is there a way to sync the 2 tables for certain rows?

name
phone
address


i am looking for a way to add a button that will move and valadate to make sure its not already done on the rows in Repairs of

name
phone
address


I do not want to delete the row in repairs

the code for the button i have so far is

<a href="add_to_contacts.php?id#<?php echo$id; ?>Add to Contacts</a>


But i not a clue on what to have in my add_to_contacts.php

i would thought it would be something like

<?php
include 'db_connect.php';

function move_row($id = 0) {
$query = $this->db->query('select * from repairs where id = ' . $id);
$row = $query->row_array();
$this->db->insert('contacts',$name);
}
?>


Please not the last bit of code is what i got so far it might be a million miles away but all i can say is i tried.

What would be the right code to use here?

Update

this is the new code

<?php
//include database connection
include 'db_connect.php';



$query = "INSERT INTO contacts (name, phone, address) SELECT name, phone, address FROM repair_jobs
where id='".$mysqli->real_escape_string($_REQUEST['id'])."'
limit 0,1";

?>

Answer

You can do this with INSERT ... SELECT Syntax

INSERT INTO contacts (name, phone, address) SELECT name, phone, address FROM repairs WHERE id = $id

Like this:

<?php    
//include database connection
include 'db_connect.php';

$query = "INSERT INTO contacts (name, phone, address) 
          SELECT name, phone, address FROM repair_jobs 
          WHERE id='".$mysqli->real_escape_string($_REQUEST['id'])."' 
          LIMIT 0,1";

$mysqli->query($query);
?>